Search code examples
cassemblymemorycpu-registerssll

questions about scale by 4 for the word size from C converting to assembly language


I am a beginner of assembly language, so I hope anyone who give me a answer could explain be more specific.

Question is converting from C to assembly language.

C code is:

while(save[i] == k)
    i += 1;

i and k are in $s3 and $s5 and base of array save[] is in $s6

The answer is in the figure.

figure

What I misunderstand is that why $S3 multiple 2^2(==4), and then store into $t1.

I check on stackoverflow for similar questions, some people said "You should add the base and index together, and remember to scale by 4 for the word size. "

But because I am a beginner, I am still confused.

For example,

  1. after a one cycle, i == 2 (in $S3).

  2. and then start over, i == 2 in $S3 multiple by 4, we get 8 here, and then writes into $t1.

So there is a question, why is 8? I think what we need is save[2] not save[8].

I thought I might confused about value and address.

An another question is: how many bits are those register like $S1, $t1? I think is 32 bits, so it should be 1 word.


Solution

  • lw is to be provided the address of the first byte of the word to load.

    If each element of save is 32 bits or 4 bytes in size, then

    • save[0] is found 0 bytes beyond the start of save.
    • save[1] is found 4 bytes beyond the start of save.
    • save[2] is found 8 bytes beyond the start of save.
    • ...