Yoffdan
Yoffdan

Reputation: 66

Deleting/Unlinking a file in nasm assembly on mac 64 bit

I am attempting to delete or unlink a file. Unlink meaning delete, I think (from what I have read).

I read documentation and another documentation. They both said that unlinking takes in 1 argument. const char * pathname.

I've done that, however, the file that I want to delete is not getting deleted. Does anyone know why? Here is my code:

global start

section .text
    start:
        ;This is the deleting/unlinking part
        mov rax, 0x2000010; unlinking
        mov rdi, file ; contains path and the file. If you look down more in section .data you can see the file and path      
        syscall
       
        ;This part is not important: Its just exiting
        mov rax, 0x2000001       ;Exiting
        xor rdi, rdi         
        syscall          

section .data
    file: db "/Users/daniel.yoffe/desktop/assembly/CoolFile.txt", 0

I've also looked at an example in linux. It was just like this. Is there something that I have done wrong? Does unlinking even delete a file? Am I missing something maybe another argument?

Help would be appreciated.

Upvotes: 0

Views: 219

Answers (1)

Kamil.S
Kamil.S

Reputation: 5543

You're using an incorrect hybrid of decimal and hexadecimal numbers. For syscall 10 you want:

;This is the deleting/unlinking part
        mov rax, 0x200000a; unlinking
        mov rdi, file ; contains path and the file. If you look down more in section .data you can see the file and path      
        syscall

On a side note ret instead of the your 2nd system exit syscall should suffice for modern Mach-o MacOS executables.

Upvotes: 3

Related Questions