92.1k views
4 votes
Translate the following C++ program to MIPS assembly program #include using namespace std; const int CITY = 2; const int WEEK = 7; int main() { int temperature[CITY][WEEK]; cout << "Enter all temperature for a week of first city and then second city. \\"; // Inserting the values into the temperature array for (int i = 0; i < CITY; ++i) { for(int j = 0; j < WEEK; ++j) { cout << "City " << i + 1 << ", Day " << j + 1 << " : "; cin >> temperature[i][j]; } } cout << "\\\\Displaying Values:\\"; // Accessing the values from the temperature array for (int i = 0; i < CITY; ++i) { for(int j = 0; j < WEEK; ++j) { cout << "City " << i + 1 << ", Day " << j + 1 << " = " << temperature[i][j] << endl; } } return 0; }

User FlatLander
by
4.6k points

2 Answers

4 votes

Answer:

.data

temperature: .space 100 #create space for 25

prompt: .asciiz "\\Enter all temperature for a week of first city and then second city.\\"

city: .asciiz "City "

day: .asciiz ",Day "

colon: .asciiz " : "

display: .asciiz "\\\\Displaying Values:\\"

#declare array

newline: .asciiz "\\" #to print new line

space: .asciiz " " #to print new line

.text

li $v0,4

la $a0,prompt #it will print prompt

syscall

la $s0,temperature #load address of array

li $s1,2 #number of city

li $s2,7 #number of week

li $t0,0 #using as i

outerLoop:

bge $t0,$s1,exitOuter # while i<row

li $t1,0 #using as j

innerLoop:

bge $t1,$s2,exitInner # while i<col

li $v0,4

la $a0,city #it will print prompt

syscall

add $a0,$t0,1

li $v0,1

syscall

li $v0,4

la $a0,day #it will print prompt

syscall

add $a0,$t1,1

li $v0,1

syscall

li $v0,4

la $a0,colon #it will print prompt

syscall

mul $t2,$t0,$s2

add $t2,$t2,$t1 #get address of [i][j]

mul $t2,$t2,4 #get effective address of [i][j]

add $t2,$t2,$s0 #get address of array[i][j]

li $v0,5

syscall

sw $v0,($t2) #load value of array[i][j]

li $v0,11

li $a0,' '

syscall

add $t1,$t1,1 #j++

j innerLoop

exitInner:

li $v0,11

li $a0,'\\'

syscall

add $t0,$t0,1 #i++

j outerLoop

exitOuter:

li $v0,4

la $a0,display

syscall

li $t0,0 #using as i

outerLoop1:

bge $t0,$s1,exitOuter1 # while i<row

li $t1,0 #using as j

innerLoop1:

bge $t1,$s2,exitInner1 # while i<col

li $v0,4

la $a0,city #it will print prompt

syscall

add $a0,$t0,1

li $v0,1

syscall

li $v0,4

la $a0,day #it will print prompt

syscall

add $a0,$t1,1

li $v0,1

syscall

li $v0,4

la $a0,colon #it will print prompt

syscall

mul $t2,$t0,$s2

add $t2,$t2,$t1 #get address of [i][j]

mul $t2,$t2,4 #get effective address of [i][j]

add $t2,$t2,$s0 #get address of array[i][j]

lw $t2,($t2) #load value of array[i][j]

li $v0,1

move $a0,$t2

syscall

li $v0,11

li $a0,'\\'

syscall

add $t1,$t1,1 #j++

j innerLoop1

exitInner1:

li $v0,11

li $a0,'\\'

syscall

add $t0,$t0,1 #i++

j outerLoop1

exitOuter1:

Step-by-step explanation:

The program has been coded in the answer part.

See answer

User Kratenko
by
4.9k points
2 votes

Answer:

Check the explanation

Step-by-step explanation:

Code:-

.data

temperature: .space 100 #create space for 25

prompt: .asciiz "\\Enter all temperature for a week of first city and then second city.\\"

city: .asciiz "City "

day: .asciiz ",Day "

colon: .asciiz " : "

display: .asciiz "\\\\Displaying Values:\\"

#declare array

newline: .asciiz "\\" #to print new line

space: .asciiz " " #to print new line

.text

li $v0,4

la $a0,prompt #it will print prompt

syscall

la $s0,temperature #load address of array

li $s1,2 #number of city

li $s2,7 #number of week

li $t0,0 #using as i

outerLoop:

bge $t0,$s1,exitOuter # while i<row

li $t1,0 #using as j

innerLoop:

bge $t1,$s2,exitInner # while i<col

li $v0,4

la $a0,city #it will print prompt

syscall

add $a0,$t0,1

li $v0,1

syscall

li $v0,4

la $a0,day #it will print prompt

syscall

add $a0,$t1,1

li $v0,1

syscall

li $v0,4

la $a0,colon #it will print prompt

syscall

mul $t2,$t0,$s2

add $t2,$t2,$t1 #get address of [i][j]

mul $t2,$t2,4 #get effective address of [i][j]

add $t2,$t2,$s0 #get address of array[i][j]

li $v0,5

syscall

sw $v0,($t2) #load value of array[i][j]

li $v0,11

li $a0,' '

syscall

add $t1,$t1,1 #j++

j innerLoop

exitInner:

li $v0,11

li $a0,'\\'

syscall

add $t0,$t0,1 #i++

j outerLoop

exitOuter:

li $v0,4

la $a0,display

syscall

li $t0,0 #using as i

outerLoop1:

bge $t0,$s1,exitOuter1 # while i<row

li $t1,0 #using as j

innerLoop1:

bge $t1,$s2,exitInner1 # while i<col

li $v0,4

la $a0,city #it will print prompt

syscall

add $a0,$t0,1

li $v0,1

syscall

li $v0,4

la $a0,day #it will print prompt

syscall

add $a0,$t1,1

li $v0,1

syscall

li $v0,4

la $a0,colon #it will print prompt

syscall

mul $t2,$t0,$s2

add $t2,$t2,$t1 #get address of [i][j]

mul $t2,$t2,4 #get effective address of [i][j]

add $t2,$t2,$s0 #get address of array[i][j]

lw $t2,($t2) #load value of array[i][j]

li $v0,1

move $a0,$t2

syscall

li $v0,11

li $a0,'\\'

syscall

add $t1,$t1,1 #j++

j innerLoop1

exitInner1:

li $v0,11

li $a0,'\\'

syscall

add $t0,$t0,1 #i++

j outerLoop1

exitOuter1:

Check below image for the code output:-

Translate the following C++ program to MIPS assembly program #include using namespace-example-1
User LampShadesDrifter
by
4.4k points