2011-01-29 38 views
0

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

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一切正常。

+0

您正在使用哪种操作系统(DOS)? – 2011-01-29 23:07:15

+0

我使用Win XP – st0n3 2011-01-29 23:49:13

回答

1

如果您在16位CPU模式乳宁这个方案比推/弹出堆栈级别为2个字节,而不是4。所以你的筹码calcolation SI错了!而且您使用的是错误的nasm指令,因为您使用的是32位寄存器,而不是16位。