简体   繁体   English

MIPS 中操作码中的 la 和 li 有什么区别?

[英]What is the difference between la and li in opcodes in MIPS?

For example in this code :例如在这段代码中:

#display message
li $v0, 4
la $a0, Message


#promt user to enter name
li $v0, 8 
la $10, username
li $a1, 20
syscall

#display the name
li $v0, 4
la $a0, userName
syscall

I am very confused about what li (load immediate) and la (load address) really mean.我对li (立即加载)和la (加载地址)的真正含义感到非常困惑。

They're fairly similar, as both are (mostly) used for loading immediate values.它们非常相似,因为两者(大部分)都用于加载即时值。 Both of them are also pseudo-instructions , so it's really up to each assembler that supports them to determine exactly how they should function.它们都是伪指令,因此真正由支持它们的每个汇编程序来确定它们应该如何运行。


li stands for Load Immediate and is a convenient way of loading an immediate up to 32 bits in size. li代表立即加载,是加载最大 32 位大小的立即数的便捷方式。 Instructions like addi and ori can only encode 16-bit immediates, so the assembler may translate li into multiple instructions. addiori等指令只能编码 16 位立即数,因此汇编程序可能会将li翻译成多条指令。

For example, li $t0,0x12345678 might become:例如, li $t0,0x12345678可能会变成:

lui $at, 0x1234 
ori $t0, $at, 0x5678        

So it's just a way to save you from writing those two instructions, and instead letting the assembler working that out for you.因此,这只是一种避免您编写这两条指令的方法,而是让汇编程序为您解决这个问题。

There's really no reason why eg li $t0, Message wouldn't be supported, since labels are also immediates, but some assemblers might not accept labels for li .真的没有理由不支持例如li $t0, Message ,因为标签也是立即数,但一些汇编程序可能不接受li标签。


la stands for Load Address. la代表加载地址。 It can be used to load integer constants just like li , eg la $t0,0x1234678 .它可以像li一样用于加载整数常量,例如la $t0,0x1234678 But it also works with labels: la $t0, Message # t0 = address of Message .但它也适用于标签: la $t0, Message # t0 = address of Message
Some assemblers may also allow you to do things like la $t0, 8($t1) # t0 = t1 + 8 .一些汇编程序可能还允许您执行诸如la $t0, 8($t1) # t0 = t1 + 8


When you'd use li and when you'd use la depends on the context.何时使用li何时使用la取决于上下文。 If the value you're loading is going to be used as an address you would typically use la to load it, and otherwise you'd typically use li .如果您要加载的值将用作地址,您通常会使用la加载它,否则您通常会使用li Since they are partially interchangable it's really up to you, but other people might find your code strange-looking if you use la all the time to load integer constants.由于它们是部分可互换的,这完全取决于您,但是如果您一直使用la加载整数常量,其他人可能会发现您的代码看起来很奇怪。

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

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