gpt4 book ai didi

assembly - 从标准输入读取一个数字到寄存器中?加载 scanf 结果

转载 作者:行者123 更新时间:2023-12-02 22:14:16 25 4
gpt4 key购买 nike

我目前正忙于组装,遇到了以下问题:

我正在尝试获取已输入到 eax 寄存器中的数字。首先,我呈现一个要求输入的字符串,然后有人必须输入一个数字。

我使用了以下代码,但我不理解它的每一个部分。请注意代码中的注释。

我知道现在这个数字绝对没有发生任何事情,除了它已经被移动到 eax 中。我真正想知道的是为什么我必须使用 leal:为什么以及它有什么作用?为什么我需要将 eax 推回堆栈?

.text
string1: .asciz "Please enter a number\n"
input: .asciz "%d"

.global main
main:
# Editor's note: this code is broken / unsafe; missing push %ebp here
movl %esp, %ebp

push $string1 # prompt string
call printf #print the string
# no add $4, %esp here: 4 bytes still allocated on the stack

leal -4(%ebp), %eax # ????
pushl %eax # the thing you pushed in eax is now pushed on the stack?
pushl $input #the number

call scanf

popl %eax
popl %eax # the number that has been entered is now in eax

call end

end:
push $0
call exit

最佳答案

您正在调用函数,因此您在堆栈上将参数传递给它们。一个整数在 eax 中返回给您,其余部分再次通过堆栈上的输入输出指针参数返回。查看x86 calling conventions .

编辑0:

leal 指令将某个临时变量的有效地址(即 scanf 放置整数值的位置)存储到 eax,然后将其传递给堆栈上的 scanf。看看这里:What's the purpose of the LEA instruction?

关于assembly - 从标准输入读取一个数字到寄存器中?加载 scanf 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10881303/

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