linux nasm代码什么都没显示

时间:2012-10-12 08:26:35

标签: linux assembly nasm

我正在制作一个用户输入数字的程序,它会打印出从零到数字的所有数字。它编译得很好,链接很好,并且在运行时不会返回任何错误,但它绝对没有打印出来。这是代码:

SECTION .data 
len   EQU 32

SECTION .bss 
other resd len
data  resd len

SECTION .text

GLOBAL _start
_start:
nop

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

mov ebp, 1

setup:                  ; This section sets up the registers ready for looping 
mov [other], ebp

loop:                   ; This section loops, printing out from zero to the number given
mov eax, 4
mov ebx, 1
mov ecx, [other]
mov edx, len
int 80h

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

当我在KDBG上单步执行时,它会返回一些错误;虽然我不知道在哪里,它会收到中断和分段错误。我不知道为什么会这样,因为当我在Geany中运行它时,它会在结尾处返回0值并且运行时没有错误。为什么不起作用?

提前致谢

注意:此代码不循环。它尚未完成。这里应该做的就是打印出数字1。

1 个答案:

答案 0 :(得分:1)

当您打印时,您正在呼叫mov ecx, [other]。这会查看存储在其他地址中的地址,并跟随该地址以获取存储在那里的任何内容。问题是这个系统调用期望ecx中的地址,而不是值。

如果您拨打mov ecx, other,那么ecx将拥有其他地址,并且可以转到该地址并打印出那里的内容。

此处还有另一个问题:当您打印存储在other中的号码时,它会将其转换为ascii值。因此,例如,当您尝试打印1而不是打印数字1时,它将打印ascii 1(这恰好是标题字符的开头;您不想打印任何内容)。如果要打印数字,请添加'0'(字符“0”)。

编辑:还有一件事,当你读到时,你将1传递给ebx。 1是STDOUT。你想要的是STDIN,它是0。