chapter four–80x86 instruction set principles of microcomputers 2016年1月12日 2016年1月12日...

25
2022年6年10年 年年年 Chapter Four–80x86 Instruction Set Principles of Microcomputers Chapter four 80x86 Instruction Set 1

Upload: dwayne-francis

Post on 19-Jan-2016

255 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 1

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Chapter four

80x86 Instruction Set( 1 )

Page 2: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 2

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Instruction Encodings

The 80x86 uses a binary encoding for each machine operation. While it is important to have a general understanding of how the 80x86 encodes instructions, it is not important that you memorize the encodings for all the instructions in the instruction set. If you were to write an assembler or disassembler (debugger), you would definitely need to know the exact encodings. For general assembly language programming, however, you won’t need to know the exact encodings.However, as you become more experienced with assembly language you will probably want to study the encodings of the 80x86 instruction set. Certainly you should beaware of such terms as opcode, mod-reg-r/m byte, displacement value, and so on.

Page 3: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 3

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Machine Language Codes

• Each instruction is coded as one or more bytes

• The first byte is generally an OpCode– A numeric code representing a particular instru

ction or class of instructions

• Additional bytes may affect the action of the instruction or provide information about the data acted upon by the instruction

Page 4: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 4

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Variable Format Instructions

• The meaning of bits in an instruction varies depending on the instruction

• The number of bytes in an instruction varies depending on the needs of the instruction

• Basic 8086 instruction format

Opcode d w mod reg r/m low disp/data hi disp/data

low data hi data

Page 5: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 5

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Opcode• 8086 Opcodes are 6, 7, or 8 bits

– d(irection) bit - used for instructions that code a register as one of the operands

• 1 = register is destination, 0 = register is source

– w(idth) bit - used to distinguish byte(0)/word(1) operands

• 7-bit opcodes do not use d, 8-bit opcodes do not use either d or w

Opcode d w

Page 6: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 6

Chapter Four–80x86 Instruction Set Principles of Microcomputers

mod/reg/r/m• The second byte of some instructions is divi

ded into three fields

• The reg field, together with the w bit (in the opcode), specifies a register operand– If there is only one register operand, d specifies

whether it is the source or destination– If there are two registers, reg specifies the desti

nation register

mod reg r/m

Page 7: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 7

Chapter Four–80x86 Instruction Set Principles of Microcomputers

regreg w=1 w=0000 AX AL001 CX CL010 DX DL011 BX BL100 SP AH101 BP CH110 SI DH111 DI BH

• The reg field contains a number between 0 and 7.

• Depending on the w-bit setting, this selects one of 8 registers as an operand used by the instruction

Page 8: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 8

Chapter Four–80x86 Instruction Set Principles of Microcomputers

register/memory

• Interpretation depends on mod field contents– mod = 11 r/m=(same code as reg)

• second operand is the specified register

– mod = 00 r/m=110 • second argument is the address specified in the the n

ext two bytes of the instruction, direct near addressing - DS:offset (Unless segment override is in effect)

Page 9: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 9

Chapter Four–80x86 Instruction Set Principles of Microcomputers

More mod/r/m

• All other combinations specify indirect addressing– Target Address = [BX/

BP]+[SI/DI]+disp

– mod specifies the size of the displacement which is coded in the next one or two bytes of the instruction

mod r/m mode00 110 direct00 --- no disp01 all byte disp

10 all word disp

11 all register(see next table)

Page 10: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 10

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Indirect Address Modes

r/m seg addr000 DS BX+SI001 DS BX+DI010 SS BP+SI011 SS BP+DI100 DS SI101 DS DI110 SS BP111 DS BX

• if mod = 01 or 10– disp is added

• if mod = 00– no displacement

• Exception: mod = 00 and r/m = 110– direct addressing using

a 16-bit offset and DS register

Page 11: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 11

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Immediate Operands

• Data is stored in the instruction in the byte or bytes immediately following the address information

• If bytes 3 or 4 contain a displacement the data follows the displacement info– data may be a single byte or a word

Page 12: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 12

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Disclaimer

• Not all instructions conform to the formats described so far! The 8086 instruction set is very complex. Some opcodes are only 4-5 bits in length, and others are 16-bits. We have hit only the highlights here.

Page 13: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 13

Chapter Four–80x86 Instruction Set Principles of Microcomputers

MOV

mov reg,reg 2 100010dw modregr/m

mov reg,mem 3-4 100010dw modregr/m disp(1-2)

mov reg,imm 2-3 1011wreg data(1-2)

mov acc,mem 3 1010000w disp(2)

mov mem,acc 3 1010001w disp(2)

mov mem,imm 4-6 1100011w modregr/m disp(1-2) data(1-2)

Page 14: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 14

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Sample Machine Codes0000 000A a dw 100002 00 b db ? .code0000 8B DF mov bx,di0002 8A F9 mov bh,cl0004 8B 1E 0000 R mov bx,a0008 8A 26 0002 R mov ah,b000C 8B 12 mov dx,[si][bp]000E A0 0002 R mov al,b0011 8A 26 0002 R mov ah,b0015 BB 0003 mov bx,30018 B1 03 mov cl,3001A C7 06 0000 R 0064 mov a,1000020 C6 06 0002 R FF mov b,255

Page 15: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 15

Chapter Four–80x86 Instruction Set Principles of Microcomputers

PC-Relative Addressing

• The jump instructions use an addressing mode called PC-relative or self-relative

• The machine code contains a displacement that is added to the current IP to cause a jump to occur

• The assembler must calculate this displacement

Page 16: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 16

Chapter Four–80x86 Instruction Set Principles of Microcomputers

JMP• Direct intrasegment short

– 11101011 disp

• Direct intrasegment (near)– 11101001 disp-low disp-high

• Indirect intrasegment– 11111111 mod 100 r/m

• Indirect intersegment– 11111111 mod 101 r/m

• Direct intersegment (far)– 11101010 offset-low offset-high seg-low seg-high

Page 17: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 17

Chapter Four–80x86 Instruction Set Principles of Microcomputers

JMP Example

003C LP:...

0054 JMP LP...

010C JMP ELP...

0406 ELP:0406 JMP XYZ

• disp=3C-56=E6– 11101011 11100110

• disp=406-10E=2F8 *• disp=406-10F=2F7

– 11101001 11110111 00000010

• Assume XYZ is a far label at 01C5:0094– EA 94 00 C5 01

Page 18: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 18

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Conditional Jumps

• These are always 2 bytes (8086-80286)– opcode disp– JA: 01110111 disp– JE: 01110100 disp– Jwhatever: 0111xxxx disp

• 80386 and later processors allow near jumps– opcode is 1000xxxx disp-lo disp-hi

Page 19: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 19

Chapter Four–80x86 Instruction Set Principles of Microcomputers

例:写出下面指令的机器语言编码。 MOV [ BX + DI - 6 ], CL

=88H=88H、、 49H49H、、 FAHFAH

100010DW mod r/mreg disp8

100010 0 0 1111101001 001001

1000 1000 0100 1001 1111 1010

Page 20: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 20

Chapter Four–80x86 Instruction Set Principles of Microcomputers

000000DW mod r/mreg Disp-L

000000 1 1 0010011010 001000

Disp-H

00001000

03H 81H 26H 08H03H 81H 26H 08H

例: 写出下面指令的机器语言编码。 ADD AX, [ BX + DI - 0826 ]

0000 0011 1000 0001 0010 0110 0000 1000

Page 21: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 21

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Addressing Modes on the x86

This is one of the most important issues for your This is one of the most important issues for your studying of the assembly language programmingstudying of the assembly language programming

““ 寻址方式”是你学习汇编语言指令及程序设计的基础!!!寻址方式”是你学习汇编语言指令及程序设计的基础!!!

Page 22: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 22

Chapter Four–80x86 Instruction Set Principles of Microcomputers

Addressing Modes on the x86

• The x86 instructions use THREE different operand types:

• Constants( 立即数寻址方式) mov al, 30h• registers (寄存器寻址方式) mov ah,al• memory addressing (存储器寻址方式) mov [2000h],al

Page 23: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 23

Chapter Four–80x86 Instruction Set Principles of Microcomputers

8086/8088 的寻址方式所谓寻址方式就是寻找操作数存放地址(位置)的

方法。在 8086/8088 系统中操作数存放的位置用以下 3种:

⑴ 操作数包含在指令字节中。即指令格式中操作数部分就是操作数本身。这种操作数叫立即数操作数。

⑵ 操作数存放在 CPU 的某个内部寄存器中。这种操作数叫寄存器操作数。

⑶ 操作数在内存的数据区中。这种操作数叫存储器操作数。

Page 24: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 24

Chapter Four–80x86 Instruction Set Principles of Microcomputers

在 8086/8088 系统中,操作数又可分为两大类:数据操作数和地址操作数。因此,寻址方式也分为数据寻址方式和地址寻址方式两大类。

1. 数据的寻址方式1 )立即数寻址方式例如: MOV AL, 80H MOV AX, 1234H

AL

操作码字节

80H

存储器

指令码

操作码字节

12H

存储器AL

指令码

AH

34H

12

34

80H

80H

12 34

Page 25: Chapter Four–80x86 Instruction Set Principles of Microcomputers 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 2016年1月12日 1 Chapter four

2023年4月21日 星期五 25

Chapter Four–80x86 Instruction Set Principles of Microcomputers

2 )寄存器寻址方式例如: MOV AX, CX

89

C1

存储器AX

CX 指令码(包括操作数)