Convert C code to MIPS (arrays)

for (i = 0; i < 64; i++) { A[i] = B[i] + C[i]; } 

MIPS instructions for the above C code:

 add $t4, $zero, $zero # I1 i is initialized to 0, $t4 = 0 Loop: add $t5, $t4, $t1 # I2 temp reg $t5 = address of b[i] lw $t6, 0($t5) # I3 temp reg $t6 = b[i] add $t5, $t4, $t2 # I4 temp reg $t5 = address of c[i] lw $t7, 0($t5) # I5 temp reg $t7 = c[i] add $t6, $t6, $t7 # I6 temp reg $t6 = b[i] + c[i] add $t5, $t4, $t0 # I7 temp reg $t5 = address of a[i] sw $t6, 0($t5) # I8 a[i] = b[i] + c[i] addi $t4, $t4, 4 # I9 i = i + 1 slti $t5, $t4, 256 # I10 $t5 = 1 if $t4 < 256, ie i < 64 bne $t5, $zero, Loop # I11 go to Loop if $t4 < 256 

In I8, can the sw command not be replaced by the addi ? addi $t5, $t6, 0

Would the same task copy the address of $t6 to $t5 ? I would like to know the difference and when to use any of them. The same can be said of the lw instruction.

Also, perhaps a related question, how does MIPS handle pointers?

edit: changed addi $ t6, $ t5, 0.

+4
source share
4 answers

For I8, can the sw instruction not be replaced by the addi instruction? i.e. $ t6, $ t5, 0

Not. The sw command stores the result in memory. add just manages the registers. And lw gets the word out of memory. This is the only MIPS instruction to do this. (Other processors may have and have add versions that have memory access but not MIPS.)

You need to adjust your thinking when working in assembler. Registers and memory are separated. In higher-level languages, registers are (almost) completely hidden. In an assembly, registers are a separate resource that a programmer must manage. And this is a scarce resource. The HLL compiler will do this for you, but when programming in the assembly, you took over the work.

How does MIPS handle pointers?

In MIPS, pointers are integers (in registers or memory) that are memory addresses. The only way to distinguish them from data is your brain. A "pointer" is something that a higher-level language developer has come up with to ease a programmer for this burden. If you look carefully, you will see that $t5 actually contains a pointer. This is the memory address used by lw and sw as the address to load or save.

+3
source

The MIPS sw instruction stores the first argument (value in $ t6) at the address in the second argument (value in $ t5), shifted by a constant value (0).

Actually, you are not trying to save the address $ t5 in the register, but rather save the value in $ t6 in the memory cell represented by the value of $ t5.

If you like, you can consider the value in $t5 to be the same as a pointer to C. In other words, MIPS does not handle pointers and values ​​in different ways - all that matters is where you use the values. If you use a register value as the second argument to lw or sw , then you are effectively using that register as a pointer. If you use the register value as the first argument to lw or sw or in most other places, you are working directly on the value. (Of course, as in C-pointer arithmetic, you can manipulate the address so that you can store some of the data somewhere else in memory.)

+4
source

For I8, can the sw instruction not be replaced by the add instruction? Does he not achieve the same task of copying the address $ t5 to $ t0? I would like to know the difference and when to use any of them.

I think you are confused with what actually stores the word in the store. In I8, the register value in $ t6 is stored in $ t5 at the zero position. The application will overwrite all the data stored in the destination register, with the sum of the two values ​​of the other registers.

Also, perhaps a related question, how does MIPS handle pointers?

"Pointers" are only addresses in memory stored in registers (as opposed to values).

+3
source

lw and sw read / write to memory. addi and other arithmetic operations work with registers.

Registers are like small buckets used by the CPU to store data. They can be solved in 5 bits or so if I remember my MIPS architecture correctly.

Memory is like a huge ocean of data, which requires more than 16 bits. So you really need to keep the address in the register.

Pointers are just memory addresses (32 bits in a 32-bit architecture).

+3
source

All Articles