sambia39
sambia39

Reputation: 35

syscall OSX create file [open]

I intend to create a file using Mac system calls (OS X and not the bios for now). Everything compiles well, but the result that should be the creating the file "fileName db "Teste.txt", 0xA, 0x0" is not realized and I do not understand. can you help me, I also post you the source code compiled with nasm Thank you in advance for your answers.

 

;------------------------------------------------------------------------------
; TITLE: SOURCE.ASM
;------------------------------------------------------------------------------

bits 64

;------------------------------------------------------------------------------
;   Definition des syscall
;------------------------------------------------------------------------------
%define _stdout         0x1
%define _stderr         0x2

%define O_RDONLY        0x0000          ; open for reading only
%define O_WRONLY        0x0001          ; open for writing only
%define O_RDWR          0x0002          ; open for reading and writing
%define O_ACCMODE       0x0003          ; mask for above modes
%define O_CREAT         0x0200          ; create if nonexistant 
%define O_TRUNC         0x0400          ; truncate to zero length 
%define O_EXCL          0x0800          ; error if already exists

%define syscall_write   0x2000004
%define syscall_exit    0x2000001
%define syscall_open    0x2000005
%define syscall_close   0x2000006

;------------------------------------------------------------------------------
;   Section code
;------------------------------------------------------------------------------
section .text

global start

start:

    xor rax, rax
    xor rbx, rbx
    mov rbx, fileName
    push mode_f
    push FLAGS
    push rbx
    syscall
    pop rax

    xor rbx, rbx
    mov rbx, rax
    mov rax, syscall_exit
    mov rdi, rbx
    syscall


f_open_file:
    mov rbp, rsp
    push rbp
    xor rax, rax
    mov rax, syscall_open
    mov rdi, [rbp+8]
    mov rsi, [rbp+16]
    mov rdx, [rbp+24]
    syscall
    pop rax

    if: cmp rax, -1
        jge else
        mov rbx, rax
        mov rax, syscall_close
        mov rdi, rbx
        syscall
        pop rax
        ret
    else:
        xor rbx, rbx
        mov rbx, MsgErrr
        push size_err
        push rbx
        call f_print_data
        xor rax, rax
        mov rax, -1
        ret

f_print_data:
    mov rbp, rsp
    push rbp
    mov rax, syscall_write
    mov rdi, _stdout
    mov rsi, [rbp+8]
    mov rdx, [rbp+16]
    syscall
    xor rax, rax
    ret


;------------------------------------------------------------------------------
;   Section Data
;------------------------------------------------------------------------------

section .data
    fileName    db "Teste.txt", 0xA, 0x0
    MsgErrr     db "Erreur open File", 0xA, 0x0
    size_file   equ ($-fileName)
    size_err    equ ($-MsgErrr)
    mode_f      equ 0777
    FLAGS       equ 0xA02 ;( O_RDWR| O_CREAT| O_EXCL )

Upvotes: 2

Views: 1406

Answers (2)

user149341
user149341

Reputation:

If you want to write assembly code that makes direct system calls, use a Linux virtual machine.

The macOS system call interface is not a public API. There is no official documentation for this interface, and it differs in a number of important ways from the Linux system call interface. This makes it a terrible place for a beginner to be learning assembly!

There are much better debugging tools available on Linux as well, such as strace, which will allow you to see the exact system calls being made by your application.

Upvotes: 2

Sep Roland
Sep Roland

Reputation: 39446

fileName    db "Teste.txt", 0xA, 0x0

The inclusion of the newline code 0xA is fine for displaying purposes, but for opening it is no good! Drop it:

fileName    db "Teste.txt", 0x0

mov rbx, fileName
push mode_f
push FLAGS
push rbx
syscall

Your code is missing the open command in order to create the file!

mov rbx, fileName
push mode_f
push FLAGS
push rbx
mov  rax, syscall_open
syscall

f_open_file:
    mov rbp, rsp
    push rbp

To actually preserve rbp you need to swap these two instruction:

f_open_file:
    push rbp
    mov  rbp, rsp

In doing so, you'll also need to adjust the offsets used on [rbp + ...].


All of your subroutines forget to pop rbp, and so you end up with an unbalanced stack. This leads to "segmentation faults".

Upvotes: 6

Related Questions