Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how does %hi work for RISC-V 64? #63

Closed
PeterlitsZo opened this issue Feb 17, 2021 · 6 comments
Closed

how does %hi work for RISC-V 64? #63

PeterlitsZo opened this issue Feb 17, 2021 · 6 comments

Comments

@PeterlitsZo
Copy link

I know that %hi(symbol) will get the high 20 bits of the symbols' address and then be as a immediate number in RISC-V 32. In short, take the [31:12] bits in RISC-V 32.

But in RISC-V 64, will it get the [31:12] bits, or just get the [63:44] bits?

Thanks for your help.

@PeterlitsZo
Copy link
Author

BTW, if I want to test something, which tools can help me please? Which tools can run the ASM code?

@jim-wilson
Copy link
Collaborator

%hi is always bits 31:12. However it is a little more complicated than that, since we always use lui with an add, so if the low 12 bits are equal to or greater than 0x800 then we need to round the %hi value up by one, and then use a negative value for the %lo.

If you assemble and link asm code, then it can be run in any smiulator. We have spike (riscv-isa-sim) which is best for hardware folk, qemu which is best for software folk, and a gdb sim which is useful if you are familiar with it.

@PeterlitsZo
Copy link
Author

%hi is always bits 31:12. However it is a little more complicated than that, since we always use lui with an add, so if the low 12 bits are equal to or greater than 0x800 then we need to round the %hi value up by one, and then use a negative value for the %lo.

If you assemble and link asm code, then it can be run in any smiulator. We have spike (riscv-isa-sim) which is best for hardware folk, qemu which is best for software folk, and a gdb sim which is useful if you are familiar with it.

Thanks for your valuable reply! I will close this issue soon.

@PeterlitsZo
Copy link
Author

PeterlitsZo commented Feb 18, 2021

I build a test toolchain on my machine, and run:

# 0xffff_ffff_8000_0000
lui a0, %hi(0xffffffff80000000)
call print_num
-------------------------------------------
number: ffffffff80000000
# 0xffff_ffff_7000_0000
lui a0, %hi(0xffffffff70000000)
call print_num
-------------------------------------------
number: 70000000

That looks like the [63:32] bits are same as the 31 bit in function %hi, and do you know why it works like this please? Thanks.

@johnwinans
Copy link

Because the immediate operand in the lui instruction is sign-extended to XLEN before it is stored into rd. Note this is true for every instruction that has an imm field.

@PeterlitsZo
Copy link
Author

Thanks for your kindly help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants