0

I'm writing a program in MIPS, and I'm struggling with using the sw function to write an answer to memory. Here's a sample code to help explain the issue:

.data
byteArray: .byte 0,1,0,1     
finalResult: .word 0 

.text
la $t0, byteArray                    
la $t1, finalResult                      

lb $t2, 0($t0)  
add $t3, $t2, $zero
lb $t2, 1($t0)  
add $t3, $t2, $t3
lb $t2, 2($t0)  
add $t3, $t2, $t3
lb $t2, 3($t0)  
add $t3, $t2, $t3 

sw $t3, 0($t1)

In this program, I just want to count the number of bytes in my array that are equal to 1 (in this case the answer is 2), and store that result in memory in my answer to the variable finalResult. When running the program, my result isn't being stored in the finalResult variable.

I really appreciate any help in figuring this out! Thank you!

Zwedgy
  • 320
  • 2
  • 3
  • 17
Curious
  • 29
  • 6
  • 1
    Looks fine to me; how are you examining the 4 bytes at `finalResult`? i.e. provide a [mcve] that includes how you're checking the result. Remember that MIPS is (usually) big-endian, so after a word store, the high byte of the word will have the result. Are you only looking at the first byte (the MSB)? – Peter Cordes Jul 25 '18 at 18:25
  • 1
    And BTW, I'd write this as `lw $t2, bytesArray` / `srl $t3, $t2, 16` / `addu $t2, $t2, $t3` do do `arr[3]+arr[1]` and `arr[2]+arr[0]` in the low 2 bytes of `$t2`. Then repeat with an 8-bit shift to get the full sum in the low byte. (This is safe because there are enough padding bits to protect your sums from carrying into each other with this https://en.wikipedia.org/wiki/SWAR). You can `AND` to mask off the low bits, or do a byte store to just one byte of `finalResult` if you know the other 3 bytes stay zero. – Peter Cordes Jul 25 '18 at 18:31
  • 1
    Well that was an easy fix! I was looking for the change in the low byte, not remembering that MIPS is big-endian. Thank you! Works great! – Curious Jul 25 '18 at 18:31
  • Thanks a good suggestion, thanks! I'm new to programming in MIPS so still figuring out the optimal ways to write the code. I'll try that out! – Curious Jul 25 '18 at 18:34
  • IDK if a compiler would have done this for you, but you can learn a lot of efficiency tricks from writing a simple function in C and compiling it with optimization enabled. (take an arg and return a result, so constants don't just optimize to `return 2;`) See [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116). – Peter Cordes Jul 25 '18 at 18:36
  • 1
    Oh and BTW, if your MIPS has a fast multiply, you could do this with one multiply that produces the result you want in the high byte of a word. (Remember that multiply is the same as shifting and adding.) So I think `(input_word * 0x01010101) >> 24` would do it. Yup, `((0x01010101 * 0x01010101) >> 24) & 0xFF` is 4. See http://graphics.stanford.edu/~seander/bithacks.html for more stuff like that, but without explanations of why they work. See [How to count the number of set bits in a 32-bit integer?](https://stackoverflow.com/a/15979139) for some explanation. – Peter Cordes Jul 25 '18 at 18:42

0 Answers0