0

Fellow SO users

I'm hoping someone could explain the behavior that I am experiencing whilst using SPIM to run my MIPS code. I'm implementing an encryption algorithm and one of the steps is loading the encryption key which is 64 bits wide. For example, if the encryption key is 0x123456789ABCDEF0, I'm declaring it in the following way;

.data
    enc_key_lo: 0x78563412
    enc_key_hi: 0xF0DEBC9A

Now, whenever I load enc_key_lo first, I check to see whether I encounter a 0 so that I can load enc_key_hi. The following is my code;

.text
la $a1, enc_key_lo
la $a2, enc_key_hi

load_lo:
    lbu $s0, 0($a1) 
    addi $a1, $a1, 1      
    beq $s0, $zero, load_hi  #If 0, then we need to load enc_key_hi
    j load_lo

load_hi:
    lbu $s1, 0($a2)
    addi $s2, $s2, 1
    /* DO SOME OPERATIONS ETC ETC */

When I run the code, I observe the register $s0 loads with the following on successive iterations of the load_lo loop;

12, 34, 56, 78, 9A, BC, DE, F0

I was expecting register $s0 to load with 0 after loading 78 but it keeps loading all the way till F0. Hence, this defeats the purpose of having the load_hi segment in the code. I noticed that if I commented out the declaration enc_key_hi, then $s0 is loaded with 0 after loading with 78.

Could someone please explain this behavior? Kind regards.

Triple777er
  • 621
  • 3
  • 17
  • 30
  • 1
    Why do you expect `$s0` to see zero after 0x78? The data sits in memory exactly as you typed it in the data segment (without zeros). – Omri Barel Mar 23 '13 at 12:51
  • Hi Omri, so how is the end of the string represented in memory for MIPS? There is no NULL character to compare with is there? – Triple777er Mar 23 '13 at 22:05
  • A value is just taken as a word - not as a string. If you want to store a string you need to tell the assembler the type of the data. For example: `enc_key_lo: .asciiz "hello"` which will put the characters `'h'`, `'e'`, `'l'`, `'l'`, `'o'`, `'\0'` in the data segment. But why would you want to store encryption keys as null-terminated strings? – Omri Barel Mar 23 '13 at 23:09

0 Answers0