在NASM中传递堆栈参数的问题

时间:2011-01-29 17:59:50

标签: x86 stack call nasm assembly

我正在尝试编写一个函数来将堆栈中推送的整数转换为ASCII代码。转换工作正常,但我在堆栈上传递的参数有问题。

org 100h 

section .text 

start: 

mov eax, 12345
push eax

call print_int
add esp, 4      ;clear the stack

jmp _exit

;value is in the stack
print_int:

push ebp
mov ebp, esp

mov ecx, 0Ah        ;divide by 10
mov eax, [ebp+8]    ;value is in ebp + 8

again1:

mov edx, 0
idiv ecx       ;quotent in EAX, remainder in EDX

push edx

cmp eax, 0
jne again1


printing:

;output a digit
pop edx     ;get digit from stack

add dl, 30h ;convert to ASCII
mov ah, 02h
int 21h     ; print


cmp esp, ebp
jne printing


mov esp, ebp
pop ebp
ret

_exit:
mov al, 0
mov ah, 4ch
int 21h

section .data 
section .bss 

问题是mov eax,[ebp + 8]将eax设置为0而不是12345.如果我将mov eax,[ebp + 8]更改为mov eax,12345一切正常。

1 个答案:

答案 0 :(得分:1)

如果你在16位CPU模式下运行这个程序比推/弹堆栈级别是2个字节而不是4.所以你的堆栈calcolation si错了!并且您使用的是错误的nasm指令,因为您使用的是32位寄存器而不是16位。