1

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!

Cœur
  • 37,241
  • 25
  • 195
  • 267
g0mb4
  • 100
  • 12
  • Putting back the line `movl 8(%esp), %ebx` that you have commented out seems to work fine here. PS: before you proceed any further learn to use a debugger, it will help a lot. – Jester Mar 25 '14 at 00:06
  • I commented it out, becouse it did not work for me, I know, that google is my friend, but I found "movl 8(%esp), %ebx" must work, but it didn't. I used gdb, but I couldn't figure it out :/ – g0mb4 Mar 25 '14 at 00:10

1 Answers1

3

Oh, I see the problem. Nice that you have included how you build the program (+1 for that). If you check the resulting executable using the file command, I bet it says 64 bit:

$ file pfile
pfile: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

To make a 32 bit program you should use:

$ as --32 pfile.S -o pfile.o
$ ld -melf_i386 pfile.o -o pfile
$ file pfile
pfile: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
$ ./pfile pfile.S
    .code32

    .section .data
...

Alternatively you can do gcc -m32 -nostdlib pfile.S -o pfile

Jester
  • 56,577
  • 4
  • 81
  • 125