3

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

我正在尝试获取已输入 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
4

1 回答 1

2

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

编辑0:

leal指令将一些临时变量的有效地址(即scanf放置整数值的位置)存储到eax中,然后将其传递到scanf堆栈中。看看这里:LEA 指令的目的是什么?

于 2012-06-04T12:47:12.083 回答