0
votes

i am trying to comment this to figure out what it does

"$a0 is an integer argument while $a1 is a pointer to (ie: the address of) a large array. The value in $a0 can be any integer and the size of the array that $a1 points to is big enough (as long as you don't dereference memory before $a1, you won't be accessing memory that isn't yours) for the code to work correctly."

just adding 31 to t1 and t0 initially then a loop starts and you are doing a few and operations but then i get lost

can someone give me a hand?

addi $t1 $zero 31
addi $t0 $zero 31
loop:srlv $t3 $a0 $t1
andi $t3 $t3 1
addi $t3 $t3 48
sub $t4 $t0 $t1
add $t2 $a1 $t4
sb $t3 0($t2)
beq $t1 $zero done
subi $t1 $t1 1
j loop
done:sb $zero 1($t2)
jr $ra
1
Well it looks like $t1 is counting the loop iterations - Lee Meador
The value in $t4 is the array index (as in $a1[$t4] in some sort of pseudo language) and $t2 gets the address of that array element so $t3 can be stored to it (or something). Confession, I don't know the MIPS mnemonics. - Lee Meador

1 Answers

0
votes

Looks to me like it's creating a string representation of $a0 as a binary value, and storing it at $a1. For example, if $a0 had the value 0x56 (binary 01010110) you'd get the string "01010110" at $a1 (you'd actually get all 32 bits, but I omitted the top 24 bits in my example to keep things simple).

Here's what the code would look like in C:

for (t1 = 31; ; t1--) {
    a2 = &a1[31 - t1];
    *a2 = ((a0 >> t1) & 1) + '0':  // '0' == 48
    if (t1 == 0) break;
}
a2[1] = 0;  // NULL-terminator