【问题标题】:Assembly language programming using nasm alongwith c library使用 nasm 和 c 库进行汇编语言编程
【发布时间】:2018-02-11 13:59:15
【问题描述】:

任何人都可以用堆栈解释我的代码吗?下面给出的代码应该是用于打印命令行参数,然后向它添加 1 并再次打印它。 但是由于在这种情况下堆栈工作,我完全感到困惑。 而且我认为代码中有一些错误。

SECTION .data
msg:db"You Entered -%s", 10, 0; print argv[1] data
msg2:db"This is int-%d", 10, 0; print INT equivalent

SECTION .text
extern printf
extern atoi
global main
main:
; set-up phase

push ebp
mov ebp, esp

; get the command-line data

mov ebx, DWORD [esp+ 12]           ; get argvstarting address
mov ebx, [ebx+ 4]                    ; get the second argument data

; print the value

push ebx                               ; put data on stack for call
push msg                               ; print the value
call printf

; convert to integer

add esp, 4                           ; stack points to entry edx
call atoi                           ; call atoi-return in EAX?

; get return value and add 1

add esp, 4                          ; esppoints to start
inc eax                             ; increase eaxfor testing

; print the result

push eax                           ; push argfor print
push msg2                         ; push print message
call printf

; finish phase

add esp, 8                        ; esp back to start
movesp, ebp
pop ebp
ret

【问题讨论】:

    标签: assembly x86 nasm


    【解决方案1】:

    所以这里发生的事情是代码将某些东西“推”到堆栈上,或者在高级语言中,为函数 printf 提供了它完成工作所需的参数。一旦 printf() 被调用一次,变量就会变成一个整数,递增,然后再次调用 printf()。堆栈被添加是因为还有其他元素。例如,如果我将 2 个元素压入堆栈,并且我想在我压入第二个元素之前将我压入堆栈的元素获取,那么我需要向堆栈添加/减去。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      相关资源
      最近更新 更多