I am trying to follow an example that I found on a blog.
It doesn't seem that androideabi can call printf directly on assembler code level (maybe I am missing a flag when I compile?).
I am running with these commands:
arm-linux-androideabi-as -o fib.o fibonacci.s
arm-linux-androideabi-ld --sysroot $env:SYSROOT -s -o fib fib.o
fib.o(.text+0x8): error: undefined reference to 'printf'
I get an undefined reference from the assembly code.
This would be neat if it actually pulled off. I am of course open to alternative solution if anyone wants to share their awesome knowledge of low level programming!
Any tips for this problem? Code is below:
.syntax unified
.equ maxfib,4000000
previous .req r4
current .req r5
next .req r6
sum .req r7
max .req r8
tmp .req r9
.section .rodata
.align 2
fibstring:
.asciz "fib is %d\n"
sumstring:
.asciz "%d\n"
len = . - sumstring
.text
.align 2
.global main
.extern printf
.type main, %function
main:
stmfd sp!, {r4-r9, lr}
ldr max, =maxfib
mov previous, 1
mov current, 1
mov sum, 0
loop:
cmp current, max
bgt last
add next, current, previous
movs tmp, current, lsr 1 @ set carry flag from lsr - for the odd-valued terms
@ we discard the result of the movs and are only interested
@ in the side effect of the lsr which pushes the lower bit
@ of current (1 for odd; 0 for even) into the carry flag
@ movs will update the status register (c.f. mov which will not)
addcc sum, sum, current @ we add current to the sum ONLY when cc (carry clear) is true
@ these are even-valued fibonacci terms
mov previous, current
mov current, next
b loop
last:
mov r1, sum
ldr r0, =sumstring @ store address of start of string to r0
bl printf
mov r0, 0
ldmfd sp!, {r4-r9, pc}
mov r7, 1 @ set r7 to 1 - the syscall for exit
swi 0 @ then invoke the syscall from linux
You need to link to the C runtime library aka libc, in order to get printf
. Either you can add -lc
at the end of your linking command, or use the gcc
frontend instead of using ld
directly.
I.e., either do this:
arm-linux-androideabi-ld --sysroot $env:SYSROOT -s -o fib fib.o -lc
Or this:
arm-linux-androideabi-gcc --sysroot $env:SYSROOT -Wl,-s -o fib fib.o