Livio Livio - 3 months ago 29
Android Question

Read binary file with ARM assembly

I would you like write a simple program that open a binary file and read every byte. I try to do this using an Android device with this syscalls (https://android.googlesource.com/platform/bionic/+/cd58770/libc/SYSCALLS.TXT) with follow code

...
ldr r0, =binaryfile
mov r1, #2
mov r7, #5
svc #0

loop:
ldr r0,=Handle
ldr r0,[r0]
ldr R1,=Array
mov R2,#80
mov r7,#3 @read syscall
svc #0
...
Handle: .skip 4
Array: .skip 80
binaryfile: .asciz "file.bin"
.end


But after the open syscall, the value of Handle is always 0xfffffffe

Any suggestion?

Answer

Your code is not completely correct, you must add ldr r0,=Handle out of the loop (as suggest by Peter) and you have also to review the parameters of the open syscall(http://man7.org/linux/man-pages/man2/open.2.html). This is a working example:

...
@ Open an input file for reading
ldr r0,=binaryfile      @ set Name for input file
mov r1,#0
ldr r2,=0666            @ permissions
mov r7,#5
swi 0 

@ Save the file handle in memory:
ldr r1,=Handle     @  load input file handle
str r0,[r1]              @  save the file handle


loop:
ldr r0,=Handle           @ load input file handle
ldr r0,[r0]
ldr r1,=readBuffer
mov r2,#16 
mov r7, #3
swi 0                    @ read the integer into R0
...
Comments