首页 > 解决方案 > 在 Arm 汇编中为斐波那契创建一个 while 循环

问题描述

我试图让用户输入一个斐波那契术语,我使用迭代来找出给定术语的值。我想使用一个while循环来做到这一点,但是它说“startLoop”和“endLoop”是错误的指令。我对 ARM Assembly 很陌生,所以如果我的代码有什么问题,请告诉我。循环从“@ While index is less than given value”的注释开始

inputMessage: .asciz "Enter a fibonacci term: "
scanFormat: .asciz "%d"
output: .asciz "You entered: %d \n"
returnValue: .asciz "Fibonacci term: %d"

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

main:
    mov r10, lr

    @ create input value
    ldr r0, =inputMessage
    bl printf
    mov lr, r10
    
    @ recieve input from user
    add sp, sp, #4 @ Reserve space for number entry ???
    ldr r0, =scanFormat @ Run scanFormat
    mov r1, sp
    bl scanf
    mov lr, r10
    
    @ print user input
    ldr r1, [sp] @ read first number
    add sp, sp, #4 @ restore sp
    ldr r0, =output @ print output
    bl printf
    mov lr, r10
    
    @ Initialize first and second fibonacci values
    mov r2, #1
    mov r3, #1
    
    
    @ Initialize index counter
    mov r4, #0
    
    @ Initialize return value
    mov r5, #0
    
    @ While index is less than given value
    startLoop
    cmp r1, r4
    bge endLoop
        add r5, r2, r3
        mov r2, r3
        mov r3, r5
        add r4, #1
        B startLoop
    endLoop
    
    @ print fibonacci term
    ldr r5, =returnValue
    bl printf
    mov lr, r10
    
    bx lr

标签: armraspberry-pi4

解决方案


推荐阅读