错误:internal_relocation - 无法找出错误

时间:2016-02-24 11:14:49

标签: assembly arm raspberry-pi2

所以我收到了这个错误:

  

program.s:62:错误:internal_relocation(类型:OFFSET_IMM)未修复

这是我的代码:

.global main
.global printf
.global fopen
.global fclose
.global fgetc

main:
    @ Open File
    PUSH {R1} @ Push address of R1 onto stack
    LDR R0, [R1, #0x04] @ Get argv[1] from stack to R0
    LDR R1, =r @ Load address of file open format (read)
    BL fopen @ Open file
    LDR R1, =fin @ Load address of file in to R1
    STR R0, [R1] @ Store contents of R1 into R0

    @ Setup array
    LDR R4, =ch_array @ Array address
    MOV R3, #0 @ Array index

    BL loopFile
    BL printArray

@ Loop through the file
loopFile:
    LDR R1, =fin @ Load R1 with address of file in
    LDR R0, [R1] @ Load R0 with pointer? of R1
    BL fgetc @ Get next character from file into R0?
    CMP R0, #-1 @ Check for end of file
    BEQ endl @ Close file

    @ get index from character (fgetc)
    /*
        Assuming the base address is in R4 (my array), the ascii is in R0
        from the file input into R0: increment the word in memory at
        the address 'R4 + (R0 * 4)'. *4 because each word is 4 bytes.
    */
    LSL R0, R0, #2
    LDR R1, [R4, R0]
    ADD R1, R1, #1
    STR R1, [R4, R0]

    B loopFile @ Run loop again

@ Close the file
endl:
    LDR R1, =fin @ Load R1 with address of file in
    LDR R0, [R1] @ Load R0 with pointer? of R1
    BL fclose @ Close the file

printArray:
    CMP R3, #ARRAY_MAX @ while (i < ARRAY_MAX)
    BEQ _exit @ Exit if max

    @ Check if value of the array at index i where
    @ (i = character number) is 0, if so then skip it
    @ else print it

    @ R3 counter of array
    @ R4 array address

    LDR R0, =printa @ Load print format
    LDR R1, R3 @ Store character value (index of array) in R1
    LDR R2, [R4, R3] @ Load count of index R3
    CMP R2, #0 @ Check if count is 0
    BEQ printArray @ If count is 0, skip
    BL printf @ Else print
    ADD R3, R3, #1 @ Increment R3 ('i')
    B printArray @ Loop next iteration of array

@ End program
_exit:
    MOV R7, #1
    MOV R0, #0
    SWI 0

.data
    .equ ARRAY_MAX, 255
    ch_array:
        .rept ARRAY_MAX @For all elements in array, repeat:
        .word 0x00 @Initialize to 0
        .endr @End repetition
    fin:   .word 0x00
    printa: .asciz "%d : %d\n"
    r:     .asciz "r"
    space: .asciz " "
    nl:    .asciz "\n"

我没有看到错误可能是什么。当我将第62行更改为:

LDR R1, [R3]

它给了我一个分段错误。有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

这不是将一个寄存器的值移动(复制)到另一个寄存器的正确方法:MOV

您应该使用MOV R1, R3指令:LDR

{{1}}指令的目的是从内存中 l oa d 数据。