Programming in Assembly Language
Programming in Assembly Language
Programming in Assembly Language
push source
o two registers
o a register and a memory location
o SP is decreased by 2
o a register and a constant
o a copy of the source contents is moved
to SS:SP INC is used to add 1 to the contents of a register or
memory location
Items are removed with pop
pop destination
Examples
o can't have both operands be memory If two words are multiplied, the result is a 32-bit
locations doubleword
For the byte form, one number is contained in the
source and the other is assumed to be in al -- the
Subtraction instructions
product will be in ax
For the word form, one number is contained in the
aas source and the other is assumed to be in ax -- the
most significant 16 bits of the product will be in dx
and the least significant 16 bits will be in ax
o ASCII adjust for subtraction
cmp destination, source Examples
neg destination
mul bx
dx = FFFFh ax = FFFFh
o Subtract with borrow
sub destination, source Division instructions
o Subtract aad
div bx
The or instruction can be used to set specific
destination bits
ax = 0002h dx = 0001h The xor instruction can be used to complement
specific destination bits
div bx
To clear the sign bit of al while leaving the other bits
ax = 0000h dx = 0005h unchanged, use the and instruction with 01111111b
=7Fh as the mask
idiv bx
Divide Overflow
To set the most significant and least significant bits
of al while preserving the other bits, use the or
It is possible that the quotient will be too big to fit in instruction with 10000001b = 81h as the mask
the specified destination (al or ax)
This can happen if the divisor is much smaller than or al,81h
the dividend
When this happens, the program terminates and the
system displays the message "Divide Overflow" To change the sign bit of dx, use the xor instruction
with a mask of 8000h
o The dividend is in dx:ax even if the The not instruction performs the one's complement
actual dividend will fit in ax operation on the destination
o For div, dx should be cleared The format is
o For idiv, dx should be made the sign
extension of ax using cwd
o not destination
Byte division
To complement the bits in ax:
Logic Instructions
The TEST instruction
o Logical OR
test destination, source
0
2
Carry flag
Parity flag
cf
pf
4 Auxiliary carry flag af
Zeros are shifted into the rightmost bit positions and
6 Zero flag zf
the last bit shifted out goes into CF
7 Sign flag sf
11 Overflow flag of Effect on flags:
OF = 0 otherwise
dh contains 8Ah and cl contains 02h
Shift Instructions dh = 10001010, cl = 00000010
after shr dh,cl
Shift and rotate instructions shift the bits in the
destination operand by one or more positions either o dh = 001000010, cf = 1
to the left or right
The instructions have two formats:
The SAR instruction
o opcode destination, 1
o opcode destination, cl
The sar (shift arithmetic right) instruction can be
used to divide an operand by powers of 2
The first shifts by one position, the second shifts by
N positions, where cl contains N (cl is the only sar operates like shr, except the msb retains its
original value
register which can be used)
The effect on the flags is the same as for shr
The SHL (shift left) instruction shifts the bits in the Rotate Instructions
destination to the left.
Rotate Left Table 4.6 (and Table 16.4) shows all the conditional
jumps
o The instruction rol (rotate left) shifts The destination_label must precede the jump
instruction by no more than 126 bytes, or follow it by
bits to the left
no more than 127 bytes
o The msb is shifted into the rightmost bit
o The cf also gets the the bit shifted out of
There are ways around this restriction (using the
unconditional jmp instruction)
the msb
Rotate Right
The CMP Instruction
Rotate through Carry Left cmp is just like sub, except that the destination is
not changed -- only the flags are set
Suppose ax = 7FFFh and bx = 0001h
o The instruction rcl shifts bits to the left
o The msb is shifted into cf
cmp ax, bx
o cf is shifted into the rightmost bit
jg below
Rotate through Carry Right
zf = 0 and sf = of = 0, so control transfers to label below
; if ax < 0
cmp ax, 0 ; ax < 0 ?
Only one cmp is needed, because jump instructions
do not affect the flags
jnl endif ; no, exit
; then
neg ax ; yes, change sign AND conditions
; endif
;initialize cx to loop_count
TOP: The condition is checked at the bottom of the loop
;body of the loop
loop TOP The loop executes until the condition is true
The loop executes 1 or more times
FOR loop example
REPEAT example
a count-controlled loop to display a row of 80 stars
lea destination,source
mov ax,@data
mov ds,ax