<分区>
我正在尝试学习 Linux 上的汇编编程,所以我用谷歌搜索/创建了一个非常简单的“cat”实现,但是我的小程序不能使用命令行参数(它说“Colud't open the file ”)。当我取消注释“fname”行时,它起作用了,所以文件 I/O 没问题。所以我认为堆栈部分坏了:/这是我的代码:
.code32
.section .data
msg_err_open:
.asciz "Colud't open the file.\n"
msg_err_open_len:
.long . - msg_err_open
fname:
.asciz "test.txt"
.section .bss
.equ BUFSIZE, 1024
.lcomm buf, BUFSIZE
.section .text
.globl _start
.align 4
_start:
# popl %ebx # argc
# popl %ebx # argv[0]
# popl %ebx # argv[1] (file)
# open
movl $5, %eax # open(
# movl 8(%esp), %ebx # filename, ????????????????
movl $fname, %ebx
movl $0, %ecx # readonly
int $0x80 # )
test %eax, %eax # megnyitás sikerült?
js err_open # ha negatív
# read
movl %eax, %ebx # file descriptor eax->ebx
movl $3, %eax # read( fd (ebx),
movl $buf, %ecx # buffer,
movl $BUFSIZE, %edx # size
int $0x80 # )
# close
movl $6, %eax # close( fd (ebx)
int $0x80 # )
# write
movl $4, %eax # write(
movl $1, %ebx # STDOUT,
movl $buf, %ecx # buffer
int $0x80 #)
# exit
movl $1, %eax # exit(
movl $0, %ebx # 0
int $0x80 # )
err_open:
# write (msg_err_open)
movl $4, %eax
movl $1, %ebx
movl $msg_err_open, %ecx
movl $msg_err_open_len, %edx # length
int $0x80
# exit(1)
movl $1, %eax
movl $1, %ebx
int $0x80
我这样完成/链接它:
as pfile.S -o pfile.o
ld pfile.o -o pfile
我的 Linux 发行版是:
Debian 3.2.41-2+deb7u2
AS 版本:
2.22 (x86_64-linux-gnu)
我认为解决方案很简单,但我没有看到。我希望它在 32 位模式下运行,x64 现在对我来说很难。感谢您的宝贵时间!