x86程序集 - 使用ascii代码打印字符

时间:2012-08-31 16:16:26

标签: linux assembly x86

我是装配编程的新手,在将字符打印到屏幕时遇到问题。每次执行我的程序时,我都会遇到分段错误,我不知道为什么。

.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

这不是打印角色的正确方法吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

movl A, %ecx

表示:将标签A地址的值复制到%ecx。正确的指示是:<​​/ p>

movl $A, %ecx

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,这不是您想要的。

相关问题