Corresponding example of assembly instructions and machine code
Assembly instructions | Machine instructions |
---|---|
mov ax,0123 | B8 23 01 |
mov ax,ds:[0123] | A1 23 01 |
push ds:[0123] | FF 36 23 01 |
It can be seen that in general assembly instructions, the idata (immediate data) in assembly instructions, whether it represents a data or the offset address of a memory unit, will appear in the corresponding machine instruction
Branch instruction
The transfer instructions of 8086CPU are divided into the following categories
- Unconditional transfer instructions (such as: jump)
- Conditional branch instruction
- Loop instructions (eg: loop)
- Process (function)
- Interrupt
Operator offset
The function of the operator offset is to obtain the offset address of the label (relative to the current segment address)
example:
assume cs:codesg
codesg segment
start: mov ax,offset start;相当于mov ax,0 (cs:offset start)
s: mov ax,offset s ;相当于mov ax,3 (cs:offset s)
codesg ends
end start
jmp instruction
jmp is an unconditional transfer. You can modify only the IP, or modify the CS and IP at the same time. At the same time, the jmp instruction needs to give two kinds of information:
- Destination address
- Transfer distance (inter-segment transfer, intra-segment short transfer, intra-segment near transfer)
Jmp instruction to transfer based on displacement
jmp short 标号(转移到标号处执行指令)
The jmp instruction in this format realizes a short transfer within a segment . It can modify the IP in the range of **-128~127**, which means that it can move up to 128 bytes when it moves forward, and it can move backward. Up to 127 bytes over
example:
assume cs:codesg
codesg segment
start: mov ax,0
jmp short s
add ax,1
s: inc ax
codesg ends
end start
In fact jmp short 标号
, the function of the instruction is (IP)=(IP)+8 bit displacement
- 8-bit displacement = the address at the "label"-the address of the first byte after the jmp instruction
- short indicates that the displacement here is an 8-bit displacement
- The range of 8-bit displacement is -128~127, expressed in complement
- 8-bit displacement is calculated by the compiler at compile time
jmp near ptr 标号
This instruction realizes the near transfer within the segment, that is, (IP)=(IP)+16-bit displacement
- 16 displacement = the address at the "label"-the address of the first byte after the jmp instruction
- near ptr indicates that the displacement here is a 16-bit displacement
- The range of 16-bit displacement is -32769~32767, expressed in complement
- The 16-bit displacement is calculated by the compiler at compile time
The jmp instruction where the destination address of the transfer is in the instruction
jmp far ptr 标号
What is achieved is transfer between segments, also known as far transfer
- (CS) = the segment address of the segment where the label is located
- (IP) = offset address in the segment where the label is located
- far ptr indicates that the instruction uses the label segment address and offset address to modify CS and IP
example:
assume cs:codesg
codesg segment
start: mov ax,0
mov bx,0
jmp far ptr s
db 256 dup(0)
s: add ax,1
inc ax
codesg ends
end start
Jmp instruction with transfer address in register
jmp 16位寄存器
Function: (IP) = (16-bit register)
Jmp instruction with transfer address in memory
jmp word ptr 内存单元地址(段内转移)
Function: A word data is stored from the address of the memory unit, which is the destination offset address of the transfer. The address of the memory unit can be given in any format of the addressing mode
example:
mov ax,0123H
mov ds:[0],ax
jmp word ptr ds:[0]
jmp dword ptr 内存单元地址(段内转移)
Function: Two word data are stored from the address of the memory unit. The word at the high address is the destination segment address of the transfer, and the low address is the destination offset address of the transfer. The memory unit address can be given in any format of the addressing mode. Out
- (CS) = (memory unit address + 2)
- (IP) = (memory unit address)
example:
mov ax,0123H
mov ds:[0],ax
mov word ptr ds:[2],0
jmp dword ptr ds:[0]
mov ax,0123H
mov ds:[0],ax
mov word ptr [bx+2],0
jmp dword ptr [bx]
jcxz instruction
The jcxz instruction is a conditional transfer instruction. All conditional transfer instructions are short transfers, and the corresponding machine code contains the transfer displacement instead of the destination address. The range of IP modification is **-128~127**, and the general conditional transfer instructions are all cx registers, such as loop instructions
jcxz 标号 (如果(cx)=0,则转移到标号初执行)
The actual command operation of jcxz:
- When (cx) = 0, (IP) = (IP) + 8-bit shift
- 8-bit displacement = the address at the "label"-the address of the first byte after the jcxz instruction
- The range of 8-bit displacement is -128~127, expressed in complement
- 8-bit displacement is calculated by the compiler at compile time
- When (cx)=0, do nothing (the program is executed downward)
In fact, it can also be understood as
if((cx)==0)
jmp short 标号
loop instruction
The loop instruction is a loop instruction. All loop instructions are short transfers, and the corresponding machine code contains the transfer displacement instead of the destination address. The range of IP modification is **-128~127**
loop 标号
The actual instruction operation of loop:
-
(cx)=(cx)- 1
-
When (cx)! = 0, (IP) = (IP) + 8-bit displacement
- 8-bit displacement = the address at the "label"-the address of the first byte after the loop instruction
- The range of 8-bit displacement is -128~127, expressed in complement
- 8-bit displacement is calculated by the compiler at compile time
-
When (cx)=0, do nothing (the program is executed downward)
In fact, it can also be understood as
(cx)--;
if((cx)!=0)
jmp short 标号