1

我想以 BYTE(8 位)或 HALF WORD(16 位)为单位操作内存加载或存储。

所以我制作了如下的C代码并使用“riscv32-unknown-elf-gcc”编译它。

void fun3 (void) {
    char i,j;
    i = (char)*(volatile unsigned int *) 0xf10007f8;
    j = (char)*(volatile unsigned int *) 0xf10007f4;
    *(volatile unsigned int *) 0xf10007f0 = (char) i+j;
    *(volatile unsigned int *) 0xf10007fc = (int ) 0x4;
}

我编译如下,函数的de-asm代码如下。

riscv32-unknown-elf-gcc -march=rv32im -mabi=ilp32 -nostartfiles -nostdlib -Os   -x c  -Wl,-T,/work/test5.x -o test5.o test5.c
riscv32-unknown-elf-objdump -d -t test5.o > test5_Os.dump


f100006c <fun3>:
f100006c:   f1000737            lui a4,0xf1000
f1000070:   7f872783            lw  a5,2040(a4) # f10007f8 <__global_pointer$+0xffffeef0>
f1000074:   7f472683            lw  a3,2036(a4)
f1000078:   0ff7f793            andi    a5,a5,255
f100007c:   0ff6f693            andi    a3,a3,255
f1000080:   00d787b3            add a5,a5,a3
f1000084:   7ef72823            sw  a5,2032(a4)
f1000088:   00400793            li  a5,4
f100008c:   7ef72e23            sw  a5,2044(a4)
f1000090:   00008067            ret

我想在上面的汇编代码中将“lw”和“sw”转换为“lb”和“sb”。

如果你知道,请回答。

4

1 回答 1

2

如果您更换:

i = (char)*(volatile unsigned int *) 0xf10007f8;
j = (char)*(volatile unsigned int *) 0xf10007f4;
*(volatile unsigned int *) 0xf10007f0 = (char) i+j;

和 :

i = *(char*) 0xf10007f8;
j = *(char*) 0xf10007f4;
*(char *) 0xf10007f0 = i+j;

编译器将生成加载和存储字节。
事实是您的代码告诉他加载一个单词,因为您使用的是无符号 int 指针,并将结果转换为 char 所以他将使用lw. 商店也是如此,您使用的是无符号整数指针。

于 2020-11-26T07:46:42.443 回答