1

Im trying to convert the following C code to MIPS but im having trouble understanding how you take the [k-1] in the array.

int vek[100];
main()
{
int k;
int first = vek[0];

for (k = 1; k < 100; k++)
{ 
   vek[k - 1] = vek[k];
}
vek[k – 1] = first;
}

Here is what i got so far:

.data
vek: .space 400

.text
 main:
 addi s0,zero,1                #k = 1
 addi s1,zero.100              #value 100
 la t1,vek                     #t1 as index in vek
 add s2,t1,zero                #first = vek[0]

 L1:
 bgt s0,s1,end                 #if k is bigger then 100 jump to end label
 addi s0,zero,-1               #k-1
 sll t2,s0,2                   #t2 = 4*k next space in the array

here is where i lose myself, i don't understand how i am supposed to get the rest of the code translated. Since there is a lack of MIPS tutorials on the web you are my last chance. If some kind soul could help translate the last part of the code and give me an explanation that would be great.

P.S this is not something i'm going to use its simply just an example of an question that will be on the exam.

  • 1
    Do you have a GCC compiler targeting MIPS? If so, use something like `gcc-mips-whatever -S foo.c` to generate a "foo.s" file. Use the generated assember output as a learning aid! – Ian Abbott Oct 28 '15 at 16:08
  • You need another register. s0 has k, s1 has 100, s2 has first, so s3 seems like the logical choice for k-1. (Assuming that there is an s3.) – user3386109 Oct 28 '15 at 16:10
  • Ok so i set s3 as k-1, but then what? How do i connect it to the vek? Like vek[k-1] – user3529167 Oct 28 '15 at 16:12
  • Use `sll t2,s3,2` followed by `add s4,t2,zero`. (Assuming that there is an s4.) – user3386109 Oct 28 '15 at 16:21
  • Ok, so when i do like i did above ill get `vek[k]`? so what should i do to get `vek[k-1]=vek[k]`? – user3529167 Oct 28 '15 at 16:29
  • Use the `sll` instruction to put addresses into the `t` registers. Then use a "load" instruction to read `ver[k]` into an `s` register, and a "store" instruction to write the contents of the `s` register to `ver[k-1]`. I'm not familiar enough with the MIPS instruction set to know the mnemonics for the "load" and "store" instructions. – user3386109 Oct 28 '15 at 16:35
  • 1
    Ok thanks, i will try this! – user3529167 Oct 28 '15 at 16:37
  • why not using a tool like qemu.org see here http://stackoverflow.com/questions/4175450/is-there-a-way-to-use-gcc-to-convert-c-to-mips – JavaSheriff Oct 28 '15 at 16:47

1 Answers1

0

Here's a simple implementation that ought to work, with a line-by-line explanation of what's going on:

get_first:
lw $s0, vek           # 1. Load the word stored at location 'vek' into $s0.
addi $s1, $zero, 0    # 2. Load zero into $s1. This will store an offset into 'vek', in bytes.

loop_1:
la $s2, vek($s1)      # 3. Load the *address* of the $s1-th word of 'vek' into $s2. This is the location you want to write to.
addi $s1, $s1, 4      # 4. Advance the $s1 pointer to the next word in 'vek'.
lw $s3, vek($s1)      # 5. Load the *value* of the $s1-th word of 'vek' into $s3. This is the value you want to copy.
sw $s3, ($s2)         # 6. Store the value obtained in line 5 into the location obtained in line 3.
blt $s1, 400, loop_1  # 7. Repeat until we have iterated over all memory in 'vek'.

set_last:
sw $s0, vek + 396     # 8. Store the value obtained in line 1 into the end of 'vek'.

You can probably make this more concise, but I was trying to make this easy to understand, and it's been a long time since I've seen MIPS.

Joe Farrell
  • 3,502
  • 1
  • 15
  • 25