【问题标题】:linux nasm assembly endlessly printing characterlinux nasm程序集无休止地打印字符
【发布时间】:2012-10-13 06:49:18
【问题描述】:

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

【问题讨论】:

  • 尝试使用像gdb这样的调试器来逐步执行机器指令
  • 程序的语义很难理解。 other 是什么意思?它看起来是一个 32 字节的缓冲区,但有时会写入一个 16 位的整数;其他地方比较它的地址。我分不清对错。
  • other 是一个计数器变量,我用它来显示小于输入的数字。

标签: linux assembly x86 nasm


【解决方案1】:

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

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

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

【讨论】:

  • 它现在不会无限循环,它会喷出大约 200 个以数字 0 结尾的字符,直到输入的数字。我怎样才能摆脱所有这些初始字符?
  • 只是一个建议...当您执行mov edx, len 时,您告诉 sys_write 打印 32 个字符。您可能只需要 1 个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 2014-02-21
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多