Question: 1 7 . Translate following C + + code to MIPS assembly language: * Write down your comment after ## int main ( ) {

17. Translate following C++ code to MIPS
assembly language:
*Write down your comment after ##
int main(){
int x[6];
for (int i=0; i<6; i++)
{x[i]= i;
}
}
x: .space 24 ## Reserve space for 6 integers (6*4 bytes =24 bytes)
.text
.globl main
main:
# Initialize i to 0
li $t0,0 ## $t0= i
loop:
# Check if i <6
li $t1,6 ## $t1=6
bge $t0, $t1, end_loop ## if i >=6, exit loop
# Calculate address of x[i]
sll $t2, $t0,2 ## $t2= i *4(shift left logical by 2 to multiply by 4)
la $t3, x ## load address of x into $t3
add $t3, $t3, $t2 ## $t3= address of x[i]
# Store i into x[i]
sw $t0,0($t3) ## x[i]= i
# Increment i
addi $t0, $t0,1 ## i++
# Jump back to the beginning of the loop
j loop
end_loop:
# Exit the program
li $v0,10 ## Load the exit code into $v0
syscall ## Make a system call to exit

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!