You are on page 1of 63

Micro Processors Lab Demo

T. Krishna Chaitanya
Assembly Language Today
Assembly Language is a low level programming language
 A program written directly in assembly language has the
potential to be smaller and to run faster than a HLL program

 But it takes too long to write a large program in assembly


language
• Only time-critical procedures are written in assembly
language (optimization for speed)

 Assembly language are often used in embedded system


programs stored in PROM chips
• Computer cartridge games, micro controllers, …
General Purpose Registers

8086 CPU has 8 general purpose registers, each register has its own name:

AX - the accumulator register (divided into AH / AL).


BX - the base address register (divided into BH / BL).
CX - the count register (divided into CH / CL).
DX - the data register (divided into DH / DL).

SI - source index register.


DI - destination index register.
BP - base pointer.
SP - stack pointer.
Segment Registers

CS - points at the segment containing the current program.


DS - generally points at segment where variables are defined.
ES - extra segment register, it's up to a coder to define its usage.
SS - points at the segment containing the stack.
8086 Instruction - Basic Structure
Label Operator Operand[s] ;Comment

Label - optional alphanumeric string


1st character must be a-z,A-Z,?,@,_,$
Last character must be :
Operator - assembly language instruction
mnemonic: an instruction format for humans
Assembler translates mnemonic into hexadecimal opcode
example: mov is f8h
Operand[s] - 0 to 3 pieces of data required by instruction
Can be several different forms
Delineated by commas
immediate, register name, memory data, memory address
Comment - Extremely useful in assembler language

These fields are separated by White Space (tab, blank, \n, etc.)
8086 Instruction - Example

Label Operator Operand[s] ;Comment

INIT: mov ax, bx ; Copy contents of bx into ax

Label - INIT:
Operator - mov
Operands - ax and bx
Comment - alphanumeric string between ; and \n

• Not case sensitive


• Unlike other assemblers, destination operand is first
• mov is the mnemonic that the assembler translates into an
opcode
Assembler Language Segment Types

 Stack
– For dynamic data storage
– Source file defines size
– Must have exactly 1
 Data
– For static data Storage
– Source file defines size
– Source file defines content (optional)
– Can have 0 or more
 Code
– For machine Instructions
– Must have 1 or more
x86 Instruction Type Classifications

 DATA TRANSFER
– General mov ax, [DAT1] ;ax gets contents of mem
– Strings cmpsb ;if DS:SI=ES:DI then ZF=1
– Special Purpose xchg ax, bx ;ax gets bx and bx gets ax
 ARITHMETIC/LOGIC
– Integer add ax, bx ;ax gets ax+bx
– ASCII, BCD aaa ;changes ASCII # to int.
– Floating Point fadd DAT ;ST get ST+DAT
– Logical and ax, bx ;ax gets ax AND bx
– Shifting ror ax, 2 ;ax contents shifted-2 right
 CONTROL TRANSFER
– Branching jnz LABEL1 ;if ZF=1 then IP=LABEL1
– Interrupt int 21h ;invoke INT handler 21h
– Subroutine call SUB1 ;invoke subroutine, SUB1
– Modify Flag cli ;IF gets zero
– Halt Processor hlt ;need RESET to run again
– No Operation nop ;
Data Transfer Instructions

 MOV target, source  reg can be any non-


– reg, reg segment register
– mem, reg except IP cannot be the
– reg, mem target register
– mem, immed  MOV's between a
– reg, immed segment register and
 Sizes of both operands memory or a 16-bit
must be the same register are possible
Data Allocation Directives

db define byte
dw define word (2 bytes)
dd define double word (4 bytes)
dq define quadword (8 bytes)
dt define tenbytes
equ equate, assign numeric expression to a name
Examples:
db 100 dup (?) define 100 bytes, with no initial values for bytes
db “Hello” define 5 bytes, ASCII equivalent of “Hello”.
Sample MOV Instructions

b db 4Fh  When a variable is created with a


w dw 2048 define directive, it is assigned a
default size attribute (byte, word,
mov bl,dh etc)
mov ax,w  You can assign a size attribute
mov ch,b using LABEL
mov al,255 LoByte LABEL BYTE
mov w,-100 aWord DW 97F2h
mov b,0
MOV AX, 0B800h ; set AX to hexadecimal value of B800h.

MOV DS, AX ; copy value of AX to DS.

MOV CL, 'A' ; set CL to ASCII code of 'A', it is 41h.

MOV CH, 1101_1111b ; set CH to binary value.

MOV BX, 15Eh ; set BX to 15Eh.

MOV [BX], CX ; copy contents of CX to memory at B800:015E

RET ; returns to operating system.


eXCHanGe

 XCHG target, source  This provides an


– reg, reg efficient means to swap
– reg, mem the operands
– mem, reg – No temporary storage is
needed
 MOV and XCHG
– Sorting often requires
cannot perform memory
this type of operation
to memory moves – This works only with the
general registers
Arithmetic Instructions

ADD dest, source  source can be a


SUB dest, source general register,
memory location, or
INC dest
constant
DEC dest
 dest can be a register
NEG dest or memory location
 Operands must be of – except operands cannot
the same size both be memory
Program Segment Structure

 Data Segments  Stack Segment


– Storage for variables – used to set aside storage
– Variable addresses are for the stack
computed as offsets – Stack addresses are
from start of this computed as offsets into
segment this segment
 Code Segment  Segment directives
– contains executable .data
instructions .code
.stack size
Program Skeleton

.model small  Select a memory model


.stack 100H  Define the stack size
.data  Declare variables
;declarations  Write code
.code – organize into procedures
main proc  Mark the end of the
;code source file
main endp – optionally, define the entry
;other procs point
end main
Write programme on the screen
eg: File name: [ d:\ add.asm…..]
Next step is to create executable file using the linker:
This causes linker to create: ADD.EXE
Press F 7
Press F 7
Press F 7
Press F 7
Press F 7
Press F 7
;add two 8-bit numbers
data segment
addend db 20h
adder db 10h
sum db ?
data ends

code segment
assume cs:code, ds:data

start: mov ax,data ;


mov ds,ax ;ds <-- initialize data segment register
mov al,addend ; move addend to the al
mov bl,adder ; move adder to the to bl
add al,bl ; add al to bl
mov sum,al ; move sum in to al
mov ax,4c00h ; ax <-- 4c DOS 21h program halt function
int 21h ;DOS service interrupt

code ends
end start
data segment
n dw 5
res dw ?
data ends

code segment
assume cs:code,ds:data

mov ax,data
mov ds,ax

mov ax,n
sub ax,1
mov bx,ax
add ax,1
yy: mul bx
dec bx
jz down
loop yy
down:mov res,ax
mov ax,4c00h
int 21h

code ends

end
Write programme on the screen
Next step is to create executable file using the linker:
This causes linker to create: ADD.EXE
Write programme on the screen
Press F 7
Press F 7
Press F 7
Press F 7
Press F 7
Press F 7
;add two 8-bit numbers
data segment
addend db 20h
adder db 10h
sum db ?
data ends

code segment
assume cs:code, ds:data

start: mov ax,data ;


mov ds,ax ;ds <-- initialize data segment register
mov al,addend ; move addend to the al
mov bl,adder ; move adder to the to bl
add al,bl ; add al to bl
mov sum,al ; move sum in to al
mov ax,4c00h ; ax <-- 4c DOS 21h program halt function
int 21h ;DOS service interrupt

code ends
end start
data segment
n dw 5
res dw ?
data ends

code segment
assume cs:code,ds:data

mov ax,data
mov ds,ax

mov ax,n
sub ax,1
mov bx,ax
add ax,1
yy: mul bx
dec bx
jz down
loop yy
down:mov res,ax
mov ax,4c00h
int 21h

code ends

end

You might also like