Introduced by the bug of embedded ASM in a VC...

Source: Internet
Author: User
In terms of syntax, we usually think that the following two statements are equivalent:
MoV ECx, offset data_lable // Where data_lable is the data definition label
Lea ECx, data_lable

Furthermore, we will think that the following two sentences are equivalent:
MoV ECx, ebp-8
Lea ECx, [ebp-8]

First, memory addressing is used, and second, register addressing and register indirect addressing are used. what surprised me was that in the second case, VC processing did not make the mov and Lea modes of register addressing and register indirect addressing equivalent. unfortunately, after compiling the statement "mov ECx, ebp-8" in the way of _ ASM, I found in the Disassembly window of VC that it has become such a statement: "mov ECx, EBP ". oh, my "-8" is missing! So far, I have not found the cause of this phenomenon. I can only classify it as a VC bug for the time being.

Does GCC have this problem? For further confirmation, I used GCC to re-write the code: "mov ECx, ebp-8", but the code after rewriting changed from the original sentence to the following two sentences:
Movl % EBP, % ECx
Subl $8, % ECx

I found that in at&t's Assembly syntax, the two-register addressing operations cannot be changed to the value of the Register, that is to say, it cannot be written in the form of "movl % ebp-8, % ECx", and the operation of register indirect addressing can be transformed, such:
Movl-8 (% EBP), % ECx is equivalent to mov ECx in Intel ASM, [ebp-8]
Movl (% EBP, % eax), % ECx is equivalent to mov ECx, [EBP + eax] in Intel ASM.
Movl (% EBP, % eax, 4), % ECx is equivalent to mov ECx, [EBP + eax * 4] in Intel ASM.
Movl-8 (% EBP, % eax, 4), % ECx is equivalent to mov ECx, [EBP + eax * 4-8] in Intel ASM.

From the above statements, it seems that at&t's support for indirect register addressing is not more humane than intel ASM, But I guess at&t adopts this method, to some extent, it may also improve the execution efficiency of micro-commands.

Of course, the sentence "mov ECx, ebp-8" can also be rewritten into the following two sentences:
Subl $8, % EBP
Movl % EBP, % ECx

However, this is generally not the case. It is obvious that EBP is usually used as the base address register in the function to store the first stack address of the function entry point, the change in this value will directly affect the subsequent statement's reference to local variables and function parameters. Therefore, in the execution body after the function header, EBP is usually not allowed to be changed, which is also the principle we should follow when designing our own assembly code.

I don't know why VC discards the immediate number after EBP, which is obviously not ethical. this reminds me of the following sentence: do not try to help users correct errors, but remind users when errors occur, because the program is smart and never understands the true intention of the designer, what we need to do is "provide as detailed logs as possible for exception capture, and notify users of such exceptions in a timely manner. The methods for trying to correct exceptions are both false and stupid ".

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.