简体   繁体   English

将值从寄存器存储到 memory、ARM 32 位的问题

[英]Problem to store value ​from register into memory, ARM 32 bit

I am new in ARM and assembler.我是 ARM 和汇编程序的新手。 I tried to write a simple program to store value from register to memory.我试图编写一个简单的程序来将值从寄存器存储到 memory。

string:
        .ascii "%d\012\000"
        .align 2

var1:
        .word 0
        .align 4

.text
.global main

main:
        push {ip, lr}
        ldr r1, adr_var1
        ldrb r1, [r1]
        mov r1, #370
        uxtb r3, r1
        ldr r1, adr_var1
        strb r3, [r1]
        ldr r0, adr_string
        mov r1, r3
        bl printf
        mov r1, #0
        mov r7, #1
        pop {ip, pc}

adr_var1:
        .word var1

adr_string:
        .word string

The problem occurs when writing data to memory.将数据写入 memory 时出现此问题。 When it tries to write the value 370 (hex: 0x172), only 0x72 is saved.当它尝试写入值 370(十六进制:0x172)时,仅保存 0x72。 STR seems to only transfer 8 data bits. STR 似乎只传输 8 个数据位。 I have tried different configurations with the STR instruction (eg STRB) and nothing works.我用 STR 指令(例如,STRB)尝试了不同的配置,但没有任何效果。 My question is how can store this value to memory.我的问题是如何将此值存储到 memory。

Thank you for any help and answer.感谢您的任何帮助和回答。

strb r3, [r1] is a byte store. strb r3, [r1]是一个字节存储。 Of course it only stores one byte.当然它只存储一个字节。

uxtb r3, r1 zero-extends a byte into a register so a str r3, [r1] word store would store 4 bytes, with the high 3 bytes all zeros. uxtb r3, r1将一个字节零扩展到寄存器中,因此str r3, [r1]字存储将存储 4 个字节,高 3 个字节全为零。 Try it with the initial value in memory being 0xFFFFFFFF so you can see the difference between storing a byte and storing a zero-extended word.尝试将 memory 中的初始值设为0xFFFFFFFF ,这样您就可以看到存储字节和存储零扩展字之间的区别。

If you want to store the full 370 , simply don't truncate it to 8 bits before storing!如果您想存储完整的370 ,只需在存储之前不要将其截断为 8 位!

Also, you should put your .align before the var1: label (and the .word 0 ).此外,您应该将.align放在var1: label (和.word 0 )。 .align expands to padding at its location; .align在其位置扩展为填充; If you want var1 to be aligned so you have to get to an alignment boundary first .如果您希望var1对齐,那么您必须首先到达 alignment 边界。


Also, use a debugger to examine registers and memory as you single step.此外,使用调试器检查寄存器和 memory 作为单步操作。 It would have been obvious that your 370 got truncated to (uint8_t)370 by uxtb (unsigned extend byte).很明显,您的370uxtb (无符号扩展字节)截断为(uint8_t)370

Plus, your code doesn't print the word from memory, it only passes r3 to printf.另外,您的代码不会打印 memory 中的单词,它只会将r3传递给 printf。 So you couldn't see the difference between a byte store leaving upper bytes unmodified vs. a word store with that buggy debug-print.因此,您看不到保留高位字节未修改的字节存储与带有错误调试打印的字存储之间的区别。 Using a debugger is vastly better.使用调试器要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM