NASM 32位:printf打印寄存器内容

我是装配新手.我有不同的输出,用于遵循我期望的简单代码.每次调用printf之前,eax的内容都会向右移动一些数字.我究竟做错了什么?谢谢.

码:

;file name : testing.asm
;assemble and link with:
;nasm -f elf testing.asm && gcc -m32 -o testing testing.o
extern  printf      ; the C function, to be called

SECTION .data       ; Data section, initialized variables

    a:  dd  15  ; int a=15
    str:    db "content in eax=%d", 10, 0 

SECTION .text       ; Code section.

    global main     ; the standard gcc entry point
main:               ; the program label for the entry point
    mov     ebp, esp
    mov     eax, [a]    ; put a from store into register

    shr     eax, 1      ; eax content should be 15>>1 = 7
    push    eax     
    push    dword str   ; string to be printed
    call    printf      ; printf(str,content_of_eax)

    shr     eax, 2
    push    eax         ; eax content should be 7>>2 = 1
    push    dword str   
    call    printf      ; printf(str,content_of_eax)    

    shr     eax, 1  
    push    eax         ; eax content should be 1>>1 = 0
    push    dword str   
    call    printf      ; printf(str,content_of_eax)

    mov     esp, ebp    ; takedown stack frame
    mov     eax, 0      ; normal, no error, return value
    ret                 ; return

输出:

content in eax=7
content in eax=4
content in eax=8

预期产出:

content in eax=7
content in eax=1
content in eax=0

解决方法:

printf在eax中返回其结果,原始值丢失.

您应该从[a]重新加载它或使用保存在函数调用(例如esi)之间的寄存器,但是您应该保存它并在返回调用者之前将其恢复.你应该在每次调用后使用add esp,8传递传递给printf的参数,以保持堆栈帧的一致性.

上一篇:php网上支付易宝


下一篇:CentOS7源码编译FFmpeg的坑