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

381 Views Asked by At

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.

2

There are 2 best solutions below

12
On BEST ANSWER

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.
  • ...
0
On

Each entry in save is 32 Bit long. This means, that the i-entry has an offset of i * 32 Bit = i * 4 Byte starting with the base address $s6 C is actual very smart and calculates this for you internally. So in C-Code writing save[i] is the same as *(save + i). An Addition to an pointer will be multipled by its size, so C will translate it into save + 4 * i

The Register size depends in your local architecture, but is generally speaking mostly 32 Bit for examples/exercises.