Files
ls-arm/main.S
T
2025-08-20 18:24:41 +02:00

169 lines
3.7 KiB
ArmAsm

.include "syscalls.S"
.include "errors.S"
.global _start
exit: @ r0 should have the status code to exit with
mov r7, #SYS_EXIT @ Move the exit syscall number to r7 (which is the register svc reads to know which syscall to execute)
svc 0 @ execute the syscall
print: @ r1 => buffer to print. r2 => how many bytes to print. clobbers r0
@ print to stdout
mov r0, #1 @ Use STDOUT fd as the write fd
mov r7, #SYS_WRITE @ call write(2)
svc 0
bx lr @ return to caller
open_fail:
push {r0}
ldr r1, =OPEN_FAIL
mov r2, #31
bl print
pop {r0}
bl exit
dir_read_fail:
push {r0}
ldr r1, =DIR_READ_FAIL
mov r2, #29
bl print
pop {r0}
bl exit
fd_not_dir:
ldr r1, =FD_NOT_DIR
mov r2, #24
bl print
mov r0, #1
bl exit
fd_not_found:
ldr r1, =FD_NOT_FOUND
mov r2, #58
bl print
mov r0, #1
bl exit
open_and_read_file:
@ open file
ldr r0, =FILE @ filepath
mov r1, #0 @ flags
mov r2, #0 @ mode_t
mov r7, #SYS_OPEN
svc 0
cmp r0, #0
blt open_fail
@ read the file contents
@ r0 needs the fd, which it should already be since the call to open() set r0 to the fd.
ldr r1, =buffer @ pointer to buffer to read into
mov r2, #50 @ how many bytes to read
mov r7, #SYS_READ
svc 0
@ print the contents of the buffer
ldr r1, =buffer
mov r2, #50 @ FIXME: hardcoded badness
bl print
ldr r1, =NEWLINE
mov r2, #1
bl print
mov r0, #0
bl exit
strlen: @ r0 holds the string length. r1 should be a pointer to the start of the string.
mov r0, #0
strlen_loop:
ldrb r2, [r1, r0]
cmp r2, #0
bxle lr
add r0, r0, #1
b strlen_loop
direntry_loop:
cmp r4, r8
bxge lr
push {lr}
add r1, r5, #10 @ offset 10 bytes into the dirent struct to reach d_name.
bl strlen
mov r2, r0
bl print
ldr r1, =NEWLINE
mov r2, #1
bl print
pop {lr}
@ load the value (as 16-bits zero-extended as opposed to loading 32-bits and then extending that ourselves with UXTH)
@ in the memory address of r5 offset by 8 bytes to get us the d_reclen
ldrh r6, [r5, #8]
add r5, r5, r6 @ skip to the next dirent
add r4, r4, r6 @ accumulate how many bytes we read
b direntry_loop
@ TODO: alphabetically sort the directory entries before printing them.
_start:
ldr r0, [sp]
cmp r0, #1
ldrgt r0, [sp, #8]
ldrle r0, =FALLBACK_DIR
@ open file
@ldr r0, =FALLBACK_DIR @ filepath
mov r1, #0 @ flags
mov r2, #0 @ mode_t
mov r7, #SYS_OPEN
svc 0
cmp r0, #ENOENT
beq fd_not_found
cmp r0, #0
blt open_fail
@ read the directory contents
@ r0 needs the fd, which it should already be since the call to open() set r0 to the fd.
ldr r1, =buffer @ pointer to linux_dirent structure
@ TODO: we can allocate the required memory on the stack instead of in the bss section I think.
mov r2, #16384 @ how many bytes to read
mov r7, #SYS_GETDENTS
svc 0
cmp r0, #ENOTDIR
beq fd_not_dir
cmp r0, #0
blt dir_read_fail
mov r8, r0 @ store how many bytes the getdents syscall read, so we can use that to exit the direntry loop
mov r4, #0 @ initialize r4 to 0
ldr r5, =buffer
bl direntry_loop
mov r0, #0
bl exit
.data
FILE: .asciz "/home/illusion/asm.txt"
FALLBACK_DIR: .asciz "."
OPEN_FAIL: .ascii "Failed to open file descriptor"
DIR_READ_FAIL: .ascii "Failed to read the directory\n"
FD_NOT_DIR: .ascii "File is not a directory\n"
FD_NOT_FOUND: .ascii "Failed to open file descriptor: No such file or directory\n"
NEWLINE: .ascii "\n"
.bss
buffer:
.skip 16384