0

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Yoffdan
  • 68
  • 7

1 Answers1

3

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.

Kamil.S
  • 5,205
  • 2
  • 22
  • 51
  • Awsome! That works! Could you do me a favor and explain why its 0x200000a instead of 0x2000010? I kept using system calls like 0x2000001, 0x2000003, 0x2000004, etc. How come there is an a at the end? – Yoffdan Oct 15 '21 at 15:54
  • Pick any dec <=> hex calculator to get familiar with the bidirectional conversion between decimal and hex and how numbers are represented in each number system. 10 decimal is 0xa in hex, 11 decimal is 0xb in hex , etc – Kamil.S Oct 15 '21 at 19:40
  • Thanks! That clears a lot of things up – Yoffdan Oct 17 '21 at 14:32