Professional Documents
Culture Documents
Dr Philip Leong
phwl@doc.ic.ac.uk
Topics
Expressions
Instruction types
Instructions vary from one CPU to another, General groupings possible:
Arithmetic/Logic
Data Movement
Transfer of Control
Test/Compare
Input/Output
Others
Halt, NOP
Operation
Notes
ADD
SUB
CMP
INC
DEC
NEG
Addition
Subtraction
Compare & Set FLAGS
Increment by 1
Decrement by 1
Negate
dst, src
dst, src
dst, src
opr
opr
opr
Operation
Notes
IMUL opr
AX := AL * opr
DX:AX := AX * opr
EDX:EAX := EAX * opr
IDIV opr
AL := AX / opr
AH := AX mod opr
Word / Byte
AX := (DX:AX) / opr
DX := (DX:AX) mod opr
Doubleword / Word
Operation
More Instructions
Instruction
Operation
Notes
SAL dst, N
N
N
dst := dst / 2
SAR dst, N
dst := dst * 2
AX := AL
DX:AX := AX
EAX:= AX
CBW, CWD, CWDE extend a signed integer by filling the extra bits of
destination with the sign bit of the operand (i.e. preserve value of result)
Computer Architecture (P. Leong)
Expressions
DW
DW
DW
0
0
0
Example
; alpha := 7; beta := 4; gamma := -3
; alpha := (alpha * beta + 5 * gamma) / (alpha beta)
MOV
IMUL
MOV
MOV
gamma
AX, BX
;
;
beta
BX, AX
AX, 5
IMUL
ADD
MOV
AX
BX
CX
DX
AX, alpha
0007
IMUL
001C
0000
;
;
;
MOV
001C
001C
0000
AX := alpha
(DX:AX) := 5 * gamma
AX := 5 * gamma + alpha * beta
MOV
0005
001C
0000
IMUL
ADD
FFF1
001C
000D
001C
FFFF
FFFF
Regs shown
in Hex
Example Continued
; alpha := 7; beta := 4; gamma := -3
; alpha := (alpha * beta + 5 * gamma) / (alpha beta)
MOV
BX, alpha
BX := alpha
SUB
CWD
IDIV
BX, beta
;
;
;
;
BX := alpha beta
Sign extend AX to DX
AX := (DX:AX) / operand
DX := (DX:AX) % operand
MOV
alpha, AX
BX
Prev
AX
BX
CX
DX
000D
001C
FFFF
MOV
;
SUB
CWD
000D
0007
000D
0003
000D
0003
FFFF
FFFF
0000
IDIV
0004
0003
MOV
0004
alpha
0001
8086 Instructions (page 11)
Integer Overflow
or if
A + B < -128
ov_label:
ADD
JO
...
BL,
0
zero_div
....
Operation
Notes
AND
TEST
OR
XOR
NOT
Bitwise
Bitwise
Bitwise
Bitwise
Bitwise
dst,
dst,
dst,
dst,
opr
src
src
src
src
AND
AND and set FLAGS
OR
XOR
NOT
Typical Uses
AND is used to clear specific bits (given by 0 bits in src) in the dst.
OR is used to set specific bits (given by 1 bits in src) in the dst.
XOR is used to toggle/invert specific bits (given by 1 bits in src) in the dst.
TEST is used to test specific bit patterns.
Computer Architecture (P. Leong)
Booleans
AL, man
AL, rich
AH, man
AH
AL, AH
okay, AL
;
;
;
;
;
;
AL := man
AL := man AND rich
AH := man
AH := NOT man
AL := (man AND rich) OR NOT man
okay := AL
8086 Instructions (page 15)
JUMP Instructions
Jump instructions take the form OPCODE label, e.g. JGE next
Opcode
Flag Conditions
Notes
JMP
JE or JZ
JNE or JNZ
Unconditional
ZF = 1
ZF = 0
Jump
Jump if Equal or Jump if Zero (=)
Jump if Not Equal or Jump if Not Zero
Signed Comparisons
JG
JGE
JL
JLE
ZF = 0 and SF = 0 Jump
SF = 0
Jump
SF = 1
Jump
ZF = 1 or SF = 1 Jump
if
if
if
if
Miscellaneous
JO
JNO
OF = 1
OF = 0
JCXZ
JECXZ
CX = 0
ECX = 0
Jump if CX = 0
Jump if ECX = 0
If Statement
If age<100 then
statements
if:
end if
CMP
JL
JMP
age, 100
stats
endif
stats:
if:
CMP
age, 100
JGE
endif
; statements
endif:
; statements
endif:
If (age >= 21) and (age <= 65) then
if:
statements
end if
CMP
age, 21
JL
endif
CMP
age, 65
JG
endif
; statements
endif:
Computer Architecture (P. Leong)
IF-Then-Else Statement
If age < 100 then
statements1
if:
else
statements2
CMP
JGE
age, 100
else
; statements1
JMP
endif
end if
else:
; statements2
endif:
While Loop
loop
exit when age > 99
statements
end loop
while: CMP
JG
age, 99
endwhile
; statements
JMP
while
endwhile:
Repeat Loop
loop
statements
repeat:
; statements
CMP
JLE
age, 99
repeat
endrepeat:
For Loop
for age : 1 .. 99
for:
MOV
age, 1
end for
next:
CMP
JG
age, 99
endfor
statements
; statements
INC
JMP
age
next
endfor:
Think about
1.
2.
3.