繁体   English   中英

x86 程序集 - 打印给定 ascii 代码的字符

[英]x86 assembly - printing a character given an ascii code

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

.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

要么

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而不执行任何其他操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM