我是汇编编程新手,在将字符打印到屏幕上时遇到问题。每次执行程序时,我都会遇到分段错误,但我不知道为什么。
.section .data
A:
.long 65 # ascii code for 'A'
.section .text
.globl _start
_start:
movl $1, %edx # length of character to print, 1
movl A, %ecx # what I want printed
movl $1, %ebx # file descriptor for STDOUT
movl $4, %eax # syscall number for sys_write
int $0x80 # calls kernel
movl $0, %ebx # return status
movl $1, %eax # syscall number for sys_exit
int $0x80 # calls kernel
这些是我用来构建的命令(我的文件名为 write.s)
as write.s -o write.o
ld write.o -o write
这不是打印字符的正确方法吗?任何帮助,将不胜感激。
movl A, %ecx
意思是:将标签A地址处的值复制到%ecx
。正确的指令是:
movl $A, %ecx
or
leal A, %ecx
在这些情况下,您可以使用 GDB 进行调试(请注意,您必须使用-g
标志来获取调试信息):
$ as -g write.s -o write.o
$ ld write.o -o write
$ gdb write
GNU gdb (GDB) 7.5
[.. snip ..]
(gdb) b test.s:13
Breakpoint 1 at 0x4000c6: file test.s, line 13.
(gdb) run
Starting program: /home/niklas/tmp/asm/write
Breakpoint 1, _start () at test.s:13
13 int $0x80 # calls kernel
(gdb) info registers ecx
ecx 0x41 65
如你所见,%ecx
具有整数值65
,这不是你想要的。
如果你跑strace ./write
,它将解码系统调用参数并为您返回值。你会看到write()
刚刚返回-EFAULT
当你传递一个错误的指针时,不做任何其他事情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)