8088 assembly language introduction
Download
Report
Transcript 8088 assembly language introduction
8088 assembly language introduction
Assistant : 蘇建仲(Su Jain-Zhong)
LAB : ISMP LAB
Tel : 62530-58
E-Mail : [email protected]
Basic Knowledge
Data Representation
Base Conversion
Binary Arithmetic
80x86 architecture
Addressing mode
80x86 Registers
IP and Flags
General registers
Pointers (指標) and Indices (索引)
AX (Accumulator,累積器)AH/AL
BX (Base), CX (Counter), DX (Data,
aDdress)BH/BL,CH/CL,DH/DL
SP (Stack P.), BP (Base P.)
SI (Source I.), DI (Destination I.)
Segment registers (節段暫存器)
CS (Code程式段), SS (Stack堆疊段), DS (Data資料
段), ES (Extra額外段)
Program example
StSeg
SEGMENT STACK
DW
64 dup (?)
StSeg
ENDS
;------------------------------------------------------------------DtSeg
SEGMENT
Data1
DB
52H
Data2
DB
29H
Sum
DB
?
DtSeg
ENDS
;------------------------------------------------------------------CdSeg
SEGMENT
Main
PROC
FAR
ASSUME CS:CdSeg,DS:DtSeg,SS:StSeg
push
DS
; Program Initialization
mov
AX,0
; (A).Save DS:0 to Stack
push
AX
mov
AX,DtSeg
; (B).Point DS to DtSeg
mov
DS,AX
mov
AL,Data1
; Program Body
add
AL,Data2
mov
Sum,AL
ret
Main
ENDP
CdSeg
ENDS
END
Main
Program structure
Segmentation
Procedure definition
程序名 PROC FAR/NEAR
……………
程序名 ENDP
Segment Register setting
段名 SEGMENT 參數
……………
段名 SEGMENT
ASSUME CS:CODE,DS:DATA,SS:STACK
Program Entry
END 程序名
Program Structure(cont)
Program Initialization
Store Return-to DOS Address
Point DS to data segment
PUSH DS
MOV AX,0
PUSH AX
MOV AX,資料段
MOV DS,AX
Program body
Instruction
Instruction Format
[label:]
label
[operation] [operands] [; comment]
length 247
1st character: English _ ? $@
operation: Mnemonics
operands: (0/1) 2 operands for 80x86
Types of Operations
Pseudo-Instruction: No Opcode
i.e. Assembler Directive
Machine code: 1-1 to Opcode
Pseudo Instruction
Program structure
SEGMENT ENDS
PROC
ENDP
ASSUME
END
Data structure
DB - 1B
DW – 2B
DD - 4B
DQ – 8B
DT – 10B
EQU
DUP(duplication)
Pseudo Instruction(cont)
MACRO
名稱 MACRO 參數
………….
ENDM
Conditional assembling
IF condition
…………
ELSE
…………
ENDIF
General Instructions
MOV destination, source
Destination source
S
,S
AX
DS
,
O
V
V
M
M
O
general registers
(AX,BX,CX,DX,
SP,BP,SI,DI)
MOV VAR,AX
MOV AX,VAR
AX
,0 MO
10 V
00
11
1B
MOV CS,any
(x)
AR
,V S
DS R,D
OV A
M VV
MO
AX
segment registers
(CS,SS,DS,ES)
immediate values
MOV V1,V2
(x)
memory variables
VA M
R, OV
01
12
3H
XCHG destination,source
Destination source (Error:XCHG SEG,SEG or MEN,MEN)
Address Loading
LEA (Effective Address)
LEA
BX, VAR1[SI]
; BX=0130
SI, VAR1
; DS=31AD
LDS
LDS
; SI=012E
LES
LES
SI, VAR1
; ES=31AD
; SI=012E
Stack
CPU
Memory
code
segment
SP <- SP-2
[SP] <- VAR
PUSH VAR
data bus
SP
0100
POP VAR
[SP] <- VAR
SP <- SP+2
0100
0101
0102
VAR
...
...
E2
3A
...
SP-2
SP
SP+2
data
stack
segment segment
D3
84
Push/Pop
PUSH
PUSH VAR
; SP=00FE
; [SP]=[00FE:0100]=84D3
POP
POP VAR
; [VAR]=3AE2
; SP=0102
PUSHA
=PUSH AX,BX,CX,DX,SP,BP,SI,DI
POPA
=POP DI,SI,BP,SP,DX,CX,BX,AX
Flags
8
7
6
NT
IO PC OF DF
IF
TF
SF
ZF
AF
PF
CF
(保護模式權限 )
Interrupt (中斷 )
Trap (單步追蹤 )
Sign (負號 )
Zero (零 )
Carry (進借位 )
NZ/ZR (0/1)
Auxiliary carry
NA/AC (0/1)
(輔助進位 )
Parity (同位 )
PL/NG (0/1)
EV/OD (0/1)
NC/CY (0/1)
Nesting task
(保護模式工作 )
Privilege
UP/DN (0/1)
9
Direction (方向 )
A
0
1
2
3
4
5
B
C
NV/OV (0/1)
D
Overflow (溢位 )
E
F
MOVs of Flags
PUSHF: Flags (push) Stack
POPF: Flags (pop) Stack
LAHF: AH (load) Flags
SAHF: AH (save) Flags
Unconditional Jump
JMP
Label
JMP
JMP
SHORT Label
FAR Label
; near jump
(the same segment, push IP)
; short jump (128 bytes)
; far jump
(in different segments, push CS:IP)
JMP L1
JMP L2
L1: ....
L2: ....
Conditional Jump
Comparison Operators
CMP
J
AX, BX
Label
}
IF (AXBX) GOTO Label
Unsigned Number
A (Above), B (Below), E (Equal to)
Signed Number
G (Greater than), L (Less than), E
Flag Testing
overflow
=1
=0
OF
JO
JNO
sign
SF
JS
JNS
zero
parity
carry
ZF
PF
CF
JZ,JE
JP,JPE
JC
JNZ,JNE JNP,JPO JNC
Unconditional Loop
Unconditional Loop
L1:
MOV
…
…
LOOP
CX, Count
L1
Conditional Jump: JCXZ (Jump if CX = Zero)
L1:
L2:
MOV
JCXZ
…
…
LOOP
…
CX, Count
L2
L1
Conditional Loop
LOOPE/LOOPZ
find the first “non-blank” element
jump out of loop when
finished: CX = 0
found: ZF = 1
MOV
CX, 10
L1:
CMP
AL, BL
LOOPE (LOOPZ) L1
General Arithmetic Operations
Unsigned number
Singed number
Operations
8-bit
16-bit
8-bit
16-bit
Addition
ADD AL,BL
ALAL+BL
8
8 8
ADD AX,BX
AXAX+BX
16 16 16
ADD AL,BL
ALAL+BL
8
8 8
ADD AX,BX
AXAX+BX
16 16 16
Subtraction
SUB AL,BL
ALAL-BL
8
8 8
SUB AX,BX
AXAX-BX
16 16 16
SUB AL,BL
ALAL-BL
8
8 8
SUB AX,BX
AXAX-BX
16 16 16
Multiplication
MUL V8
AXAL*V8
16 8
8
MUL V16
DX:AXAX*V16
32 16 16
IMUL V8
AXAL*V8
16 8
8
IMUL V16
DX:AXAX*V16
32 16 16
Division
Sign
Extension
DIV V8
DIV V16
IDIV V8
IDIV V16
AL…AHAX*V8 AX…DX(DX:AX)*V16 AL…AHAX*V8 AX…DX(DX:AX)*V16
8…8 16
8
16…16 32
16
8…8 16
8
16…16 32
16
MOV AH,0
MOV DX,0
CBW (Convert
Byte to Word)
AHAL(7)
CWD (Convert Word to
Double word)
DXAX(15)
Simple Arithmetic
increment
INC VAR
decrement
DEC VAR
; VAR VAR + 1
; VAR VAR - 1
negation (2’s complement)
NEG VAR
; VAR - VAR
Overflow
Addition/Subtraction
Overflow Flags [ref. Unit 1-11]
Unsigned number: CF = carry of MSB
Signed number:
OF = (carry of MSBcarry of 2nd MSB)
Instructions: the same
Multiplication: never overflow
Dividend
Division: Divisor Quotient ... Remainder
overflow when
divisor = 0 (INT 03H)
divisor is too small (INT 04H)
CMP
JAE
DIV
AH,Divisor8
Overflow
Divisor8
Multi-Word Arithmetic Operations
ADC destination, source
destination destination +source +CF
SBB destination, source
destination destination - source -CF
Var1
DD
Var2
DD
Result DD
MOV
ADD (SUB)
MOV
MOV
ADC (SBB)
MOV
7ADBDDAAH
3678CC00H
?
AX,WORD PTR [Var1]
AX,WORD PTR [Var2]
WORD PTR[Result],AX
AX,WORD PTR [Var1+2]
AX,WORD PTR [Var2+2]
WORD PTR[Result+2],AX
CF
+)
7ADB
DDAA
3678
CC00
????
????
Bit-wise Operations
Bit-wise Operations
AND AL,BL
OR AL,BL
NOT AL
XOR AL,BL
0101 0110
0011 1100
0001 0100
0111 1110
1010 1001
0110 1010
=
=
=
=
=
=
AL
BL
AL
AL
AL
AL
Shifting and Rotation
Shifting Count
Logical
Shifting Shifting
SHL
ROL
ROR
CF
CF
RCL
Arithmetic
MOV CL, 4
SHL AL, CL
Right
Rotation
Left
Rotation
through
CF
Count = 1: SHL AL,1
Count > 1
Shifting
Rotation
CF
SHR
0
0
CF
SAL=SHL
CF
CF
SAR
0
S
CF
RCR
CF
Flags and Testing
Flag Testing
Carry CF
Set (=1)
Clear (=0)
Complement
Direction DF Interrupt IF
(used in multiplebyte arithmetic)
(used in string
instructions)
(used in interrupt
routine)
STC
CLC
CMC
STD
CLD
STI
CLI
Comparison and Testing
destination is not stored
used with J€
CMP destination, source
: destination – source (SUB)
TEST destination, source
: destination AND source (AND)
Data Transformation
ASCII (American
Standard Code for
Information
Interchange)
'5'
'7'
00110101
00110111
OR AX, 3030H
or XOR AX, 3030H
Unpacked decimal
00000101
0111 DAA,DAS
?
?
SHL AX, 4
SHR AL, 4
Packed Decimal, BCD
0101
(Binary Coded Decimal,
二進制編碼十進位 )
AND AX, 0F0FH
or XOR AX, 3030H
AAA, AAS,
00000111
AAM, AAD
SHL AL, 4
SHR AX,4
AAD
Binary
00111001
AAM
ADD, SUB,
MUL, DIV
BCD Arithmetic
Addition: Add-6 adjustment
+)
57
39
90
+6)
96
0101 0111
0011 1001
1001 0000
0110
1001 0110
ADD AL,BL
DAA
=AL
Subtraction: Subtract-6 adjustment
57 0101 0111
-) 39 0011 1001
1E 0001 1110
0110
-6)
18 0001 1000
=AL
=BL
=AL
=BL
SUB AL,BL
DAS
=AL
No adjustment for MUL/DIV
Arithmetic for Unpacked Decimal
Addition: Add-6 adjustment
57 0000 0101 0000 0111 =AL
+) 39 0000 0011 0000 1001 =BL
ADD AL,BL
8? 0000 1000 0001 0000
(AF=1) 0110
AAA
+6)
96 0000 1001 0000 0110 =AL
Subtraction: Subtract-6 adjustment
57 0000 0101 0000 0111 =AL
-) 39 0000 0011 0000 1001 =BL
SUB AL,BL
1E 0000 0001 1111 1110
(AF=1) 0110
AAS
-6)
96 0000 0001 0000 1000 =AL
Arithmetic for Unpacked Decimal
(cont.)
Multiplication: Divide-10 adjustment
)
4
9
36
(binary)
10)
36
(unpacked)
0000 0100 =AL
0000 1001 =BL
0010 0100
MUL BL
(AF=1) 1010
AAM
0000 0011 0000 0110 =AX
Division: Multiply-10 adjustment
36
(unpacked)
10)
(AF=1) 1010
36
(binary)
)
0000 0011 0000 0110 =AX
4
9
AAD
0010 0100
0000 0100 =BL DIV BL
0000 1001 =AL