我如何从getc中获取一个角色并将其放入我的数组中?

时间:2016-02-17 18:05:18

标签: assembly arm

所以我有一个字符数组,用ascii表中的字符数索引,最多255个。

现在我有一个文件,我读完所有内容,并通过它循环,但我想在我的数组中为我读入的字符增加字符数。我似乎无法弄清楚怎么做此...

这是我的代码(完全注释):

.global main
.global printf
.global .fopen
.global .fclose
.global getc

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 getc ; Get next character from file into R0?
    CMP R0, #-1 ; Check for end of file
    BEQ endl ; Close file

    @ Write the storing in array code
    ; get index from character (getc)

    B loopFile ; Run loop again

@ Close th 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 R2, #ARRAY_MAX ; while (i < ARRAY_MAX)
    BEQ _exit ; Exit if max
    @ print?
    ; Check if value of the array at index i (i = character number) is 0, if so then skip it
    ADD R2, #0x01 ; Add 1 to i
    B printArray ; Loop next iteration of array
    */

_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
    r:     .asciz "r"
    space: .word ' '
    nl:    .word '\n'

1 个答案:

答案 0 :(得分:1)

假设基地址在R4且ASCII码在R0中,你想要做的是在地址R4 + R0*4({{1}因为每个单词都是4个字节,所以是必要的。

在拇指装配中可以这样做:

*4