Data Transfer, Addressing and Arithmetic: Microprocessor and Assembly Languages
Data Transfer, Addressing and Arithmetic: Microprocessor and Assembly Languages
Arithmetic
Microprocessor and Assembly Languages
.data
myWord dw 1000h
myDword DWORD 10000000h
.code
inc myWord ; 1001h
dec myWord ; 1000h
inc myDword ; 10000001h
mov ax,00FFh
inc ax ; AX = 0100h
mov ax,00FFh
inc al ; AX = 0000h
Your turn...
Show the value of the destination operand after each of t
he following instructions executes:
.data
myByte db 0FFh, 0
.code
mov al,myByte ; AL =
mov ah,[myByte+1] ; AH = FFh
dec ah ; AH = 00h
inc al ; AL = FFh
dec ax ; AX = 00h
FFFF
ADD and SUB Instructions
• ADD destination, source
• Logic: destination destination + source
• SUB destination, source
• Logic: destination destination – source
• Same operand rules as for the MOV
instruction
• Source remains unchanged
• Both operands must be of the same size
• Cannot be both mem operands at the same
time
• ADD and SUB affect all status flags
ADD and SUB Examples
.data
var1 DWORD 10000h
var2 DWORD 20000h
.code ; ---EAX---
mov eax,var1 ; 00010000h
add eax,var2 ; 00030000h
add ax,0FFFFh ; 0003FFFFh
add eax,1 ; 00040000h
sub ax,1 ; 0004FFFFh
NEG (negate) Instruction
Reverses the sign of an operand. Operand can be a register or
memory operand.
.data
valB db -1
valW dw +32767
.code
mov al,valB ; AL = -1
neg al ; AL = +1
neg valW ; valW = -32767
Rval DWORD ?
Xval DWORD 26
Yval DWORD 30
Zval DWORD 40
.code
mov eax,Xval
neg eax ; EAX = -26
mov ebx,Yval ; EBX = 30
sub ebx,Zval ; EBX = -10
add eax,ebx
mov Rval,eax ; -36
Your turn...
Translate the following expression into assembly language. Do
not permit Xval, Yval, or Zval to be modified:
Rval = Xval - (-Yval + Zval)
mov ebx,Yval
neg ebx
add ebx,Zval
mov eax,Xval
sub eax,ebx
mov Rval,eax
Flags Affected by Arithmetic
• The ALU has a number of status flags that reflect the outcome of a
rithmetic (and bitwise) operations
– based on the contents of the destination operand
• Essential flags:
– Zero flag – set when destination equals zero
– Sign flag – set when destination is negative
– Carry flag – set when unsigned value is out of range (unsigned
overflow)
– Overflow flag – set when signed value is out of range (signed o
verflow)
• The MOV instruction never affects the flags.
Concept Map
CPU
part of executes
executes
ALU
conditional jumps
arithmetic & bitwise
operations attached to used by provide
affect
status flags
branching logic
You can use diagrams such as these to express the relationships between assembly
language concepts.
Zero Flag (ZF)
Whenever the destination operand equals Zero, the Zero flag is
set.
mov cx,1
sub cx,1 ; CX = 0, ZF = 1
mov ax,0FFFFh
inc ax ; AX = 0, ZF = 1
inc ax ; AX = 1, ZF = 0
mov ax,00FFh
add ax,1 ; AX= SF= ZF= CF=
sub ax,1 ; AX= 0100h SF= 0 ZF= 0 CF= 0
add al,1 ; AL= 00FFh SF= 0 ZF= 0 CF= 0
mov bh,6Ch 00h 0 1 1
add bh,95h ; BH= SF= ZF= CF=
01h 0 0 1
mov al,2
sub al,3 ; AL= SF= ZF= CF=
FFh 1 0 1
Overflow Flag (OF)
The Overflow flag is set when the signed result of an oper
ation is invalid or out of range.
; Example 1
mov al,+127
add al,1 ; OF = 1, AL = ??
; Example 2
mov al,7Fh ; OF = 1, AL = 80h
add al,1
The two examples are identical at the binary level because 7Fh
equals +127. To determine the value of the destination operand,
it is often easier to calculate in hexadecimal.
A Rule of Thumb
• When adding two integers, remember that the
Overflow flag is only set when . . .
– Two positive operands are added and their sum is ne
gative
– Two negative operands are added and their sum is p
ositive
– Then we have signed overflow
mov al,-128
neg al ; CF = 0 OF = 1
mov ax,8000h
add ax,2 ; CF = OF =
0 0
mov ax,0
sub ax,2 ; CF = OF =
1 0
mov al,-5
sub al,+125 ; CF = OF =
0 1