1

I'm writing a simple assembly program on Darwin x86_64 (MacOS 10.14.6) that creates a file (test.txt) and writes "E" to it. However, for some reason, the "E" is not written to the file. What am I doing wrong?

Here's my program:

global start
section .text
    start:
        ;Create the file
        mov rax, 0x2000005
        mov rdi, fname
        mov rsi, 0x200
        mov rdx, 0o644
        syscall

        ;Write to file
        mov rdi, rax     ;As far as I know, this uses the fd returned by creating a file (previous syscall)
        mov rsi, msg
        mov rdx, 1
        mov rax, 0x2000004
        syscall

        ;Exit
        mov rax, 0x2000001
        mov rdi, 0
        syscall
section .data
    msg: db "E"     ;Message
    fname: db "test.txt"     ;File name

I have also tried this:

global start
section .text
    start:
        mov rax, 0x2000005
        mov rdi, fname
        mov rsi, 0x200
        mov rdx, 0o644
        syscall
        mov rdi, rax
        mov rsi, msg
        mov rdx, 2
        mov rax, 0x2000004
        syscall
        mov rax, 0x2000001
        mov rdi, 0
        syscall
section .data
    msg: db "E", -1
    fname: db "test.txt", 0

Yet neither work.

1 Answers1

1

I found the answer:

I found that opening the file with create mode and write mode works.

global start
section .text
    start:
        ;Open file with create mode and write mode
        mov rax, 0x2000005
        mov rdi, fname
        mov rsi, 0x201
        mov rdx, 0o644
        syscall

        ;Now write
        mov rdi, rax
        mov rsi, msg
        mov rdx, 1
        mov rax, 0x2000004
        syscall

        ;Exit
        mov rax, 0x2000001
        mov rdi, 0
        syscall
section .data
    msg: db "E"
    fname: db "test.txt"

Also, here is an array of flags for opening files:

O_ACCMODE: 0x3
O_APPEND: 0x8
O_ASYNC: 0x40
O_CLOEXEC: 0x1000000
O_CREAT: 0x200
O_DIRECTORY: 0x100000
O_DSYNC: 0x400000
O_EXCL: 0x800
O_EXLOCK: 0x20
O_NDELAY: 0x4
O_NOCTTY: 0x20000
O_NOFOLLOW: 0x100
O_NONBLOCK: 0x4
O_RDONLY: 0x0
O_RDWR: 0x2
O_SHLOCK: 0x10
O_SYNC: 0x80
O_TRUNC: 0x400
O_WRONLY: 0x1

To combine, use the | operator (in C) or or (in assembly).

  • Thanks for writing this up! Yeah, that's an easy mistake to make. If you don't open the file for writing, you are not permitted to write. – fuz Apr 05 '20 at 16:45