简体   繁体   中英

In MIPS/SPIM, whats the difference between li and lw?

After realizing how lost I am with assembly language and using MIPS, I decided to start from the basics and actually understand it.

Obviously MIPS code has specific purposes, but a lot of the stuff seems to do similar things and I'm having a hard time understanding some of the differences.

What is the difference between loading immediate.. (li) and loading a word.. (lw)? I'm not even sure what a "word" is. Or what the following does:

li $t0,y
lw $t0,0($t0)

Is it loading y into the register $t0 in the first line? And then loading 0($t0) as y?

Sorry, this is probably a really dumb question. If anyone could just explain what those two lines are doing and the difference between li/lw, i'd greatly appreciate it. Thanks!

A word is a fixed-length sequence of bits.
On MIPS32, a word is 32 bits wide.
The instruction lw $regA, offset($regB) loads a word from the memory location specified by offset($regB) into register regA .

By contrast, li reg, immediate is not a true instruction . No MIPS cpu can execute li . It's a pseudo-instruction that is turned into a sequence of two instructions by the assembler:

lui $reg, [most significant 16 bits of immediate]
ori $regA, $regA, [least significant 16 bits of immediate]

lui loads the upper 16 bits of a 32-bit register with an immediate constant from the instruction, setting the lower 16 bits to zero.
ori takes the content of $regA , does a bitwise logical OR with the 16 bit immediate from the instruction, and stores the result back into $regA .

So, to sum up: li $reg, immediate will always put the same immediate into a register, while lw $regA, offset($regB) will load the current value from the memory location offset($regB) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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