www.errorediridondanzaciclicodotcom

  • Home
  • MIPS ex 1
  • BACK P.P.

  • My solution for an exercise in MIPS

    Here, I would explain MY solution for this exercise I saw on a book. It’s a MIPS exercise and consists in translating the MIPS code into C code. Moreover you can check, in a second exercise, your solution in C with given numbers (hexadecimal).
    Here, the assembly code:

    addi $t0, $s6, 4
    add $t1, $s6, $0
    sw $t1, 0($t0)
    lw $t0, 0($t0)
    add $s0, $t1, $t0

    I will use the numbers of the second exercise in order to check the final code with the right numeric solution.

    $s6 is a register that contains the base address of an array (A) and for the exercise is equal to 0x00000100. It's like to have in memory an array A as following:

    Array A in memory ($s6)
    Address Value
    0x00000100 ..........
    0x00000104 ..........
    0x00000108 ..........


    The code: addi $t0, $s6, 4 adds the value of 4 to the register $s6 and stores it in $t0, therefore 0x00000100 + 4 = 0x00000104 (in this case the hexadecimal addition is equal to the decimal addition). Now it’s important to underline that what we have obtained is the value of an address (an address plus a number is an address also).
    In C we could write the result as $t0=&A[1] that means the address of the second item of the array A (remember in MIPS the addresses are byte-addresses) is put in the register $t0.

    The second line is the same except for the destination register $t1 and for the address that is now the first (0). What we have now is the base address of A[0], &A[0], stored in $t1, so $t1 = &A[0] = 0x00000100.

    Third line: sw $t1, 0($t0), it stores the content of $t1 in the first position of the array that has base address specified in $t0. This register has been previously (line 1) initialized with the address of 0x00000104. So, we are now taking the content of $t1 (0x00000100) and storing it in memory at address specified in 0($t0). This address (0x00000104) will now receive the value 0x00000100. That means, in C, A[1]=0x00000100.

    Array A in memory ($t0)
    Address Value
    0x00000104 0x00000100
    0x00000108 ..........


    Fourth line: lw $t0, 0($t0), load the content (value) of first element of the array as specified in 0($t0) in register $t0, so the register $t0 is now equal to 0x00000100. The array A, at the base address that is 0x00000104, has the value of 0x00000100 (we just stored it previously). It’s like:

    Array A in memory ($t0)
    Address Value
    0x00000104 0x00000100
    0x00000108 ..........


    Fifth line: add $t0 and $t1, both registers containing the values of two addresses that are the same 0x00000100 + 0x00000100 = 0x00000200 (here again we have that hexadecimal sum and decimal sum have the same result).
    The number 0x00000200 is equivalent to 512 (2 x 16x16) in decimal and it’s the right solution of the following exercise where you are asked to evaluate the above code with some values supplied.
    Translated in C we have f = &A[0] + &A[0] = 2&A[0] = 2&A (we can omit the 0) that is the suggested result of the problem.