我目前正忙于组装并遇到以下问题:
我正在尝试获取已输入 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