0

我正在 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

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

4

0 回答 0