简体   繁体   English

小立即在 risc-v 中的 lui

[英]lui in risc-v with small immediate

For a risc-v instruction like lui x28 123 , venus shows that its machine code is 0x0007BE37 .对于像lui x28 123这样的 risc-v 指令,venus 显示它的机器码是0x0007BE37 However lui should load only upper 20 bits of immediate 123 .但是lui应该只加载立即数123的高20位。 Since in hex 123 is 0x0000007B , I think the corresponding machine code should be 0x00000E37 .由于十六进制1230x0000007B ,我认为相应的机器码应该是0x00000E37

I am puzzled why venus produces different results, is there a problem with my understanding of lui ?我很奇怪为什么 venus 会产生不同的结果,我对lui的理解有问题吗?

When you provide a numeric value as a literal to an instruction like LUI , then some assemblers will take that as the exact immediate value you want encoded in the instruction.当您将数值作为文字提供给LUI之类的指令时,一些汇编程序会将其作为您希望在指令中编码的确切立即值。 This is important and useful for compilers generating assembly or assembly programmers who want to use LUI and specify the exact bit pattern for the 20 bits in that instruction, without any interference from the assembler.这对于生成汇编程序的编译器或想要使用LUI并为该指令中的 20 位指定确切位模式而不受汇编程序任何干扰的汇编程序员来说非常重要且有用。

If you want it to take only the high 20 bits of your literal, there's compile time functions 1 like %hi and %lo that some assemblers will accept, eg lui %hi(123) — the %hi is for use in lui and the %lo for use in an instruction that wants the 12 low bits, like addi and sw / lw .如果您希望它只占用文字的高 20 位,则有一些汇编程序会接受的编译时函数1 ,例如%hi%lo ,例如lui %hi(123)%hi用于lui%lo用于需要 12 个低位的指令,例如addisw / lw

1 These may be evaluated as late as link time. 1这些可能会在链接时进行评估。


These functions, ( %hi & %lo ) are complicated due to the traditional choice of pairing with instructions like lw and addi , wherein their immediate makes up the 12-bit rest, but itself is signed .由于传统选择与lwaddi等指令配对,这些函数( %hi%lo )很复杂,其中它们的立即数构成 12 位 rest,但本身是有符号的 So, the %hi function offsets the value by 1 in the specific cases where the lw 's offset will appear negative and will sign extend with 1's at runtime.因此, %hi function 在特定情况下将值偏移 1,其中lw的偏移量将显示为负数,并且在运行时将符号扩展为 1。 So, when these are paired 2 the environment makes things work, but if you were to use %hi() in LUI on a random constant without such pairing, you might not get the exact 20 bits you're expecting.因此,当它们配对2时,环境会使事情正常进行,但是如果您在没有这种配对的情况下在随机常数上使用LUI中的%hi() ,您可能无法获得您期望的确切 20 位。

2 More than one %lo can ~pair with the same %hi as long as the function arguments match. 2只要 function arguments 匹配,多个%lo可以与相同的%hi配对。 So, for example, a read/modify/write sequence to a global variable might have one lui %hi() for both lw %lo() and sw %lo() .因此,例如,对全局变量的读/修改/写序列可能对lw %lo()sw %lo()都有一个lui %hi() ) 。

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

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