[英]How MIPS assembler acquires immediate (CONSTANT) values?
I couldn't find similar question anywhere.我在任何地方都找不到类似的问题。 For example when we want to add a predefined value in memory we get its address and copy its content but if we want to add 40000 to a register how assembler interprets 40000 and copies its value.
例如,当我们想在内存中添加一个预定义的值时,我们会得到它的地址并复制它的内容,但是如果我们想将 40000 添加到寄存器中,汇编器如何解释 40000 并复制它的值。 Thank you if help me understand this concept
谢谢你帮助我理解这个概念
The value is encoded directly into the instruction word.该值直接编码到指令字中。
For example, the instruction ADDI rs,rt,immediate
has the following encoding:例如,指令
ADDI rs,rt,immediate
具有以下编码:
001000 rs rt immediate #bits: 6 5 5 16
So the immediate constant would be placed in the 16 least-significant bits of the instruction word.因此,立即数将放置在指令字的 16 个最低有效位中。 Note that the immediate is sign-extended, so it can only encode values in the range -32768..+32767.
请注意,立即数是符号扩展的,因此它只能对 -32768..+32767 范围内的值进行编码。 So you can't add 40000 to a register with a single instruction, unless you already happen to have the value 40000 in some register.
所以你不能用一条指令将 40000 添加到寄存器中,除非你已经碰巧在某个寄存器中有值 40000。
For more information, see the document MIPS32™ Architecture For Programmers Volume II: The MIPS32™ Instruction Set .有关更多信息,请参阅文档MIPS32™ 程序员架构第二卷:MIPS32™ 指令集。
unsigned int fun0 ( void )
{
return 40000;
}
unsigned int fun1 ( void )
{
return 0x40000;
}
unsigned int fun2 ( void )
{
return 0x12345678;
}
unsigned int fun3 ( void )
{
return 0x12340000;
}
unsigned int fun4 ( void )
{
return 0x00005678;
}
Disassembly of section .text:
00000000 <fun0>:
0: 03e00008 jr $31
4: 34029c40 li $2,0x9c40 ; 0x34xxxxxx ORI $2,$0,0x9c40
00000008 <fun1>:
8: 03e00008 jr $31
c: 3c020004 lui $2,0x4
00000010 <fun2>:
10: 3c021234 lui $2,0x1234
14: 03e00008 jr $31
18: 24425678 addiu $2,$2,22136
0000001c <fun3>:
1c: 03e00008 jr $31
20: 3c021234 lui $2,0x1234
00000024 <fun4>:
24: 03e00008 jr $31
28: 24025678 li $2,22136 ; 0x24xxxxxx ADDIU $2,$0,0x5678
unsigned int fun5 ( void )
{
return 0x9999;
}
00000000 <fun5>:
0: 03e00008 jr $31
4: 34029999 li $2,0x9999 ; 0x34xxxxxx ORI $2,$0,0x9999
li is a pseudo instruction. li 是一个伪指令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.