Skip to content

Instantly share code, notes, and snippets.

@hikilaka
Created September 5, 2015 02:49
Show Gist options
  • Save hikilaka/3e17513899e258399b06 to your computer and use it in GitHub Desktop.
Save hikilaka/3e17513899e258399b06 to your computer and use it in GitHub Desktop.
.data
.balign 4
zero_file: .string "/dev/zero"
.balign 4
fail_open_msg: .ascii "cannot open /dev/zero!\n"
.set fail_open_msg_sz, . - fail_open_msg
.balign 4
succ_open_msg: .ascii "successfully opened /dev/zero!\n"
.set succ_open_msg_sz, . - succ_open_msg
.balign 4
fail_mmap_msg: .ascii "mmap failed to allocate memory!\n"
.set fail_mmap_msg_sz, . - fail_mmap_msg
.balign 4
succ_mmap_msg: .ascii "mmap successfully allocated memory!\n"
.set succ_mmap_msg_sz, . - succ_mmap_msg
.text
.global _start
_start:
ldr r0, =zero_file
ldr r1, =open_rw
ldr r2, =open_rusr
ldr r7, =sys_open
mov r1, #0
cmp r0, r1
; if less then 0, print message and exit
ldrlt r0, =stdout
ldrlt r1, =fail_open_msg
ldrlt r2, =fail_open_msg_sz
ldrlt r7, =sys_write
swilt #0
blt exit
; otherwise print ok message and close file
push {r0} ; fd
ldr r0, =stdout
ldr r1, =succ_open_msg
ldr r2, =succ_open_msg_sz
ldr r7, =sys_write
swi #0
mov r0, #0
mov r1, #32 ; allocate 32 bytes
ldr r2, =mmap_prot_read
ldr r3, =mmap_prot_write
orr r2, r3
ldr r3, =mmap_private
pop {r4}
mov r5, #0
ldr r7, =sys_mmap
swi #0
push {r0} ; push mem_ptr to stack
movs r1, #-1
cmp r0, r1
ldr r0, =stdout
ldrne r1, =succ_mmap_msg
ldrne r2, =succ_mmap_msg_sz
ldreq r1, =fail_mmap_msg
ldreq r2, =fail_mmap_msg_sz
ldr r7, =sys_write
swi #0
pop {r0} ; pop mem_ptr off stack
mov r1, #32
ldr r7, =sys_munmap
swi #0
mov r0, r4
ldr r7, =sys_close
swi #0
exit:
mov r0, #0
ldr r7, =sys_exit
swi #0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment