汇编中call printf参数压栈时错误理解
EAX, ECX,EDX,EBX均可以32bit,16bit,8bit訪問,如下所示:
<-------------------EAX------------------------>
|<----------------------|-----------|----------->|
?|<---------AX--------->|
?|<---AH--->|<---AL--->|
測試代碼如下:
1 .section .data 2 output: 3 .asciz "Value is:%x\n" 4 val: 5 .int 0 6 7 .section .text 8 .globl main 9 main: 10 nop 11 movl $0x12345678, %eax 12 movl $0, %ecx 13 movw %ax, %cx 14 pushw %cx 15 pushl $output 16 call printf 17 addl $6, %esp 18 19 movl $1, %eax 20 movl $0, %ebx 21 int $0x80我的電腦是小端格式(MSB存于高位),所以預期的輸出結果是:0x5678,但是我編譯執行后結果是:0x2dec5678,前面多出了2個字節,但是明明我壓棧的時候使用的pushw,原來是printf獲取參數時使用的是4字節長度,所以雖然我壓入的只有2個字節,但是它訪問時以4字節訪問。
將代碼修改:
1 .section .data 2 output: 3 .asciz "Value is:%x\n" 4 val: 5 .int 0 6 7 .section .text 8 .globl main 9 main: 10 nop 11 movl $0x12345678, %eax 12 movl %eax, %ecx 13 pushl %ecx 14 pushl $output 15 call printf 16 addl $8, %esp 17 18 movl $0, %ecx 19 movw %ax, %cx 20 pushl %ecx 21 pushl $output 22 call printf 23 addl $8, %esp 24 25 movl $1, %eax 26 movl $0, %ebx 27 int $0x80預期輸出:
Value is:12345678
Value is:5678
結果輸出:
Value is:12345678
Value is:12
? 起先還以為是將0x12345678的最高字節0x12傳送到cx里面了,覺得相當怪異,明明使用的是movw,要錯也是0x1234,分析了下,原來又是printf作怪,函數調用后,其返回值一般是存儲在eax里面來傳遞的,而第一個printf的輸出:Value is:12345678恰好有0x12,18個字符(含'\n'),所以代碼執行完15行的call后,eax的值已被printf的返回值修改,而不是我預期的0x12345678。所以匯編里面調用庫函數時,要特別注意某些特殊寄存器的特殊用途,特別是這些庫是由其他高級語言通過編譯工具生成的時候。
轉載于:https://www.cnblogs.com/thammer/p/4311621.html
總結
以上是生活随笔為你收集整理的汇编中call printf参数压栈时错误理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tabcontrol动态添加TabPag
- 下一篇: 驱动之LCD的介绍与应用20170209