X86 NASM中在堆栈上传递参数的问题
我正在尝试编写一个函数,将堆栈中的整数转换为ASCII码。转换工作正常,但在堆栈上传递的参数有问题X86 NASM中在堆栈上传递参数的问题,x86,stack,call,nasm,assembly,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 ;
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一切正常。如果在16位CPU模式下运行此程序,则推送/弹出堆栈级别为2字节,而不是4字节。所以你的堆栈计算是错误的!您使用了错误的nasm指令,因为您使用的是32位寄存器而不是16位。您使用的是哪个操作系统(DOS)?这是16位程序还是32位?因为您正在使用32位寄存器并调用DOS interupt int 21!我是一个新的汇编程序,我不知道这一点。可以编写32位控制台应用程序并使用DOS中断吗?Windows IA32(32位模式)模式不支持像int 21这样的旧DOS中断!