0

So, in this code I take user input for a specific term in the fibonacci series. And I want to output the number. I made a forloop and attempted to print the result. However the error it gives is "collect2: error: ld returned 1 exit status". Does not execute. Also there are little resources for this assembly syntax, so I can't really learn it well.

Code:

.cpu cortex-a53
.fpu neon-fp-armv8

.data

string: .asciz "Enter a Fibonacci term: "
scan: .asciz "%d"
result: .asciz "The %dth Fibonacci number is: %d\n"

.text
.align 2
.global main
.type main, %function

main:

push {fp, lr} @pushes on to stack
add fp, sp, #4 @adds the bytes to the stack

ldr r0, =string @loads string prompt into r0
bl printf @prints the string
sub sp, sp, #4 @allocates memory for sp
ldr r0, =scan @loads scan into r0
mov r1, sp @sp is moved to r1
bl scanf @branches to scanf

add sp, sp, #4 @creates bytes on stack

forloop:

cmp r0, #0 @compares user input to 0
bgt myexit @closes program if input is 0
mov r2, #0 @stores first term of fib
mov r3, #1 @stores second term of fib
add r2, r2, r3 @adds r2 & r3 then overwrites r2
bl forloop

ldr r6, =result
bl printf
Michael
  • 57,169
  • 9
  • 80
  • 125
Valskarx
  • 25
  • 6
  • 1
    Since you're getting a linker error you should post the commands you run for assembling and linking. – Michael Oct 07 '19 at 18:57
  • I use the linux terminal, go into the directory, find the folder. Then I type make. – Valskarx Oct 07 '19 at 19:01
  • The problem should be printed before the final error. Presumably some symbol not found, e.g. `printf`. That's probably because you forgot to link against libc. Please post the commands and all error messages and the makefile if needed. – Jester Oct 07 '19 at 19:10
  • `bl` is branch-and-link. You don't want that for your loop branch, just a normal `b`. (That's not your bug, though.) Looks like you aren't passing args to `printf`. Also, your loop body makes no sense; the initializers are inside the loop. But anyway, this isn't a [mcve] of the error you're getting, and doesn't show what commands `make` ran, or the Makefile. If you're building with GCC, maybe it's trying to link into a PIE executable so `-no-pie` might help to get the linker to make a PLT for you. – Peter Cordes Oct 07 '19 at 19:20
  • "little resources"? There's stuff all over the place for ARM assembly. Google `arm assembly`. The first hit in my results looks decent, https://azeria-labs.com/writing-arm-assembly-part-1/ – Peter Cordes Oct 07 '19 at 19:23
  • No there are specific versions of ARM. And online I can't seem to find any matching this. I'm here for a reason. – Valskarx Oct 07 '19 at 19:43
  • This looks like GAS syntax for 32-bit ARM. https://sourceware.org/binutils/docs/as/, for Linux(?) with libc functions. You're not actually *using* any cortex-a53 or FPU instructions like `ldrd` to load a pair of regs, just push/pop and other baseline ARM instructions that have existed since forever. And your code would assemble fine in thumb or ARM mode, I think, using unified syntax. – Peter Cordes Oct 07 '19 at 20:29

0 Answers0