0

My question is about how I'm supposed to get around the use of pseudo-instructions such as la and li. Many Internet sources just use pseudo-instructions anyway, which confuses me to some extent. Part of my problem is with what is going wrong with this snippet. The simulator spits out a syntax error with the liu instruction and I don't understand what is going wrong.

From my understanding, the liu command grabs the significant bits, shifts left 16 bits, and puts that in the register ($a0). The ori instruction takes the label, and performs an OR on the lower 16 bits which is effectively adding it to the register.

       .data
m1:    .asciiz "Some string"

       .text
main:   
       lui     $a0, m1        # loads m1 into $a0 to be printed
       ori     $a0, $a0, m1

Am I not supposed to use the label m1 for liu or ori? And if I'm not meant to, what do I do instead?

I've already read this and I understand what is supposed to be happening, but I'm still not sure as to why my instructions has a different effect, and why there is a andi $a0,$a0,0x0000ffff before the liu and ori commands.

The MIPS simulator I'm using is QtSpim if that is relevant. I've gone looking for answers to this question here and elsewhere, and I've failed to understand them. Any help/corrections would be wonderful.

Community
  • 1
  • 1
SamSam
  • 13
  • 5

1 Answers1

4

The code in the referenced answer is unfortunately incorrect. It should be:

lui $a0, m1 >> 16
ori $a0, $a0, m1 & 0xffff

The only problem with that is that SPIM doesn't seem to support these operators on labels.

Anyway, the logic is that you have to split the 32 bit address into 2 halves, and load them separately. Right shifting by 16 gives you the top 16 bits (the shift will be reversed by the lui at runtime) while masking with 0xffff gives you the low 16 bits.

GNU assembler has special support functions %hi and %lo to get the high and low halves. With them you can write equivalent code as follows:

lui     $a0, %hi(m1)       # loads m1 into $a0 to be printed
ori     $a0, $a0, %lo(m1)
Jester
  • 56,577
  • 4
  • 81
  • 125
  • Thank you for the help! You've cleared up what was going wrong and explained the logic in a simple way! – SamSam May 20 '13 at 11:50