gpt4 book ai didi

程序集 EAX 寄存器行为异常

转载 作者:行者123 更新时间:2023-12-02 21:49:18 28 4
gpt4 key购买 nike

我对汇编相当陌生,并尝试从标准输入读取值(从 C 调用 scanf 函数)并将其打印回标准输出(使用 printf)。


.text
readstr: .asciz "%d" #string used for calling scanf
printstr: .asciz "%d\n" #string used for calling printf

.global main

main: movl %esp, %ebp #initialize base pointer
call inout

pushl %eax
pushl $printstr
call printf

pushl $0 #push exit code to stack
call exit #exit with the exit code

inout: pushl %ebp
movl %esp, %ebp

subl $4, %esp
leal -4(%ebp), %eax
pushl %eax
pushl $readstr
call scanf

movl %ebp, %esp
popl %ebp
ret

预期输出与输入的数字相同,但输出始终为

1

注意:在64位suse linux企业桌面上编译,使用gcc -m32 -o inout inout.s

这里出了什么问题?

最佳答案

调用 scanf() 后,%eax 包含函数的返回值,即 the number of input items assigned 。在您的情况下,这始终是 1,因为总是有一个输入项。

在从 inout() 返回之前,您需要将 -4(%ebp) 处的值放入 %eax 中。

关于程序集 EAX 寄存器行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18962292/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com