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

  • Thread starter Thread starter mr_coffee
  • Start date Start date
  • Tags Tags
    C code Code Mips
Click For Summary
SUMMARY

The discussion revolves around the conversion of MIPS 32 assembly code to C code. The assembly code provided calculates the difference between two elements in an array B, specifically using the indices derived from the variable i. The correct C statement is confirmed to be c = B[i + 2] - B[i], as the assembly instructions effectively double the index i to access the correct memory locations. The confusion regarding the purpose of the initial addition operations is clarified, indicating they serve to compute the index for accessing the array elements.

PREREQUISITES
  • MIPS 32 assembly language syntax
  • Understanding of memory addressing in MIPS
  • Basic C programming and array manipulation
  • Knowledge of data types and sizes in MIPS architecture
NEXT STEPS
  • Study MIPS 32 assembly language instructions and their C equivalents
  • Learn about memory alignment and addressing in MIPS architecture
  • Explore array manipulation in C, focusing on pointer arithmetic
  • Investigate the implications of data types and sizes in assembly programming
USEFUL FOR

This discussion is beneficial for computer science students, software developers working with low-level programming, and anyone interested in understanding the translation between assembly language and high-level programming languages like C.

mr_coffee
Messages
1,613
Reaction score
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
Yep it was correct
 

Similar threads

Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
15K
  • · Replies 4 ·
Replies
4
Views
2K