1

この (Linux、AT&T、Intel) x86 プログラムは、3 つの引数を読み取り、最大のものを存在ステータスとして %ebx に格納することを意図しています。引数をレジスタにポップすると、結果の値はバイトのように見えます。int 値を取得するにはどうすればよいですか?

[編集 -- 以下の harold のコメントのおかげで、引数の int 値を取得するためにどのように使用するかが問題だと思いますatoi。]

.section .text

.globl _start           

_start:
popl    %edi        # Get the number of arguments
popl    %eax        # Get the program name
popl    %ebx        # Get the first actual argument 
movl    (%ebx), %ebx    # get the actual value into the register (?) 
popl    %ecx        # ;
movl    (%ecx), %ecx
popl    %edx        #
movl    (%edx), %edx

bxcx:   
cmpl    %ebx,%ecx
jle     bxdx
movl    %ecx,%ebx
bxdx:
cmpl    %ebx,%edx
jle end
movl    %edx,%ebx

end:
movl    $1,%eax         
int $0x80           
4

1 に答える 1

3

を呼び出すことができるようにするにはatoi、libcに対してリンクする必要があります。例えば:

ld -lc foo.o

実際に電話をかけるには、cdeclの呼び出し規約に従う必要があります。

  1. 関数への引数はスタックに渡され、左端の引数が最後にプッシュされます。
  2. 関数の戻り値はアキュムレータ(この場合は%eax)に配置されます。
  3. レジスタ%ebp、%esi、%edi、および%ebxは呼び出し間で保持されるため、一時的な保存に使用できます。
  4. その他の必要なレジスタは、呼び出し元のコードによって保存する必要があります(上記の呼び出し先に保存されたレジスタ、引数の前のスタック、またはメモリ内の他の場所)。

の署名atoi

int atoi(const char *nptr);

したがって、最初のコマンドライン引数の整数値を取得するには、次のようにします。

.section .text

.globl _start           

_start:
popl    %edi        # Get the number of arguments
popl    %eax        # Get the program name
call    atoi        # Try to read the first argument as an integer and clobber %eax with the value
于 2012-04-05T15:17:17.817 に答える