printf的寄存器改变数值,ARM汇编 [英] Printf Change values in registers, ARM Assembly

查看:338
本文介绍了printf的寄存器改变数值,ARM汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来装配编程,我编程为ARM。
我做节目有两个子程序:一个附加在内存中的字节向量字节的信息,以及一个打印这个载体。向量的第一个地址包含以下,最高可达255当​​我用GDB调试它,我可以看到appendbyte子程序工作正常元素的数量。但是,当涉及到printvector之一,也存在一些问题。首先,在寄存器r1加载的元素是错误的(它加载0,当它应该是7)。然后,当我阅读GDB寄存器值后,我使用的printf的功能,很多寄存器的获取不应该更改其他值,因为我并没有修改它们,我只是用printf上。为什么的printfmodyfing的值。

I'm new to assembly programing and I'm programing for ARM. I'm making a program with two subroutines: one that appends a byte info on a byte vector in memory, and one that prints this vector. The first address of the vector contains the number of elements that follows, up to 255. As I debug it with GDB, I can see that the "appendbyte" subroutine works fine. But when it comes to the "printvector" one, there are some problems. First, the element loaded in register r1 is wrong (it loads 0, when it should be 7). Then, when I read the registers values with GDB after I use the "printf" function, a lot of register get other values that weren't supposed to change, since I didn't modify them, I just used "printf". Why is "printf" modyfing the values.

我在想一些关于对齐。我不知道我是否使用正确的指令。

I was thinking something about the align. I'm not sure if i'm using the directive correctly.

下面是完整的code:

Here is the full code:

    .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 imprime

    pop {pc}

.text
.align  

printvector:
    push {lr}

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

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

.align  
endprint:
    pop {pc}

.align  
appendbyte:
    push {lr}

    ldr r0, =vet    @stores in r0 the beggining address of the vector
    ldr r1, [r0], #1    @stores in r1 the number of elements and makes r0 point to the next address
    add r3, r0, r1  @stores in r3 the address of the first available position
    str r8, [r3]    @put the value at the first available position
    ldr r0, =vet    @stores in r0 the beggining address of the vector
    add r1, r1, #1  @ increment the number of elements in the vector
    str r1, [r0]    @ 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

的问题是在

    ldr r1, [r3], #1

    bl printf

我希望我是对这个问题不清楚。
在此先感谢!

I hope I was clear on the problem. Thanks in advance!

推荐答案

在ARM ABI指定的寄存器r0-R3和R12被认为是对函数调用波动。这意味着被叫方没有恢复自己的价值。 LR也改变了,如果你使用BL,因为LR然后将包含返回地址调用的函数。

The ARM ABI specifies that registers r0-r3 and r12 are to be considered volatile on function calls. Meaning that the callee does not have to restore their value. LR also changes if you use bl, because LR will then contain the return address for the called function.

更多的信息可以对武器信息中心条目的的 ABI 或在的 APCS (ARM过程调用标准)的文件。

More information can be found on ARMs Information Center entry for the ABI or in the APCS (ARM Procedure Call Standard) document.

这篇关于printf的寄存器改变数值,ARM汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆