简体   繁体   中英

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?

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

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