Another relative addressing method for a label address

Source: Internet
Author: User

In an assembler, when accessing data, this is usually the case:

_asm{
...
DATA_LABLE:
   _emit 0x87
   _emit 0xa0
   _emit 0x49
   _emit 0x90
...
   mov ebx, dword ptr [DATA_LABLE]
...
}

In which, when the program is compiled, the data_lable label address in the MOV directive is turned into an absolute address. And sometimes an absolute address this may be a barrier to such a demand: we would like to write the assembly code regardless of which address space is placed in the normal operation, as we write in the high-level language of those functions, function location can be arbitrarily placed, the slightest impact on the function itself. Of course, it must be noted that, while the same functionality is required, the implementation of the compilation and high-level languages is far from being achieved. The function of a high-level language is finally compiled, and its function address is a fixed absolute address, and what we want to do with the assembly is the real "can be arbitrarily placed" binary execution block.

Using the call command, you can realize the relative addressing of the Run-time label address, 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 this:

1. First of all, in the assembly function block or the first part, use the following statement to obtain the Run-time address and compile address correction difference.

  call FUNC_START
FUNC_START:
   pop ebx
   sub ebx, offset FUNC_START
   mov [ebp-xx], ebx

A brief explanation: The call function will push the EIP register into the stack, then use the "Pop Ebp" is the EIP value assigned to EBP, and the EIP is "the Next statement address", where the program runs to "Call Func_start", it means that the label "FUNC_ Start: The starting address of the "pop ebx" directive. On the other hand, the "offset Func_start" in sub directives, at compile time, offset is converted to an absolute address. In this way, a sub operation is used to obtain a correction of the code at the compile and run time for the instruction address. The following sentence: "mov [ebp-xx], ebx", actually just icing on the cake, it saved this value in a custom function local variable space for subsequent statements to facilitate reference.

2. Accordingly, the reference to the label data becomes two sentences:

  mov eax, [ebp-xx]
   mov ebx, dword ptr [DATA_LABLE+eax]

With this type of code in an assembler function, the binary execution block can be placed anywhere without a program error due to incorrect references to the data_lable data address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.