在ARM Assembly中将其他值存储在内存中后,始终加载零值

时间:2013-10-14 15:28:53

标签: assembly arm

我在ARM Assembly中制作了两个子程序。一个应该在向量的第一个可用位置存储一个字节值。这个,在我的代码中称为“appendbyte”,似乎工作正常。另一个应该打印存储在这个向量中的值,但它为其中的所有元素打印0,这是不对的。

向量结构具有存储在向量的第一个地址中的元素数,并且最多必须存储255个值。

以下是完整代码:

    .text
    .global main    
    .equ    num, 255    @ Max number of elements

main:
    push    {lr}

    mov r8, #7
    bl appendbyte
    mov r8, #5
    bl appendbyte
    mov r8, #8
    bl appendbyte
    bl printvector

    pop {pc}

printvector:
    push {lr}

    ldr r4, =vet @ stores the address of the start of the vector in r4
    ldr r5, [r4], #1 @ stores the number of elements in r5

loop:
    cmp r5, #0 @if there isn't elements to print
    beq fimimprime @quit subroutine
    ldr r0, =node   @r0 receives the print format
    ldr r1, [r4], #1 @stores in r1 the value of the element pointed by r4. Increments r4 after that.
    sub r5, r5, #1 @decrements r5 (number of elements left to print)
    bl printf @call printf
    b loop @continue on the loop

endprint:
    pop {pc}

appendbyte:
    push {lr}

    ldr r4, =vet    @stores in r4 the beggining address of the vector
    ldr r5, [r4], #1    @stores in r5 the number of elements and makes r4 point to the next address
    add r6, r4, r5  @stores in r6 the address of the first available position
    str r8, [r6]    @put the value at the first available position
    ldr r4, =vet    @stores in r4 the beggining address of the vector
    add r5, r5, #1  @ increment the number of elements in the vector
    str r5, [r4]    @ stores it in the vector

    pop {pc}

.data           @ Read/write data follows
.align          @ Make sure data is aligned on 32-bit boundaries
vet:    .byte 0
    .skip   num     @ Reserve num bytes

.align
node:   .asciz "[%d]\n"

.end

我希望我对这个问题很清楚。 提前谢谢!

0 个答案:

没有答案