为什么我的ARM汇编程序不会使用C printf打印任何内容

时间:2016-02-24 12:45:43

标签: assembly arm

所以我从gcc调用ARM中的printf函数从寄存器中打印出来。

这是我的代码:

.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
    MOV 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"

但是,屏幕上没有任何内容。没有错误或任何错误,没有分段错误。我正确地加载了这些参数,所以我似乎无法弄清楚我哪里出错!

由于

0 个答案:

没有答案