Assembly language reading notes [transfer instructions]

Time:2019-12-16

First, reference

Assembly language – Wang Shuang

Two, text

The instructions that can modify IP, or both CS and IP are collectively referred to as transfer instructions
A transfer instruction is an instruction that can control the CPU to execute code somewhere in memory

2.2 call and RET instructions

Call and RET instructions are both transfer instructions, which modify IP, or modify CS and IP at the same time. They are often used to implement subprogram design.

2.2.1 RET and retf

The RET instruction uses the data in the stack to modify the content of IP and realize the near transfer;
The retf instruction uses the data in the stack to modify the contents of CS and IP so as to realize the remote transfer;

The CPU executes the RET instruction, which is equivalent to:
pop IP

The actual change is:
(IP) = ( (ss)*16 + (sp) )
(sp) = (sp) + 2

The CPU executes the retf instruction, which is equivalent to:
pop IP
pop CS

The actual change is:

(IP) = ( (ss)*16 + (sp) )
(sp) = (sp) + 2
(CS) = ( (ss)*16 + (sp) )
(sp) = (sp) + 2

2.2.2 call

When the CPU executes the call instruction, it performs two steps:
(1) push the current IP or CS and IP into the stack;
(2) transfer

Call instruction cannot implement short transfer

2.2.3 call and ret

2.3 modular programming

Call and RET instruction support the modularization design in assembly language programming. In actual programming, the modularization of program is essential,
Because of the complexity of the real problems, the analysis of the real problems needs to be transformed into interrelated and different level subproblems, which is the way to be solved. With call and RET instructions, we can use simple methods to realize multiple interrelated and functional independent subprograms to solve a complex problem.

2.3.2 register conflict

Registers used in subroutines are likely to be used in the main program, which causes conflicts in the use of registers

How to avoid this kind of conflict? There may be two options:
(1) when writing subroutines, pay attention to check whether there are any registers that will generate conflicts in subroutines. If so, the caller uses other registers;
(2) when writing subroutines, do not use registers that may cause conflicts

Analyze the feasibility of two schemes:
(1) this will cause great trouble to the programming of calling subroutines, because it is necessary to check carefully whether there are registers that will generate conflicts in subroutines. For example, if the registers BX and CX are used in subroutines, the main program (caller) cannot use BX and CX registers
(2) this scheme can not be implemented, because when writing subroutines, it is impossible to know the future call situation

None of the above is feasible. We hope that:
(1) when writing a program that calls a subroutine, you don’t need to care about which registers the subroutine uses;
(2) when writing subroutines, it is not necessary to care about which registers the caller uses;
(3) no register conflict

The simple way to solve the problem is to save the contents of all the registers used in the subprogram at the beginning of the subprogram, and recover them before the subprogram returns. You can use the stack to save the contents of the registers

Assembly language reading notes [transfer instructions]