使用nasm和c库进行汇编语言编程

时间:2018-02-11 13:59:15

标签: assembly x86 nasm

任何人都可以用堆栈解释我的代码吗?下面给出的代码应该用于打印命令行参数然后将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

1 个答案:

答案 0 :(得分:0)

所以这里发生的是代码是"推动"把东西放到堆栈上,或者用高级语言,给函数printf提供它完成它所需的参数。一旦调用printf(),变量将变为整数,递增,并再次调用printf()。添加堆栈是因为还有其他元素。例如,如果我将2个元素推入堆栈,并且我想在推送第2个元素之前获取我推入堆栈的元素,那么我需要添加/减去堆栈。

相关问题