Professional Documents
Culture Documents
06instructions 4up
06instructions 4up
Expressions
Dr Philip Leong
phwl@doc.ic.ac.uk
Computer Architecture (P. Leong) 8086 Instructions (page 1) Computer Architecture (P. Leong) 8086 Instructions (page 2)
Computer Architecture (P. Leong) 8086 Instructions (page 3) Computer Architecture (P. Leong) 8086 Instructions (page 4)
1
Integer Addition & Subtraction Integer Multiply & Divide
Instruction Operation Notes Instruction Operation Notes
IMUL DestReg, SrcReg, immediate DestReg := SrcReg * immediate SAL/SAR are quick ways of multiplying/dividing by powers of 2. N must be
IMUL DestReg, MemOpr, immediate DestReg := MemOpr * immediate a constant (immediate value) or the byte register CL.
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) 8086 Instructions (page 7) Computer Architecture (P. Leong) 8086 Instructions (page 8)
2
Expressions Example
var alpha, beta, gamma : int (* global variables *) ; alpha := 7; beta := 4; gamma := -3
... ; alpha := (alpha * beta + 5 * gamma) / (alpha – beta)
alpha := 7; beta := 4; gamma := -3; MOV AX, alpha ; AX := alpha
alpha := (alpha * beta + 5 * gamma) / (alpha – beta) IMUL beta ; (DX:AX) := alpha * beta
...
MOV BX, AX ; Save least sig. word in BX
MOV AX, 5 ; AX := 5
In this example we will represent Integers as 16-bit 2’s complement
values and use direct addressing and data declaration directives for IMUL gamma ; (DX:AX) := 5 * gamma
global variables: ADD AX, BX ; AX := 5 * gamma + alpha * beta
MOV IMUL MOV MOV IMUL ADD
alpha DW 0
beta DW 0 AX 0007 001C 001C 0005 FFF1 000D
gamma DW 0 BX 001C 001C 001C 001C Regs shown
in Hex
CX
DX 0000 0000 0000 FFFF FFFF
Computer Architecture (P. Leong) 8086 Instructions (page 9) Computer Architecture (P. Leong) 8086 Instructions (page 10)
Prev MOV SUB CWD IDIV MOV ADD AL, BL ; Add, will set FLAGS.ZF if overflow
AX 000D 000D 000D 000D 0004 0004 JO ov_label ; Jump to ov_label if Overflow
...
BX 001C 0007 0003 0003 0003 alpha
ov_label: ; Handle Overflow condition somehow?
CX
DX FFFF FFFF FFFF 0000 0001
Computer Architecture (P. Leong) 8086 Instructions (page 11) Computer Architecture (P. Leong) 8086 Instructions (page 12)
3
Integer Divide by Zero “LOGICAL” (Bit-level) Instructions
Another erroneous condition is division by zero which causes an Instruction Operation Notes
interrupt to occur (we will cover interrupts later in course).
AND dst, src dst := dst & src Bitwise AND
TEST dst, src dst & src Bitwise AND and set FLAGS
We can guard against this occurring by explicitly checking the divisor OR dst, src dst := dst | src Bitwise OR
before division, e.g. XOR dst, src dst := dst ^ src Bitwise XOR
NOT opr opr := ~ opr Bitwise NOT
Divide by Zero Test
Computer Architecture (P. Leong) 8086 Instructions (page 15) Computer Architecture (P. Leong) 8086 Instructions (page 16)
4
More JUMP Instructions If Statement
Unsigned Comparisons If age<100 then if: CMP age, 100 if: CMP age, 100
statements JL stats JGE endif
JA ZF = 0 and CF = 0 Jump if Above ( > )
end if JMP endif ; statements
JAE CF = 0 Jump if Above or Equal ( >= )
stats: endif:
JB CF = 1 Jump if Below ( < )
; statements
JBE ZF = 1 or CF = 1 Jump if Below or Equal ( <= )
endif:
Miscellaneous
If (age >= 21) and (age <= 65) then if: CMP age, 21
JO OF = 1 Jump if Overflow, ditto for CF, SF & PF statements JL endif
JNO OF = 0 Jump if No Overflow, ditto for ... end if CMP age, 65
JG endif
JCXZ CX = 0 Jump if CX = 0 ; statements
JECXZ ECX = 0 Jump if ECX = 0 endif:
Computer Architecture (P. Leong) 8086 Instructions (page 17) Computer Architecture (P. Leong) 8086 Instructions (page 18)
endif:
Computer Architecture (P. Leong) 8086 Instructions (page 19) Computer Architecture (P. Leong) 8086 Instructions (page 20)
5
Repeat Loop For Loop
loop repeat: for age : 1 .. 99 for: MOV age, 1
statements statements
exit when age > 99 ; statements end for next: CMP age, 99
end loop JG endfor
CMP age, 99
JLE repeat ; statements
endfor:
Computer Architecture (P. Leong) 8086 Instructions (page 21) Computer Architecture (P. Leong) 8086 Instructions (page 22)
Think about
1. How high level language statements are translated to 8086
instructions
2. How integer multiplication could be implemented if the
IMUL instruction didn’t exist
3. How logical operations can be used to set and clear bits