Livio Livio - 8 months ago 61
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 ( with follow code

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

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"

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

Any suggestion?


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( 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

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