Common tasks on AArch64 using ASM

Use Case 1 - Basic File Deletion:

.global _start
.section .data
filename: .asciz "/path/to/file"

.section .text
_start:
    mov x0, filename  // filename
    mov x8, 87        // syscall number for unlink
    svc 0             // make the system call

    mov x8, 93        // syscall number for exit
    mov x0, 0         // exit code
    svc 0             // make the system call

Use Case 2 - File Size Check:

.global _start
.section .data
filename: .asciz "/path/to/file"
filestat: .skip 144  // space for struct stat

.section .text
_start:
    mov x0, filename  // filename
    mov x1, filestat  // struct stat
    mov x8, 4         // syscall number for stat
    svc 0             // make the system call

    ldr x0, [x1, 48]  // load st_size field from struct stat
    cmp x0, #1024     // compare file size with limit
    b.gt delete       // if greater, go to delete

exit:
    mov x8, 93        // syscall number for exit
    mov x0, 0         // exit code
    svc 0             // make the system call

delete:
    mov x0, filename  // filename
    mov x8, 87        // syscall number for unlink
    svc 0             // make the system call
    b exit            // go to exit

Use Case 3 - Memory Allocation:

.global _start
.section .data
size: .quad 4096  // size of memory to allocate

.section .text
_start:
    mov x0, 0         // address
    ldr x1, =size     // size
    mov x2, 7         // prot
    mov x3, 34        // flags
    mov x4, -1        // fd
    mov x5, 0         // offset
    mov x8, 222       // syscall number for mmap
    svc 0             // make the system call

    mov x8, 93        // syscall number for exit
    mov x0, 0         // exit code
    svc 0             // make the system call

Use Case 4 - File Reading:

.global _start
.section .data
filename: .asciz "/path/to/file"
buffer: .skip 4096  // space for file contents

.section .text
_start:
    mov x0, filename  // filename
    mov x1, 0         // flags
    mov x2, 0         // mode
    mov x8, 56        // syscall number for openat
    svc 0             // make the system call

    mov x1, x0        // fd
    mov x0, buffer    // buffer
    mov x2, 4096      // count
    mov x8, 63        // syscall number for read
    svc 0             // make the system call

    mov x8, 93        // syscall number for exit
    mov x0, 0         // exit code
    svc 0             // make the system call

Use Case 5 - Memory Deallocation:

.global _start
.section .data
size: .quad 4096  // size of memory to deallocate

.section .text
_start:
    mov x0, buffer    // address
    ldr x1, =size     // size
    mov x8, 215       // syscall number for munmap
    svc 0             // make the system call

    mov x8, 93        // syscall number for exit
    mov x0, 0         // exit code
    svc 0             // make the system call

Use Case 6 - Error Handling:

.global _start
.section .data
filename: .asciz "/path/to/file"
buffer: .skip 4096  // space for file contents
error_message: .asciz "An error occurred\n"

.section .text
_start:
    mov x0, filename  // filename
    mov x1, 0         // flags
    mov x2, 0         // mode
    mov x8, 56        // syscall number for openat
    svc 0             // make the system call

    cmp x0, 0         // check if the system call failed
    blt error         // if less than zero, go to error

    mov x1, x0        // fd
    mov x0, buffer    // buffer
    mov x2, 4096      // count
    mov x8, 63        // syscall number for read
    svc 0             // make the system call

    cmp x0, 0         // check if the system call failed
    blt error         // if less than zero, go to error

exit:
    mov x8, 93        // syscall number for exit
    mov x0, 0         // exit code
    svc 0             // make the system call

error:
    mov x0, 1         // file descriptor (stdout)
    mov x1, error_message  // buffer
    mov x2, 18        // count
    mov x8, 64        // syscall number for write
    svc 0             // make the system call
    b exit            // go to exit

Use Case 7 - User Input:

.global _start
.section .data
buffer: .skip 4096  // space for file contents

.section .text
_start:
    ldr x0, [sp]      // argc
    add x1, sp, 8     // argv

    cmp x0, 2         // check if there are at least two arguments
    blt error         // if less than two, go to error

    ldr x0, [x1, 8]   // filename (second argument)
    mov x1, 0         // flags
    mov x2, 0         // mode
    mov x8, 56        // syscall number for openat
    svc 0             // make the system call

    cmp x0, 0         // check if the system call failed
    blt error         // if less than zero, go to error

    mov x1, x0        // fd
    mov x0, buffer    // buffer
    mov x2, 4096      // count
    mov x8, 63        // syscall number for read
    svc 0             // make the system call

    cmp x0, 0         // check if the system call failed
    blt error         // if less than zero, go to error

exit:
    mov x8, 93        // syscall number for exit
    mov x0, 0         // exit code
    svc 0             // make the system call

error:
    mov x0, 1         // file descriptor (stdout)
    mov x1, error_message  // buffer
    mov x2, 18        // count
    mov x8, 64        // syscall number for write
    svc 0             // make the system call
    b exit            // go to exit

Use Case 8 - Recursive deletion:

.global _start
.section .data
    file1: .asciz "/path/to/file1"
    file2: .asciz "/path/to/file2"
    files: .quad file1, file2, 0

.section .text
_start:
    mov x19, files
loop:

    ldr x0, [x19], 8
    cbz x0, exit
    bl unlink
    b loop
exit:
    mov x8, 60
    xor x0, x0
    svc 0

    .global _start
.section .data
    dir: .asciz "/path/to/directory"
    statbuf: .space 144

Disclaimer

DISCLAIMER: The information and code provided here are intended for educational and informational purposes only. The user assumes full responsibility for the use of this information and code. The provider of this information and code makes no representations or warranties, express or implied, about the completeness, accuracy, reliability, suitability, or availability of the information and code provided. Any reliance you place on such information and any use of this code is therefore strictly at your own risk. In no event will the provider be liable for any loss or damage including without limitation, indirect or consequential loss or damage, arising out of, or in connection with, the use of this information and code.