In: Computer Science
convert following C++ code into MIPS assembly:
int main()
{
int x[10], occur, count = 0;
cout << "Type in array numbers:" << endl;
for (int i=0; i<10; i++) // reading in integers
{
cin >> x[i];
}
cout << "Type in occurrence value:" << endl;
cin >> occur;
// Finding and printing out occurrence indexes in the array
cout << "Occurrences indices are:" << endl;
for (int i=0; i<10; i++)
{
If (x[i] == occur)
{
cout << i << endl;
count ++;
}
}
cout << "Number of occurrences found:" << endl;
cout << count;
return 0;
}
Please up vote ,comment if any query .Thanks for question , Be safe .
Note : check attached image for output ,code compiled and tested in MARS MIPS simulator.
Program Plan :
Program :
.data#variable section
x : .space 40
#size 10 and word takes 4 bytes so 10*4 =40 bytes
space reserved
#string declaration
arrayInput:
.asciiz "Type in array numbers:\n"
occurencePrompt : .asciiz "Type in occurrence
value:\n"
occurenceIndex : .asciiz "Occurrences indices
are:\n"
occurenceFound : .asciiz "Number of occurrences
found:\n"
.text #code section
jal main #call main function of
program
#terminate program
li $v0,10 #syscall 10 to terminate
program
syscall
.globl main #declare main as global
main: #main function start from here
la $a0,arrayInput #$a0=address of arrayInput
String
li $v0,4 #syscall 4 to print string
syscall
li $t0,0 #loop count i =0 $t0(i)=0
la $s0,x #$s0=address of x array
loop:#loop label
#$t0==10(length of array) go
to endOfLoop label
beq $t0,10,endOfLoop
li $v0,5 #read integer input
using syscall 5
syscall #input stored in
$v0
mul $t1,$t0,4 #t1=$t0(loop
count i)*4 = 0*4
add $t1,$t1,$s0 #t1=$t1+base
address of array x
#store value of $v0(user
input) at address stored in $t1 register
sw $v0,0($t1) #store word sw
register ,address
#$t0=$t0+1
addi $t0,$t0,1 #increment i
loop count
j loop #jump to loop
label
endOfLoop: #endof loop label
la $a0,occurencePrompt #$a0=address of
occurencePrompt string
li $v0,4 #syscall 4 to print string
syscall
li $v0,5 #read user input occurence saved
in $v0
syscall
addi $s1,$v0,0 #occurence saved in $s1
from $v0
la $a0,occurenceIndex #a0=address
of occurenceIndex
li $v0,4
syscall
li $t0,0 #loop index i =0
li $t1,0 #count occurence =0
secondLoop:
beq
$t0,10,printOccurence#if loop indexi ==10(length of array) go to
printOccurence label
mul $t2,$t0,4
#$t2=$t0*4 (4=word offset)
add $t2,$t2,$s0
#$t2=$t2+base address of array x
lw $t3,0($t2)
#load value from x[$t0] into $t3
bne $s1,$t3,skip
#if occurence not equal to x[$t0]($t3) go to skip label
#else
addi $t1,$t1,1
#t1(count)=$t1(count)+1
skip: #skip label
addi $t0,$t0,1
#increment loop index i
j secondLoop
#jump to secondLoop
printOccurence:#label
la
$a0,occurenceFound #$a0=address of occurenceFound string
li
$v0,4 #syscall 4 to print string
syscall
addi $a0,$t1,0 #move value of count into $a0=t1(count)
li
$v0,1 #syscall 1 to print integer
syscall
jr $ra #return to address from main
function called
Output :
Please up vote ,comment if any query .