In assembler, data access is usually as follows:
_ ASM {
...
Data_lable:
_ Emit 0x87
_ Emit 0xa0
_ Emit 0x49
_ Emit 0x90
...
MoV EBX, dword ptr [data_lable]
...
}
After the program is compiled, the data_lable address in the mov command is converted into an absolute address. sometimes the absolute address may bring obstacles to such a requirement: we hope that the Assembly code we write can run normally no matter which address space we put in, just like the functions in advanced languages, function positions can be placed at will without affecting function usage. of course, it must be pointed out that although the same function is required, the implementation of compilation and advanced languages is far from the same. after a function in advanced language is finally compiled, its function address is also a fixed absolute address, and what we want to implement in assembly is a truly binary execution block that can be placed at will.
By using the call command, you can achieve relative addressing of the number address during the runtime. The general idea is as follows:
_ ASM {
...
Call func_start
Func_start:
Pop EBX
Sub EBX, offset func_start
MoV [EBP-XX], EBX
...
Data_lable:
_ Emit 0x87
_ Emit 0xa0
_ Emit 0x49
_ Emit 0x90
...
MoV eax, [EBP-xx]
MoV EBX, dword ptr [data_lable + eax]
...
}
The steps are as follows:
1. First, use the following statement in the Assembly function block or function header to get the correction difference between the runtime address and the compilation address.
Call func_start
Func_start:
Pop EBX
Sub EBX, offset func_start
MoV [EBP-XX], EBX
Slightly explained: The call function will push the EIP Register into the stack, and then use "Pop EBP" to assign the EIP value to EBP, while EIP indicates "the address of the next statement ", here, when the program runs "Call func_start", it indicates the start address of the "Pop EBX" command starting with "func_start. on the other hand, the "offset func_start" in the sub command will be converted into an absolute address during compilation. in this way, the sub operation is used to obtain the correction value of the instruction address during the compilation and runtime periods. the following sentence: "mov [EBP-XX], EBX" is actually just a icing on the cake. It stores this value in a custom function's local variable space, for reference by subsequent statements.
2. Correspondingly, the reference to the label data is changed to the following two sentences:
MoV eax, [EBP-xx]
MoV EBX, dword ptr [data_lable + eax]
After this type of code in the Assembly function is processed, the binary execution block of this segment can be placed anywhere without program errors caused by incorrect reference to the data_lable data address.