riscv-non-isa / riscv-asm-manual

RISC-V Assembly Programmer's Manual
https://jira.riscv.org/browse/RVG-4
Creative Commons Attribution 4.0 International
1.44k stars 238 forks source link

Is the "Load Immediate" example accurate? #29

Closed WRansohoff closed 4 years ago

WRansohoff commented 4 years ago

I might be doing this math wrong, but I think that maybe the "Load Immediate" example should use 0x33 as its LUI immediate instead of 0x32:

0000000000000000 <_start>:
   0:   00033537            lui     a0,0x33
   4:   bfb50513            addi    a0,a0,-1029
   8:   00e51513            slli    a0,a0,0xe
   c:   abe50513            addi    a0,a0,-1346

Instead of:

0000000000000000 <_start>:
   0:   00032537            lui     a0,0x32
   4:   bfb50513            addi    a0,a0,-1029
   8:   00e51513            slli    a0,a0,0xe
   c:   abe50513            addi    a0,a0,-1346

When I do the math with 0x32, I get 0xC6FEBABE, and I get the same result when I run ( ( ( ( 0x32 << 12 ) - 1029 ) << 0xE ) - 1346 ) in Python. But it's possible that I am misunderstanding how the sign-extension or LUI instruction works. Sorry that I can't verify this with an objdump, but this is what I get when I build the corresponding test code with GCC:

00000000 <_start>:
   0:   cafec537            lui a0,0xcafec
   4:   abe50513            addi    a0,a0,-1346 # cafebabe <CONSTANT+0x0>

(Compiled with: riscv32-unknown-elf-gcc -x assembler-with-cpp -c -march=rv32i -o0 test.S -o test

Also, presented without judgement: I've gotten a few odd looks today for having CAFEBABE scribbled all over my notes when people come by to talk. Is it possible that something like FEEDFACE or DEC0FFEE might work just as well?

aswaterman commented 4 years ago

The example is clearly incorrect for the reason you pointed out.

I also agree we should dispense with the sexually charged choice of constant. I've rectified both problems with 219a2b2ee359e6feaab69033f228c528e800b8bd

WRansohoff commented 4 years ago

Thank you! Appreciate the clarification