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

関数を呼び出すので、スタック上の関数にパラメーターを渡します。で 1 つの整数が返されeax、残りはスタック上の入出力ポインタ パラメータを介して返されます。x86 呼び出し規約を確認してください。

編集 0:

leal命令は、一時変数の有効アドレス(整数値を入れる場所)scanfに格納しeax、それをscanfスタックに渡します。こちらをご覧ください: LEA 命令の目的は何ですか?

于 2012-06-04T12:47:12.083 に答える