I'm trying to learn Assembly programming on linux, so I googled/created a very simple implementation of "cat", but my little program does not work with command-line arguments (it says "Colud't open the file"). When I uncomment the "fname" line, it works, so the file I/O is fine. So I think the stack-part is broken :/ Here's my code:
.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
I comple/link it this way:
as pfile.S -o pfile.o
ld pfile.o -o pfile
My linux distro is:
Debian 3.2.41-2+deb7u2
AS version:
2.22 (x86_64-linux-gnu)
I think the solution is trivial, but I don't see it. I want it to run on 32 bit mode, x64 is pretty hard for me now. Thank you for your time!