Part7 ArithmeticAndLogic PDF
Part7 ArithmeticAndLogic PDF
Thorne : Chapter 6, 7, 9
(Irvine, Edition IV : 4.1, 4.2, 6.2 7.2, 7.3, 7.4)
SYSC3006 1
Breakdown of Intel 8086 Assembly Instructions
SYSC3006 3
Signed versus Unsigned Arithmetic
SYSC3006 4
Arithmetic Operations : Binary Addition and Subtraction
8-bit Signed Integer Examples:
• The computer does exactly the same thing for 2’s complement
signed integers! ☺
Signed is now being
-11710 = 1000 10112 used to mean 2’s
complement signed
+ 9910 = + 0110 00112
- 1810 = 1110 11102 ( 0001 00012 +1 = 12h=18d)
-32
3210 = 1110 00002
– 510 = – 0000 01012
-3710 = 1101 10112 ( 0010 01002 +1 = 25h=37d)
SYSC3006 5
Arithmetic Operations : Binary Addition and Subtraction
SYSC3006 6
Overflow : The Concept
Overflow:
• Carry flag (CF) for unsigned numbers
• Overflow flag (OF) for singed numbers
SYSC3006 8
Addition and Subtraction Overflow
Another
A th example:
l unsigned
i d signed
i d
0111 11112 127 127
+ 0000 00012 + 1 + 1
1000 00002 128 – 128
CORRECT WRONG
OVERFLOW …
CF = 0 (CF → unsigned number) even though
OF = 1 (OF → signed number) there is no carry
SF = 1 outside
id off fixed
fi d
width!
SYSC3006 10
Overflow Cookie Cutters
SYSC3006 11
Data Manipulation : ADD
Unsigned Signed
JA Above JG Greater
JAE Above or Equal JGE Greater or Equal
JB Below JL Less
JBE Below or Equal JLE Less or Equal
• There are also instructions for Not conditions too!
SYSC3006 15
Example : Conditional Branches
Suppose AL contains 7FH:
Unsigned Scenario Signed Scenario
CMP AL,, 80h CMP AL,80h
,
JA Bigger JG Bigger
SYSC3006 16
Limitation of JJ* Instructions
SYSC3006 17
Example Write a code fragment showing how you would implement the
following pseudocode
boolean done = FALSE;
while ( ! done )
{ …. }
Solution: TRUE equ 1
FALSE equ 0
.code
MOV AL
AL, FALSE ; AL= register variable done
notDone: CMP AL, TRUE
JE amDone
; ....; Somewhere : MOV AL, TRUE
JMP notDone
amDone: …
SYSC3006 18
LOOP Instruction
• U
Useful
f l when
h you have
h an action
ti repeated
t d a given
i number
b off
times
• C++ analogy
for (int i=max; i > 0; i--)
SYSC3006 19
Data Manipulation : DIV
Unsigned Integer Division
• Syntax : DIV src
• Semantics : Performs an integer division : Accumulator / src
– The size of divisor (8-bit or 16-bit) is determined by size of
src
– src may be specified using register, direct or indirect mode
but not immediate mode 16-bit dividend
bit divisor
88-bit
• 8-bit division : DIV src where src = 8-bit operand
– Semantics divide src into 16-bit value in AX
Two
wo 88-bit
bt Integer result
results – AL := AX ÷ src (
(unsigned
i d di
divide)
id )
AH := AX mod src ( unsigned modulus) Integer remainder
– The flags are undefined after DIV (values may have
changed,
h d no meaning)
i )
SYSC3006 20
Data Manipulation : DIV 32-bit dividend
16-bit divisor
Example : .data
control DB ? AND OR XOR
.code
code 0 0 0 0 0
OR control, BH 0 1 0 1 1
(where BH=02h) 1 0 0 1 1
1 1 1 1 0
Example : XOR AX, AX
XOR AX
AX, 0FFh O e ti bit by
Operation b bit!
SYSC3006 22
Data Manipulation : Shift
SHR AL, 1
Rotate-Carry-Left
C f
RCL AL, 1
b7 ----------------- b0 C
Rotate Left
SYSC3006 24
Examplep Write a code fragment
g to test whether a variable is
divisible by 4, leaving the boolean result in AX.
Solution: A number divisible by 4 would have the least significant
two bits equal
q 0s.
FALSE equ 0
TRUE equ 1
.data
variable dw 1922h
.code
code
MOV AX, variable
Alterative :
AND AX, 03h
JZ yes TEST variable,
i bl 03h
MOV AX, FALSE
JMP continue
yes: MOV AX TRUE
AX,
continue:
SYSC3006 25
…
Example Suppose a robot has four motors, each of which can be
off,
ff on iin forward
f d direction
di ti or on in i reverse direction.
di ti The
Th status
t t
of these motors are written by the robot into a status word, say
called “motors” in the following bitmap formation.
7 6 5 4 3 2 1 0
Motor1 Motor2 Motor3 Motor4
where
h the h two bits
bi for
f eachh motor are set according
di
01 forward
10 reverse
11 off
Write a code fragment that waits until motor1 is off before
continuing on.
SYSC3006 26
Solution: .data
data
motors db ?
.code
waiting: MOV AL,
AL motors
AND AL, 0C0h
CMP AL, 0C0h
JNZ waiting
…
SYSC3006 27