COMSATS Institute of
Information Technology
CCS221 COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE
Asif Muhammad Malik
[email protected]
Quote of the day
You can always tell a real friend:
when you've made a fool of
yourself
he doesn't feel you've done a
permanent job
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
Quick recap
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
INC and DEC Instructions
The INC (increment) and DEC (decrement) instructions, respectively, add 1 and
subtract 1 from a register or memory operand. The syntax is
INC reg/mem
DEC reg/mem
Example:
.data
myWord WORD 1000h
.code
inc
myWord ; myWord = 1001h
mov bx,
myWord
dec bx
; BX = 1000h
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
ADD Instruction
The ADD instruction adds a source operand to a destination operand of the same size
ADD dest,source
The set of possible operands is the same as for the MOV instruction
.data
var1 DWORD
10000h
var2 DWORD
20000h
.code
mov eax,
var1 ; EAX = 10000h
add eax,
var2 ; EAX = 30000h
The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are changed
according to the value that is placed in the destination operand
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
SUB Instruction
The SUB instruction subtracts a source operand from a destination operand
SUB dest,source
Example:
.data
var1
DWORD
30000h
var2
DWORD
10000h
.code
mov eax, var1
; EAX = 30000h
sub eax, var2
; EAX = 20000h
The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags are changed
according to the value that is placed in the destination operand
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
NEG Instruction
The NEG (negate) instruction reverses the sign of a
number by converting the number to its twos
complement
The following operands are permitted:
NEG reg
NEG mem
Recall that the twos complement of a number can be
found by reversing all the bits in the destination
operand and adding 1
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
Implementing Arithmetic
Expressions
Rval = -Xval + (Yval - Zval);
Lets see how the sample statement would be implemented in assembly language. The following signed 32-bit variables
will be used:
Rval SDWORD ?
Xval SDWORD 26
Yval SDWORD 30
Zval SDWORD 40
When translating an expression, evaluate each term separately and combine the terms at the end
First, we negate a copy of Xval and store it in a register:
mov eax, Xval ; first term: -Xval
neg eax ; EAX = -26
mov ebx, Yval ; second term: (Yval - Zval)
sub ebx, Zval ; EBX = -10
add eax, ebx
; add the terms and store:
mov Rval,eax ; -36
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
Flags Affected by Addition and
Subtraction
When executing arithmetic instructions, we want to know result:
Negative, Positive, or Zero?
Is it too large / small to fit into the destination operand?
We use the values of CPU status flags to check the outcome of arithmetic
operations
The Carry flag indicates unsigned integer overflow. E.g., if an instruction
has an 8-bit destination operand but the instruction generates a result
larger than 11111111 binary, the Carry flag is set
The Overflow flag indicates signed integer overflow. E.g., if an instruction
has a 16-bit destination operand but it generates a negative result smaller
than -32,768 decimal, the Overflow flag is set
The Zero flag indicates that an operation produced zero. E.g., if an
operand is subtracted from another of equal value, the Zero flag is set
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
10
Flags Affected by Addition and
Subtraction
The Sign flag indicates that an operation produced a
negative result. If the most significant bit of the
destination operand is set, the Sign flag is set
The Parity flag indicates whether or not an even
number of 1 bits occurs in the least significant byte of
the destination operand, immediately after an
arithmetic or Boolean instruction has executed
The Auxiliary Carry flag is set when a 1 bit carries out
of position 3 in the least significant byte of the
destination operand
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
11
Unsigned Operations: Zero,
Carry, and Auxiliary Carry
The Zero flag is set when the result of an arithmetic
operation equals zero:
mov ecx, 1
sub ecx, 1
; ECX = 0, ZF = 1
mov eax, 0FFFFFFFFh
inc
eax
; EAX = 0, ZF = 1
inc
eax
; EAX = 1, ZF = 0
dec eax
; EAX = 0, ZF = 1
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
12
Unsigned Operations: Zero,
Carry, and Auxiliary Carry
Addition and the Carry Flag: The Carry flag is 1 when
the sum exceeds the storage size of its destination
operand
mov al,0FFh
add al,1
4/17/16
; AL = 00, CF = 1
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
13
Unsigned Operations: Zero,
Carry, and Auxiliary Carry
Subtraction and the Carry Flag: A subtract operation
sets the Carry flag when a larger unsigned integer is
subtracted from a smaller one
mov al,1
sub al,2 ; AL = FFh, CF = 1
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
14
Unsigned Operations: Zero,
Carry, and Auxiliary Carry
The Auxiliary Carry flag indicates a carry or borrow out of bit
3 in the destination operand
mov al,0Fh
add al,1 ; AC = 1
Here is the arithmetic:
00001111
+00000001
--------------------00010000
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
15
Unsigned Operations: Zero,
Carry, and Auxiliary Carry
The Parity flag is set when the least significant byte of
the destination has an even number of 1 bits
mov al,10001100b
add al,00000010b ; AL = 10001110, PF = 1
sub al,10000000b ; AL = 00001110, PF = 0
After the ADD instruction executes, AL contains binary
10001110 (four 0 bits and four 1 bits), and PF 1
After the SUB instruction executes, AL contains an odd
number of 1 bits, so the Parity flag equals 0
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
16
Signed Operations: Sign and
Overflow Flags
The Sign flag is set when the result of a signed
arithmetic operation is negative
mov eax, 4
sub eax, 5 ; EAX = -1, SF = 1
The example below shows the hexadecimal values of BL
when a negative result is generated:
mov bl,1 ; BL = 01h
sub bl,2 ; BL = FFh (-1), SF = 1
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
17
Signed Operations: Sign and
Overflow Flags
The Overflow flag is set when the result of a signed
arithmetic operation overflows or underflows the
destination operand
mov al,+127
add al,1 ; OF = 1
Similarly, the smallest possible negative integer byte
value is -128. Subtracting 1 from it causes underflow
mov al,-128
sub al,1 ; OF = 1
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
18
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 negative
Two negative operands are added and their sum is positive
What will be the values of the Overflow flag?
mov al,80h
add al,92h
; OF =
1
mov al,-2
add al,+127
; OF =
19
Signed Operations: Sign and
Overflow Flags
The NEG instruction produces an invalid result if the
destination operand cannot be stored correctly
mov al,-128
; AL = 10000000b
neg al
; AL = 10000000b, OF = 1
On the other hand, if 127 is negated, the result is valid
and the Overflow flag is clear:
mov al,+127
neg al
4/17/16
; AL = 01111111b
; AL = 10000001b, OF = 0
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
20
Example Program
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
21
Summary
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
22
Reading Material
Chapter # 4
Assembly Language for x86 Processors,
Seventh Edition
By KIP R. IRVINE
4/17/16
CCS221 COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE
23