Sam
Sam

Reputation: 31

Mips assembly help? Specifically with Hi/Lo

I'm trying to dechiper this piece of MIPS assembly code. However, I am still unsure of what the add/load operations with %hi(A) / %lo(A) do. I have commented on what I understand...

                # 28($fp) is zero
lw  $2,28($fp)  # $2 = 0
sll $3,$2,1     # $3 = 0*2^1 = 0 (multipying with 2^1)
lui $2,%hi(A)   # ??
lw  $4,28($fp)  # $4 = 0
sll $4,$4,2     # $4 - 0*2^1 (not sure of the purpose yet)
addiu   $2,$2,%lo(A) #?? 
addu    $2,$4,$2     # $2 += $4
sw  $3,0($2)         # save $3 in memory location 0
lw  $2,28($fp)       # $2 = 0
addiu   $2,$2,1      # $2 = 1
sw  $2,28($fp)       # MEM 28($fp) = 3

I would appreciate it if somebody could look over the comments and help me out with this. I've looked around but have not yet found an answer I understand. I am especially confused by what Hi / Lo do.

Upvotes: 2

Views: 732

Answers (1)

Jester
Jester

Reputation: 58772

%hi gives you the top 16 bits, %lo the low 16 bits1. This is needed because you can't load a 32 bit immediate in one go, so you have to do it in two halves. This loading has been intermixed with other code here, the parts that belong together are:

lui $2,%hi(A)   # ??
addiu   $2,$2,%lo(A) #?? 

These two just load address of A into $2. lui loads the top 16 bits, then adds the low 16. In source code you normally use the la pseudoinstruction, e.g. la $2, A

sll $4,$4,2     # $4 - 0*2^1 (not sure of the purpose yet)

This is calculating the offset in the array based on an index. Presumably it's an array of 32 bit integers, hence the shift by 2, which is actually a multplication by 2^2=4 and not 2^1 as your comment says.


1 Due to sign extension, technically that's not always true, but the values will properly add up to the required address.

Upvotes: 4

Related Questions