需要帮助理解ARM程序集计数代码

时间:2016-03-02 07:37:42

标签: assembly arm

我在业余时间编码,它帮助我通过反复试验来理解编程。我最近遇到了这个手臂汇编代码,它有评论,但我似乎没有正确理解它。此代码计算终端上的字符数和显示数。有人可以解释一下吗?

    .global main
main:
    LDR R0, [R1, #4]        @ get argv[1]
    LDR R1, =r              @ set mode to 'r' read
    BL fopen                @ call fopen
    LDR R1, =fin            @ get var address for fin
    STR R0, [R1]            @ store result of fopen in fin
loop:                       @ loop until EOF
    LDR R0, =fin            @ get fin
    LDR R0, [R0]            @ get what's stored at fin (FILE *)
    BL getc                 @ get next char in fin
    CMP R0, #-1             @ if the char is EOF, exit loop
    BEQ printarr
    MOV R2, #4           @ 4 is size of 1 word
    MUL R1, R0, R2          @ get the array offset
    LDR R3, =array          @ store start addr of array
    ADD R3, R1              @ get adrr of array + offset
    LDR R4, [R3]            @ get the current value stored
    ADD R4, #1           @ increment that value
    STR R4, [R3]            @ store inceremented value
    B loop
printarr:
    MOV R5, #0           @ set array counter to 0
loop2:
    LDR R4, =array          @ store begining of array at R4
    CMP R5, #256            @ check if counter is 256
    BEQ exit                @ if it is, exit
    MOV R6, #4
    MUL R6, R5              @ get array offset
    ADD R6, R4              @ address of offset
    LDR R6, [R6]            @ value at offset
    CMP R6, #0           @ check if value at offset is 0
    BEQ loop2end            @ if it is, skip the printing
    LDR R0, =f1             @ store the format string at R0 (first arg)
    MOV R1, R5              @ store the counter at R1+R2 (second & third args)
    MOV R2, R1
    MOV R3, R6              @ store value at offset in R3 (fourth args)
    PUSH {R5}               @ save the counter
    BL printf               @ call printf with args R0,R1,R2,R3
    POP {R5}                @ retrieve counter
loop2end:
    ADD R5, #1           @ increment counter
    B loop2                 @ loop around again
exit:
    LDR R0, =fin
    LDR R0, [R0]
    BL fclose
    MOV R0, #0
    MOV R7, #1
    SWI 0
.data
fin: .word 0
r: .asciz "r"
f1: .asciz "ASCII %3d - {%c}: %d\n"
array:  .rept 256
        .word 0
        .endr

0 个答案:

没有答案