Skip to content

Instantly share code, notes, and snippets.

@cho45
Last active August 29, 2015 13:57
Show Gist options
  • Save cho45/9584210 to your computer and use it in GitHub Desktop.
Save cho45/9584210 to your computer and use it in GitHub Desktop.
/*#!as --gstabs+ -o ls.o ls.s && ld -o ls -e _start ls.o && objdump -d -j .text -j .data ls && ./ls
*/
.global _start
.macro sys_exit
mov r7, $0x01 /* set system call number to 1 (exit) */
svc $0x00 /* supervisor call */
.endm
O_RDONLY = 0x0000
.macro sys_open
mov r7, $0x05
svc $0x00 /* supervisor call */
.endm
.macro sys_write
mov r7, $0x04
svc $0x00 /* supervisor call */
.endm
.macro sys_close
mov r7, $0x06
svc $0x00 /* supervisor call */
.endm
.macro sys_getdents
mov r7, $0x8d
svc $0x00 /* supervisor call */
.endm
.section .text
_start:
bl main
/* not reached */
mov r0, $0xff
sys_exit
main:
/* open */
ldr r0, =current_dir
mov r1, #O_RDONLY
sys_open
cmp r0, $0x00
rsble r0, r0, #0
blle error
mov v1, r0
1:
/* getdents */
mov r0, v1
ldr r1, =dentry_buffer
mov r2, #dentry_buffer_len
sys_getdents
cmp r0, $0x00
beq 2f
mov v2, r0 /* read bytes */
rsblt r0, r0, #0
bllt error
ldr v3, =dentry_buffer
3:
ldrh v5, [v3, #8] /* linux_dirent d_reclen */
mov r0, $0x00
add r1, v3, #10
sub r2, v5, #12
sys_write
mov r0, $0x0a
push {r0}
mov r0, $0x00
mov r1, sp
mov r2, #1
sys_write
pop {r0}
sub v2, v2, v5 /* len -= d_reclen */
add v3, v3, v5 /* buffer += d_reclen */
cmp v2, $0x00
bne 3b
b 1b
2:
/* close */
mov r0, v1
sys_close
mov r0, $0x00 /* set exit status to 0 */
sys_exit
current_dir:
.asciz "."
.align 2
error:
cmp r0, $0x00
moveq r0, $0x01
sys_exit
.section .bss
.align 2
buffer: .skip 4096
dentry_buffer:
.skip 4096
dentry_buffer_len = . - dentry_buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment