1

我正在编写一个程序来从用户那里获取一个整数,然后打印出从 0 到该数字的所有数字。我的代码可以很好地输入,但是在将其打印出来时,它会以似乎无限循环的方式连续打印。这是我的代码:

SECTION .data           ; Constant variable declaration
len   EQU 32        ; Constant length
msg db "Enter a number: ", 0 ; Input message
msglen EQU $-msg             ; Input message length

SECTION .bss            ; Uninitialised data declaration
other resd len      ; Output counter that is incremented
data  resd len      ; Input data buffer 

SECTION .text           ; Main program initialiser

GLOBAL _start           ; Linker entry point declaration
_start:                 ; Entry point
nop                 ; This keeps the debugger happy :)

Msg:                    ; This section prints out the message
mov eax, 4          ; }
mov ebx, 1          ; }
mov ecx, msg        ; } System_write call
mov edx, msglen     ; }
int 80h             ; }

input:                  ; This section gets the integer from the user
mov eax, 3          ; }
mov ebx, 0          ; }
mov ecx, data       ; } System_read call
mov edx, len        ; }
int 80h             ; }

ASCIIAdj:   
mov ebp, 48         ; This line sets the counter to '0' ASCII

setup:                  ; This section adjusts the counter
mov [other], ebp    ; Increment counter 

loop:                   ; This section loops, printing out from zero to the number given
mov eax, 4          ; }
mov ebx, 1          ; }
mov ecx, other      ; } System_write call
mov edx, len        ; }
int 80h             ; }
mov eax, 1          ; Move 1 to eax
add ebp, eax        ; Add eax to ebp (essentially increment ebp)
    mov eax, other      ; move other to eax
    mov ebx, data       ; move data to ebx
    cmp eax, ebx        ; compare them
jne setup           ; If they are not the same, go back to the setup to increment other

exit:                   ; Exits the program
mov eax, 1          ; }
mov ebx, 0          ; } System_exit call
int 80h             ; }

为什么会不断循环?我已经增加了计数器,并比较了输入和计数器,那么它为什么不中断呢?

提前致谢

编辑:预期输出:

Enter a number: 6
0123456

程序的一般语义:

Display "Enter a number: "
Read in an integer less than 32 bytes in size.
Set a counter variable to the ASCII value of zero
Loop:
Display the character, adding 1 to it, and checking to see if it is equal to the value    inputted.
If it is equal, goto the exit section and exit
Else loop.
4

1 回答 1

1

这是在挖掘我记忆深处的黑暗深处,但我认为你想要

mov eax, [other]      ; move other to eax
mov ebx, [data]       ; move data to ebx

请注意代码中缺少的括号。您正在将 and 的地址加载到and,而不是其中包含的值。otherdataeaxebx

于 2012-10-13T07:03:00.617 回答