0

Ubuntu11.04 の NASM で 2 つの入力数値を取り、合計を生成するプログラムを作成しました。プログラムは次のとおりです。

    section .data
       msg1:    db "the numbers are:%d%d",0
       msg3:    db "REsult= %d",10,0
    section .bss
       a resd 1
       b resd 1
       sum resd 1
    section .text
      global main
      extern printf,scanf
    main:
;; push ebp
;; mov ebp,esp
;; sub esp,10

      push a
      push b
      push msg1
      call scanf
      add esp,12

      mov eax,DWORD[a]
      add eax,DWORD[b]
      mov DWORD[sum],eax

      push DWORD[sum]
      push msg3
      call printf
      add esp,8

;; mov esp,ebp
;; pop ebp
       ret

私がここで犯した間違いを見つけるのを手伝ってくれませんか?また、Vedio であれテキストであれ、NASM のチュートリアルについて教えていただければ幸いです。Art of Assembly Language または NASM マニュアルを入手しました。しかし、1 つ目は NASM に基づいておらず、2 つ目は私のような初心者には入手が困難です。

ありがとう

4

1 に答える 1

0

これでうまくいくはずです:

global main
extern printf, scanf, exit

section .data 
scanf_fmt       db  "%d %d", 0
printf1_fmt     db  "The numbers entered are: %d and %d,", " The result = %d", 10, 0

main:
    push    ebp
    mov     ebp, esp
    sub     esp, 8                          ; make room for 2 dwords

    lea     eax, [ebp - 4]                  ; get pointers to our locals
    lea     ecx, [ebp - 8]                  ;
    push    eax                             ; give pointers to scanf
    push    ecx                             ;
    push    scanf_fmt                       ;
    call    scanf                           ; read in 2 dwords
    add     esp, 12

    mov     eax, dword [ebp - 4]            ; add em together
    add     eax, dword [ebp - 8]            ;

    push    eax                             ; total
    push    dword [ebp - 4]                 ; second num
    push    dword [ebp - 8]                 ; first num
    push    printf1_fmt                     ; format string
    call    printf                          ; show it
    add     esp, 16                         

    add     esp, 8                          ; restore stack pointers
    mov     esp, ebp
    pop     ebp
    call    exit                            ; linking agaist the c libs, must use exit.

出力: ここに画像の説明を入力

于 2013-02-23T18:22:42.193 に答える