You are on page 1of 14

Data Transfer Instructions

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


• reg, reg segment register except
• mem, reg IP cannot be the target
• reg, mem 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

2/4/2015 Ali Saleh - Assembly Language Programming 1


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
mov ch,b
mov al,255
mov w,-100
mov b,0

2/4/2015 Ali Saleh - Assembly Language Programming 2


Addresses with Displacements

b db 4Fh, 20h, 3Ch • The assembler


w dw 2048, -100, 0 computes an address
based on the expression
mov bx, w+2 • NOTE: These are address
mov b+1, ah computations done at
assembly time
mov ah, b+5 MOV ax, b-1
mov dx, w-3 will not subtract 1 from the
• Type checking is still in value stored at b
effect

2/4/2015 Ali Saleh - Assembly Language Programming 3


eXCHanGe

• XCHG target, source • This provides an


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

2/4/2015 Ali Saleh - Assembly Language Programming 4


Arithmetic Instructions

ADD dest, source • source can be a general


SUB dest, source register, memory
location, or constant
INC dest
• dest can be a register or
DEC dest
memory location
NEG dest • except operands cannot
• Operands must be of both be memory
the same size

2/4/2015 Ali Saleh - Assembly Language Programming 5


Arithmetic
•Only integer operations possible unless machine has
FPU (coprocessor).
•Modern CPUs such as Pentium have on-chip FPU,
I.e., no separate chip is required.
•Pentium notorious for FP bug.

2/4/2015 Ali Saleh - Assembly Language Programming 6


Examples: inc, dec
•INC, DEC byte or word operand which may be
register or memory:
•inc al ; al:=al + 1
•dec bx ; bx:=bx - 1
•inc membyte;
•dec byte ptr membyte
•dec memword

•All flags except carry may be affected.

2/4/2015 Ali Saleh - Assembly Language Programming 7


Addition
•add destination, source:
•destination:=destination + source
•addal, 1 ; al:=al + 1
•addcl, al ; cl:=cl + al
•addbx, 100h ; bx:=bx + 100h
•addvar1, ax ; var1:=var1 + ax

•All flags may be affected.

2/4/2015 Ali Saleh - Assembly Language Programming 8


Subtraction
•sub destination, source:
•destination:=destination - source
•sub al, 1 ; al:=al - 1
•sub cl, al ; cl:=cl - al
•sub dx, var1 ; dx:=dx - var1
•sub var1, 10 ; var1:=var1 - 10
•All flags may be affected.

2/4/2015 Ali Saleh - Assembly Language Programming 9


Carry flag
•If the result of an addition or subtraction is too large
for the destination operand, the carry flag is set:
• mov al, 0FFH
• add al, 20H ; carry flag set

2/4/2015 Ali Saleh - Assembly Language Programming 10


sign flag
•You should try this with debug and verify it.
•If source is larger than destination then sub requires
a borrow:

• mov al, 5
• sub al,10 ; sign flag set

2/4/2015 Ali Saleh - Assembly Language Programming 11


Zero Flag
•If result is zero, then zero flag is set:
•mov al, 4 ; put 4 in al
•sub al, 4 ; zero flag set

2/4/2015 Ali Saleh - Assembly Language Programming 12


Overflow flag
•Set whenever an addition operation generates a
signed number that is out of range.
•Valid ranges are -128..127 for byte (short int) and -
32768..32767 for word (int).
•If the appropriate range is violated, then the
overflow flag is set.
• mov cl, 126
• add cl, 2 ; overflow set; cl = -128 (80h)

2/4/2015 Ali Saleh - Assembly Language Programming 13


NEG
• Negate. Makes operand negative (two's complement).
• Algorithm:
• Invert all bits of the operand
• Add 1 to inverted operand
• Example:
MOV AL, 5 ; AL = 05h
NEG AL ; AL = 0FBh (-5)
NEG AL ; AL = 05h (5)

2/4/2015 Ali Saleh - Assembly Language Programming 14

You might also like