Chap#4 Pentium Instructions,
Part 2 : Arithmetic, Logical, Bit Manipulation, Program Transfer, and Processor
Control Instructions
Objectives
:
-
The
arithmetic, logical, and bit manipulation instruction
-
The
program transfer instructions
-
The
processor control instructions
-
How
an assembler generates machine code
-
The
special properties of relocatable code
4.1
Introduction
-
Pentium's instruction
set. Part II
4.2
Arithmetic Instructions
-
1) ADD
D, S (Add byte, W, DW)
e.g. ADD BX,AX
-
2) ADC
D, S (Add byte, W, DW with carry)
e.g. ADC AX,
BX (P100)
-
3) INC
D, S (Increment byte, W, DW by 1)
e.g. INC
AX
-
4) SUB
D, S (Subtract byte, W, DW)
e.g. SUB
AL, BL
-
5) SBB
D, S (Subtrac byte, W, DW with Borrow)
e.g. SBB BH, CH
-
6) DEC
D, S (Decrement byte, W, DW by 1)
e.g. DEC BYTE
PTR [200H] (P101)
-
7) CMP
D, S (Compare byte, W, DW)
e.g. CMP AX,
3B2EH
-
8) CMPXCHG
D, S (Compare and Exchange)
e.g. CMPXCHG
BL, CL (P103)
-
9) CMPXCHG8B
D (Compare and Exchange 8 Bytes)
e.g. MOV EDX, 12345678H
MOV EAX, 9ABCDEF0H
MOV ECX, 01020304H
MOV EBX, 05060708H
CMPXCHG8B VAL64BIT
- - -
VAL64BIT
DQ 123456789ABCDEF0H
- - -
-
10) MUL
Sourse (Multiply Byte, W, DW Unsigned)
e.g.
MUL CL ==> CL * AL = AX
MUL BX ==> BX * AX = DX:AX
MUL
EBX ==> EBX * EAX = EDX:EAX
-
11) IMUL Sourse
(Integer Multiply Byte, W, DW)
e.g.1)
IMUL CL ;if CL=80H, AL=20, then
AX=F000H(2's complement)
e.g.2)
IMUL CX, -12 ; CX=CX * -12
IMUL EAX, EBX ; EAX=EAX * EBX
e.g.3)
IMUL AX, BX, 5
; AX=BX * 5
IMUL EBX, [SI], -23 ; EBX= [SI] * -23
-
12) DIV
Sourse (Divide Byte, W, DW Unsigned)
e.g.
DIV CL ==> AX/CL =AL ---(AH: remainder)
DIV CX ==> DX:AX/CX =AX ---(DX: remainder)
DIV EBX ==> EDX:EAX/EBX = EAX ---(EDX: remainder)
-
13) IDIV Sourse
(Integer Divide Byte, W, DW)
Random number generator :
RANDHI DD 12345678H
RANDLO DD 9ABCDEF0H
GTERM DD 5A5A5A5AH
- - -
MOV EDX, RANDHI
MOV EAX, RANDLO
MOV EBX, GTERM
IDIV EBX
MOV RANDHI, EDX
MOV RANDLO, EAX
- - -
-
14) NEG
D (Negate Byte, W, DW)
NEG AX ==> AX = 0 - AX (2'S Complement)
-
15) CBW
(Convert Byte to Word)
AL Convert to
AX
before IDIV or IMUL
-
16) CWD/CWDE
(Convert Word to DW)
CWD ---AX Convert to DX:AX
CWDE --- AX
Convert to EAX
Preparing for IDIV or IMUL
-
17) CDQ
(Convert DW to Quadword)
CWQ --- EAX
Convert to EDX:EAX
Preparing for IDIV or IMUL
-
18) DAA
(Decimal Adjust for Addition)
e.g. ADD AL, BL ;AL=15H, BL=25H
;AL=3AH
DAA
;AL=40H
-
19) DAS
(Decimal Adjust for Subtraction)
e.g. SUB AL,CL ;AL=10H,
CL=02H
;AL=0EH
DAS
;AL=08H
-
20) AAA
(ASCII Adjust for Addition)
e.g. ADD AL, BL ;AL=34H,
BL=38H
;AL=6CH
AAA
;AX=0102H
OR AX,3030H ;AX=3132H
-
21) AAS
(ASCII Adjust for Subtraction)
;ASC1=38H, ASC2=34H
MOV AL,ASC1 ;AL=38H
SUB AL,ASC2 ;AL=04H
AAS
;AX=0004
OR AL,30H ;AL=34H
- - -
MOV AL,ASC2 ;AL=34H
SUB AL,ASC1 ;AL=FCH
AAS
;AX=FF06H
-
22) AAM
(ASCII Adjust for Multiply)
; AL=35H, CL=39H
AND CL,0FH
AND AL,0FH
MUL CL
AAM
;AX=0405H
OR AX,3030H
;AX=3435H
-
23) AAD
(ASCII Adjust for Division)
;AX=3238H, CL=37H
AND CL, 0FH
AND AX,0F0FH
AAD
;AX=001CH
DIV CL
;AX=0004H
-
24) XADD
D, S (Exchange and ADD Byte, W, DW)
;AX=2000H, BX=3000H
XADD AX, BX ;AX=5000H,
BX=2000H
4.3
Logical Instructions
-
1)
NOT Dest.
("NOT" Byte, W, DW)
-
2)
AND D, S ("AND"
Byte, W, DW) (P112)
-
3)
OR D, S ("OR"
Byte, W, DW) (P113)
-
4)
XOR D, S
("Exclusive-OR" Byte, W, DW) (P114)
-
5)
TEST D, S
("TEST" Byte, W, DW)
e.g. AND DX, 2000H ;DX change, Set
Flag
e.g. TEST DX, 2000H ;DX not-change,
Set Flag
6)
BSF/BSR D, S
(Bit Scan Forward/Reverse) (P115)
-
7)
BT/BTC/BTS/BTR D, S
(Bit Test and Complement/Set/Reset) (P116)
* Bit are 0 ~ 31
-
8)
SETcc Dest.
(Set Byte on Condition) (P117)
e.g. SETNZ AL ; if true then
AL = 01H
: if false then AL = 00H
4.4
Bit Manipulation Instructions
-
1)
SHL/SAL D, S
(Shift Logical/Arithmetic Left Byte, W, DW)
-
2)
SHR D, Count
(Shift Logical Right Byte, W, DW) (P118)
-
3)
SAR D, Count
(Shift Arithmetic Right Byte, W, DW) (P119)
-
4)
SHLD/SHRD D, S, Count
(Shift Left/Right Double Precision) (P120)
-
5)
ROL D, Count
(Rotate Left Byte, W, DW) (P121)
-
6)
ROR D, Count
(Rotate Right Byte, W, DW) (P122)
-
7)
RCL D, Count
(Rotate Left Through Carry Byte, W, DW) (P123)
-
8)
RCR D, Count
(Rotate Right Through Carry Byte, W, DW)
4.5
Program Transfer Instructions
-
1)
JMP Target
(Unconditional Jump to Target) (P126)
-
2)
Conditional Jumps (P127)
-
3)
LOOP Short-Label (Loop) (P129)
-
4)
LOOPE/LOOPZ Short-Label
(Loop if Equal, Loop if Zero)
-
5)
LOOPNE/LOOPNZ Short-Label
(Loop if not Equal, Loop if not Zero)
-
6)
JCXZ/JECXZ Short-Label
(Jump if CX/ECX = 0) (P130)
-
7)
CALL Procedure-Name
(Call Procedure) (P131) (P132)
-
8)
RET Optional-Pop-Value
(Return from Procedure) (P133)
-
9)
INT Interrupt-Type
(Interrupt) (P134)
-
10)
IRET/IRETD
(Interrupt Return) (P135)
-
11)
INTO
(Interrupt on Overflow)
-
12)
BOUND Index, Range
(Check Array Index Against Bounds)
-
13)
ENTER/LEAVE
(Enter/Leave a Procedure) (P137)
-
@
Assembler Directive PROC and ENDP
(P138)
4.6
Processor Control Instructions
-
1)
CLC (Clear Carry Flag)
-
2)
STC (Set Carry Flag)
-
3)
CMC (Complement Carry Flag)
-
4)
CLD (Clear Direction Flag)
-
5)
STD (Set Direction Flag)
-
6)
CLI (Clear Intrrupt-Enable Flag)
-
7)
STI (Set Intrrupt-Enable Flag)
-
8)
HLT (Halt Until Interrpt or Reset)
-
9)
NOP (No Operation)
-
10)
LOCK ( Lock Bus During Next Instruction)
4.7
How an Assembler Generates Machine Code
-
MOVSB
: m/c code : 1010 010w (0)
MOVSW : m/c code : 1010 010w (1)
-
MUL
CX : m/c code : 1111 0111 1110 0r/w(=001)
MUL SI : m/c code : 1111 0111 1110 0r/w(=110)
-
Refer
to P452 ~ P458
4.8
The Beauty of Relocatable Code
-
A
relocatable program is a program that can be loaded into memory at any
address and the executed normally.
4.9
Summary