Reputation: 3
Hi so I am brand new to the whole MIPS assembly code and am trying to teach myself with online help sources. I can understand some parts of it and how it works. My problem that I need to solve now is
int A[50], B[50];
for (i=1; i < 50; i++) {
A[i] = A[i] + B[i-1] / A[i-1];
}
Some tips and pointers would very much be appreciated looking to expand my knowledge. I've tried those online things but don't seem to help me learn.
Upvotes: 0
Views: 1502
Reputation: 11537
Here is how the compilers translate this kind of code.
This is C code, but it is strictly equivalent to your original code and it is written in such a way that every C instruction can be mapped to an asm instruction. I have even written which instruction should be used.
int * pA=&A[0]; // mov
int * pB=&B[0]; // mov
int i=1; // addi
int n=50; // addi
do{
// body of for loop
int B_1=*(pB-1) ; // lw
int A_1=*(pA-1) ; // lw
int D=B_1/A1; // div
int A=*pA; // lw
A += D; // add
*pA = A; // sw
// increment part of the loop
i+=1; // addi
pA+=1; // addi
pb+=1; // addi
// test part of the loop
while (i<N); // blt -> do
}
You just have to pick a register for all the variables (for instance, choose to copy pA
in $t3
(or r4
or whatever), and figure out how to translate this instruction in asm. Look at MIPS documentation and translation should be easy.
There are though a couple of important things to know.
Pointers are similar to memory addresses that are manipulated by the processor, except when increment (or in general pointer arithmetic) is concerned. If p
is a pointer to an element of type T
in C, p++
means that p
will point to the next element of type T
. For adresses, the size of the type must be considered. If an int
is 4 bytes, "point to next int" will correspond to "add to address the size of an int" (ie 4).
lw
and sw
use based addressing, that is they can add an immediate to an address. Pointer arithmetic is also relevant. So if p
is an int
pointer, and you want to implement x=*(p+2)
, assuming x
is in $t4
and p
is in $t7
, you have to do lw $t4,8($t7)
that means read memory at address $t7+8
(as 8==2*sizeof(int)
.
It should not be too difficult to write a first code. Then, you can try to optimize it. Maybe loading A[i-1]
at every iteration is not useful, for instance? Maybe you can reduce the number of variables (registers)?
Upvotes: 1