0

I am trying to create a file in x86 Assembly and write to it. However, no file is being created. I have tried numerous tutorials, asking chatgpt, looking at the documentation but could not find where I went wrong. I would gladly appreciate any help. The program exits without any errors. The code is being assembled without any errors as well.

.att_syntax
.section .text
.global main

main:
    # print out debug message
    movl $4, %eax               # write (syscall)
    movl $1, %ebx               # stdout
    movl $debug_msg, %ecx           #
    movl $15, %edx              #
    int $0x80               #

    # create file
    movl $2, %eax               # open (syscall)
    movl $filename, %ebx            # set filename to 'out.asm'
    movl $0x202, %ecx           # flags - write + create
    movl $0x644, %edx           # usermode - set permissions to read and write (owner) and read(all)
    int $0x80               #
    # push file descriptor onto stack
    pushl %eax

    # write basic setup to file
    movl $1, %eax               # write
    popl %ebx
    pushl %ebx
    movl $debug_msg, %ecx
    movl $18, %edx

    # open src code

    # compile src code
    
    # close file
    movl $3, %eax       # close (syscall)
    popl %ebx       # file descriptor
    pushl %ebx
    int $0x80           # 

.section .data
    debug_msg:
    .string "Debug String :)"

    filename:
    .string "out.txt"

Any help would be very appreciated! :)

lstuma
  • 31
  • 4
  • 1
    The 32 bit syscall number for `open` is not 2, that is `fork`. You want `5`. Note that `open` can not create a file, for that you want `creat`. A helpful tool is `strace`. – Jester Mar 22 '23 at 16:05
  • Thank you so much! I have another question though. According to this: https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md the syscall number for open would be 2. Why is this one wrong and do you know anywhere I can get the correct syscall numbers? – lstuma Mar 22 '23 at 16:13
  • 2
    @lstuma: You are looking in the x86-64 section of the document. That would be correct for 64-bit code, but you are writing 32-bit code, so look in the "x86 (32-bit)" section further down. – Nate Eldredge Mar 22 '23 at 16:26
  • 2
    Also note that you have to invoke the `_exit` system call when you are done. As it stands your program will just execute whatever garbage happens to follow your code in memory, and probably crash (if you are lucky). Also, your stack handling looks very questionable, as you have more pushes than pops. – Nate Eldredge Mar 22 '23 at 16:27
  • 1
    (Alternatively, rewrite your code as 64-bit, and join us here in the 21st century. For the last 15 years or so, the only place you find 32-bit x86 assembly code is in the millions of obsolete tutorials that litter the Internet.) – Nate Eldredge Mar 22 '23 at 16:29
  • @Jester: `open(O_CREAT)` will create a file if it doesn't exist. As the POSIX man page says (https://man7.org/linux/man-pages/man3/creat.3p.html), `creat(path, mode)` is exactly equivalent to `open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);` – Peter Cordes Mar 22 '23 at 16:42
  • 1
    `0x644` is not what you want for mode. Unix permissions go in groups of 3 bits (octal digits) not 4 (hex digits), so you want `0q644` ([How to represent octal numbers in Assembly?](https://stackoverflow.com/q/39564402)). Or preferably `0q666` and let the user's `umask` do its job, usually clearing the write bits for other and maybe group. – Peter Cordes Mar 22 '23 at 16:45
  • @NateEldredge I will join the rest of the 21st century once I have gotten the hang of 32 bit x86 assembly. But for now this is only to flex in front of my teacher and has no serious real world applications anyway. – lstuma Mar 22 '23 at 16:52
  • @PeterCordes yeah, I assumed that is only for the C wrapper but apparently it works with the syscall as well, you are correct. – Jester Mar 22 '23 at 19:06
  • @lstuma: Up to you, of course. My point is just that x86-64 assembly is not really any more difficult than x86-32, and it's more immediately useful. I don't think it really helps to do 32-bit as a "warmup" for 64-bit. The fundamentals will carry over, but you'd learn them just as well and just as quickly if you started with 64-bit. And then you will also spend time learning about quirks that are specific to x86-32, that *won't* carry over to x86-64 and will have been a waste of time in the long run. – Nate Eldredge Mar 23 '23 at 03:31
  • It's kind of like saying your plan for learning to use a computer is to first get the hang of using a typewriter. Yes, on a typewriter you can learn to touch type, which is helpful for using a computer. But you'll also spend a lot of time learning to change ribbons, feed paper, lubricate the carriage, use correcting tape, etc, which don't translate into marketable computer skills. – Nate Eldredge Mar 23 '23 at 03:34

0 Answers0