linux nasm程序集无休止地打印字符

时间:2012-10-13 06:49:18

标签: linux assembly x86 nasm

我正在编写一个程序来从用户那里获取一个整数,然后打印出从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.

1 个答案:

答案 0 :(得分:1)

这正在把我的记忆深深地挖回到我记忆深处的深处,但我想你想要

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

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