Assembly 读取整数并按相反顺序显示
我需要让用户输入5个整数,然后将它们放入堆栈中,然后将它们弹出,以便以相反的顺序显示它们,即输入1,2,3,4,5,则应显示5 4 3 2 1。目前它只是显示+1。有什么问题Assembly 读取整数并按相反顺序显示,assembly,masm,irvine32,Assembly,Masm,Irvine32,我需要让用户输入5个整数,然后将它们放入堆栈中,然后将它们弹出,以便以相反的顺序显示它们,即输入1,2,3,4,5,则应显示5 4 3 2 1。目前它只是显示+1。有什么问题 INCLUDE Irvine32.inc .data aName DWORD 5 DUP (?) nameSize = 5 .code main PROC mov edx, OFFSET aName mov ecx, 4 ;buffer size - 1 ; Pu
INCLUDE Irvine32.inc
.data
aName DWORD 5 DUP (?)
nameSize = 5
.code
main PROC
mov edx, OFFSET aName
mov ecx, 4 ;buffer size - 1
; Push the name on the stack.
mov ecx,nameSize
mov esi,0
L1:
Call ReadInt
push eax ; push on stack
inc esi
Loop L1
; Pop the name from the stack, in reverse,
; and store in the aName array.
mov ecx,nameSize
mov esi,0
L2: pop eax ; get character
mov aName[esi],edx;
inc esi
Loop L2
; Display the name.
mov edx,OFFSET aName
call WriteInt
call Crlf
exit
main ENDP
END main
首先,照弗兰克说的去做,然后用RTFM!!!!您正在错误的寄存器中将参数传递给WriteInt!WriteInt在
eax
中接受有符号二进制整数,而不是edx
从写入源:
;
; Writes a 32-bit signed binary integer to the console window
; in ASCII decimal.
; Receives: EAX = the integer
; Returns: nothing
; Comments: Displays a leading sign, no leading zeros.
如果必须推送和弹出,为什么要使用数组
这是错误的:
L2: pop eax ; get character
mov aName[esi],edx;
inc esi
Loop L2
您的数组是DWORD大小的,因此您应该向esi添加4
让我们简化一下:
.code
main PROC
mov ecx, nameSize
GetInput:
call ReadInt ; get input
push eax ; push to stack
loop GetInput
call Crlf
mov ecx, nameSize
DisplayIt:
pop eax ; get number from stack (LIFO)
call WriteDec ; display it
call Crlf
loop DisplayIt
call Crlf
call WaitMsg
exit
main ENDP
END main
*评论后编辑*
然而,在每个数字之间有+。为什么会这样
使用WriteDec
,就像我所做的那样
如果您查看Irvines先生的源代码或阅读手册,您会看到
WriteDec
打印无符号小数,其中WriteInt
打印有符号和无符号小数,用+或-表示有符号或无符号。您只调用WriteInt一次(然后错误调用)。什么是正确的解决方案?我又调用了WriteInt 4次,每次都有+1的输出WriteInt写什么int?(RTFM!)你想让它写什么?它是否需要在一个循环中并以弹出的形式写?好的,所以我已经成功地以相反的方式打印了它!然而,在每个数字之间有+。为什么会这样?