开发者

Problem with passing parameters on the stack in NASM

I am trying to write a function to convert an integer pushed in the stack to ASCII codes. The conversion works fine but I have a problem with the parameter that is passed on the stack.

org 100h 

section .text 

start: 

mov eax, 12345
push eax

call print_int
add esp, 4      ;clear the stack

jmp _exit

;value is i开发者_运维问答n 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 

The problem is that mov eax, [ebp+8] sets eax to 0 instead of 12345. If I change mov eax, [ebp+8] to mov eax, 12345 everything is OK.


If you are runing this program under 16 bit CPU mode than the push/pop stack level is 2 bytes and not 4. So your stack calcolation si wrong! And you are using wrong nasm directive, because you are using 32 bit registers instead 16 bit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜