MIPS 32 ASM to C code, did i do this right?

In summary, the given MIPS assembly code is implementing the C statement: c = B[i+2] - B[i]. The code first adds the value of i with itself, then multiplies it by 2, and finally subtracts the values of B[i+2] and B[i]. The purpose of the beginning codes is to set up the memory in a specific way and to calculate the values needed for the final subtraction. The other possible options for the C statement include different combinations of B[i] and B[i+2] with various offsets. The code is correct in its implementation of the C statement.
  • #1
mr_coffee
1,629
1
have the following MIPS 32 asm code:
Assuming B is an array of 10 words whose base address is in register $s0, andvariable c and i are in $s1 and $s2, respectively. What is the C statement implemented by the below MIPS assembly code?
add $t0, $s2, $s2
add $t0, $t0, $t0
lw $t1, 0($s0)
lw $t2, 8($s0)
sub $s1, $t2, $t1

In C what does it look like?
i said c = B[i+2] - B

but the other optpoins are:
c = B - B[i+2]
c = B - B[i+8]
c = B[i+8] - B

Can someone see if I'm correct?

lw $t2,8($s0);
I said was really index B[2] because, in mips memory is set up by 4's.
meaning

If i have memory set up like this:

[0x10ff00ff] 32
[0x00ff00ff] 28
[0x0000ffff] 24
[0xffff0000] 20
[0x000011ff]16
[0x10ffff00] 12
[0xff00ffff] 8
[0xff0000ff] 4
[0x0000ff44] 0
32 bit data and the numbers are word address

I'm confused on what the beginning codes prupose is for, the 2 add statements.
It basically does this i think:
$t0 = i + i;
$t0 = (i+i) + (i+i)?
 
Physics news on Phys.org
  • #2
Yep it was correct
 
  • #3


I can confirm that your interpretation of the MIPS assembly code is correct. The code is essentially performing the operation of c = B[i+2] - B in C. The first two add statements are used to calculate the index for the array access, by multiplying i by 2 (since each word in the array is 4 bytes) and then adding 2 to it. This results in the value of 8, which is then used as the offset for the load word (lw) instructions.

It's important to note that the order of the sub operation can vary depending on the intended purpose. In the provided options, all variations are correct, as they all result in the same final value of c. It ultimately depends on the specific task or algorithm that the code is being used for.

As for the memory setup in MIPS, you are correct in that each address represents a word (4 bytes) in memory. This is why the offset used in the lw instructions is 8, as it represents 2 words (8 bytes).

Overall, your understanding and interpretation of the MIPS code is accurate. Keep up the good work!
 

1. How do I convert MIPS 32 ASM code to C code?

There are a few steps involved in converting MIPS 32 ASM code to C code. First, you will need to understand the function of each instruction in the ASM code. Then, you can manually rewrite each instruction in C code, using the appropriate syntax and data types. Finally, you will need to test and debug your C code to ensure it performs the same operations as the ASM code.

2. What tools do I need to convert MIPS 32 ASM code to C code?

You will need a text editor, a C compiler, and some knowledge of both MIPS 32 ASM and C programming languages. Some helpful resources for converting ASM to C include online converters, textbooks, and programming forums.

3. Can I use automated software to convert MIPS 32 ASM code to C code?

While there are some automated tools available, they may not produce accurate or efficient code. It is recommended to manually convert the code to ensure the best results.

4. What are some common challenges when converting MIPS 32 ASM code to C code?

Some common challenges when converting ASM to C include understanding the differences in syntax and data types between the two languages, debugging errors in the converted code, and ensuring the same functionality as the original ASM code.

5. How can I test my converted C code to ensure it is correct?

You can test your C code by comparing the output of the converted code to the output of the original ASM code. You can also use debugging tools to step through the code and check for any errors or unexpected behavior.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
5K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
14K
Back
Top