读入整数并以相反的顺序显示

时间:2014-12-13 02:06:55

标签: 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


   ; 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

1 个答案:

答案 0 :(得分:0)

首先,就像Frank说的那样和RTFM一样!您正在将参数传递给WRONG寄存器中的WriteInt! WriteInt在eax NOT edx !!!

中采用带符号的二进制整数

来自WriteInt来源:

;
; 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打印有符号和无符号小数,用+或 - 表示签名与否。

相关问题