c - 在以下 NASM 任务中需要帮助

标签 c linux assembly x86 nasm

我在 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

你能帮我找出我在这里犯的错误吗?如果您向我提供任何 NASM 教程(无论是视频还是文本),我也将不胜感激。我已经获得了《汇编语言艺术》或《NASM 手册》。但第一个不是基于 NASM,第二个对于像我这样的初学者来说很难获得。

谢谢

最佳答案

这应该能让你继续:

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.

输出: enter image description here

关于c - 在以下 NASM 任务中需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14874265/

相关文章:

c - 为什么不应该在 linux 内核中将全局变量初始化为 0/NULL/false?

linux - Bash:如何从 txt 文件中读取行,按列值识别和删除重复项

c - 在汇编 x86 或 ARM 中引发软中断

c - 从结构体数组返回元素

c - 在 C 中交换方矩阵名称

linux - Alsa amixer 在使用 softvol 时列出播放和捕获设备

assembly - 中断窗口

assembly - 实现 ARMv5TE 和 VFPv1 的 ARM 处理器?

python - 使用 ctypes 映射基本 typedef 别名?

linux - imagemagick convert 命令在我的系统上似乎无法正常工作