Avishai Fuss
Avishai Fuss

Reputation: 73

Accessing bytes in array MIPS

In MIPS, I have created an array using .byte that is initialized with values.

array: .byte 1,2,3,4,5,6,7,8,9

those values are stored in memory as 8 bit integers, for example:

0x04030201

How can I access the individual values in order to sum the integers? Is using a bit mask the only way? Is there an easier way to do it?

Upvotes: 2

Views: 4732

Answers (1)

Taylor Hansen
Taylor Hansen

Reputation: 170

You could use the opcode lb $t, offset($s). It works the same as lw $t, offset($s), but it loads a byte instead of a 4-byte word into $t.

So let's say you want to load the 6th byte of the array. You would do:

la $t0, array  # load the array address
lb $t1, 5($t0) # get 6th byte through an offset

# then do whatever you want with it here

EDIT: You also have lh for 2-byte halfwords. Also, here's the MIPS instruction reference I used to answer your question: http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html

Upvotes: 5

Related Questions