Error understanding of Call printf parameter stack in assembler

Source: Internet
Author: User

EAX, ECX,EDX,EBX can be 32bit,16bit,8bit accessed as follows:

<-------------------EAX------------------------>
|<----------------------|-----------|----------->|
|<---------AX--------->|
|<---AH--->|<---AL--->|

The test code is as follows:

1 . Section. Data2 Output:3. Asciz"Value is:%x\n"4 Val:5.int 06     7 . Section. Text8 . Globl Main9 Main:Ten     NOP One movl $0x12345678,%eax AMOVL $0,%ecx - MOVW%ax,%cx -     PUSHW%cx the PUSHL $output -     Pagerprintf -Addl $6,%esp -      +MOVL $1,%eax -MOVL $0,%EBX +     int$0x80

My computer is a small-format (MSB in the high-level), so the expected output is: 0x5678, but I compiled the result is: 0x2dec5678, 2 more bytes in front, but clearly I press the stack when the use of PUSHW, It turns out that printf was using 4-byte lengths to get the parameters, so although I was only 2 bytes in the press, it was accessed with 4 bytes.

To modify the code:

1 . Section. Data2 Output:3. Asciz"Value is:%x\n"4 Val:5.int 06     7 . Section. Text8 . Globl Main9 Main:Ten     NOP One movl $0x12345678,%eax A movl%eax,%ecx - PUSHL%ecx - PUSHL $output the     Pagerprintf -Addl $8,%esp -  -MOVL $0,%ecx + MOVW%ax,%cx - PUSHL%ecx + PUSHL $output A     Pagerprintf atAddl $8,%esp -      -MOVL $1,%eax -MOVL $0,%EBX -     int$0x80

Expected output:

Value is:12345678
Value is:5678

Result output:

Value is:12345678
Value Is:12

At first thought is the 0x12345678 of the highest byte 0x12 transferred to the CX inside, feel quite strange, obviously use is MOVW, to wrong is also 0x1234, analysis, the original is printf mischief, function call, its return value is usually stored in the eax inside to pass, And the first printf output: value is:12345678 has exactly 0x12,18 characters (with ' \ n '), so after the code executes 15 lines of call, the value of EAX has been modified by the return value of printf, not the 0x12345678 I expected. So when you call library functions in the Assembly, pay special attention to the special purpose of some special registers, especially when these libraries are generated by other high-level languages through the compilation tool.

Error understanding of Call printf parameter stack in assembler

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.