斐波那契数组

时间:2017-03-24 01:12:11

标签: arrays assembly arm fibonacci

我试图将Fibonacci序列的前10个数字存储在一个数组中,然后使用用户输入在输入的索引中打印序列的值。我不确定我的Fibonacci循环是不正确的,还是我不知道如何搜索/打印索引。

我的代码:

.data
        .balign 4
        output: .asciz "%d\n"
        select: .asciz "Input an index to check:"
        a:      .skip 40
.text
        .global main
        .extern printf
        .extern scanf

main:
        push    {ip, lr}

        ldr     r7, =a          @ Array a to r7
        mov     r8, #3          @ count
        mov     r9, #0          @ hard code first value
        mov     r10,#1          @ hard code second value

        str     r9, [r7, #1]    @ store 0 in array
        str     r10,[r7, #2]    @ store 1 in array

fib:
        cmp     r8, #10         @ Is count equal to 10?
        beq     end             @ if 10 elements exit writeloop

        mov     r11, r10
        add     r10, r9, r10
        mov     r9, r11

        str     r10, [r7, r8]
        add     r8, r8, #1      @ increment count

        b       writeloop       @ continue the loop

end:
        ldr     r0, =select     @ Ask for index to check
        bl      printf          @ C printf
        ldr     r0, =output
        ldr     r1, =select
        bl      scanf
        ldr     r1, =select
        ldr     r1, [r1]
        ldr     r1, [r7, r1, lsl #2]
        ldr     r0, =output
        bl      printf

        pop     {ip, pc}

当我接受输入时,由于某种原因,在我输入两个值之前它不会计算。例如,如果我想检查位置1,它将询问我的输入,直到我输入一个值并按两次输入它才会运行。

3 [Enter] 
3 [Enter]

然后它给了我错误的答案。我无法弄清楚我做错了什么。

1 个答案:

答案 0 :(得分:0)

一些仓促的代码添加到您的代码中以灌注泵。 我更喜欢将scanf和printf调用放入单独的函数中。 假设13元素Fib序列。 假设字节大小元素。 对于较大的Fib值,您需要字大小元素。 调试器也可以帮助您 - 在近期和长期。

/*
        David @InfinitelyManic & Absiel
        http://stackoverflow.com/questions/42989732/fibonacci-array
*/
.bss
        array:  .byte 13
.data
        fmt:    .asciz "Input an index to check:\n"
        fmt1:   .asciz "The Fibonacci number at index %d is %d:\n"
        _data:  .string "%d"
        .align
.text
        .global main

main:
        // generate Fibonacci array
        ldr r7,=array           // pointer to array

        mov r9, #0              // init first value
        mov r10,#1              // init second value
        strb r9, [r7, #0]       // store 0 in array
        strb r10,[r7, #1]       // store 1 in array

        mov r8, #2              // init counter at 3rd element position  == 2
fib:
        mov r11, r10            // save 1
        add r10, r9, r10        // 0 + 1...
        mov r9, r11             // new

        strb r10, [r7, r8]      // store in array
        add r8, r8, #(1 * 1)    // inc counter
        cmp r8, #(13 * 1)       // max fib sequence count
        ble fib

_end:   // end building array


        bl _write               // ask question
        bl _input               // get keyboard value

        mov r1, r0              // save scanf input for printf
        ldrb r2, [r7, r0]       // get val at index; save in r2 for printf

        bl _write1              // output

_exit:
        mov r7, #1
        svc 0
_write:
        push {lr}
        ldr r0,=fmt
        bl printf
        pop {pc}

_write1:
        push {lr}
        ldr r0,=fmt1
        bl printf
        pop {pc}

_input:
        push {lr}
        sub sp, sp, #8
        ldr r0,=_data
        mov r1, sp
        bl scanf
        ldr r0, [sp]
        add sp, sp, #8
        pop {pc}
.align
.end

输出:

pi@raspberrypi:~/asm $ ./fibonacci
Input an index to check:
0
The Fibonacci number at index 0 is 0:
pi@raspberrypi:~/asm $ ./fibonacci
Input an index to check:
1
The Fibonacci number at index 1 is 1:
pi@raspberrypi:~/asm $ ./fibonacci
Input an index to check:
2
The Fibonacci number at index 2 is 1:
pi@raspberrypi:~/asm $ ./fibonacci
Input an index to check:
5
The Fibonacci number at index 5 is 5:
pi@raspberrypi:~/asm $ ./fibonacci
Input an index to check:
9
The Fibonacci number at index 9 is 34:
pi@raspberrypi:~/asm $ ./fibonacci
Input an index to check:
10
The Fibonacci number at index 10 is 55:
pi@raspberrypi:~/asm $ ./fibonacci
Input an index to check:
13
The Fibonacci number at index 13 is 233: