In: Computer Science
For this assignment, you are required to write a MIPS program that reads its own instructions and counts the number of occurrences of each type of instruction (R-type, I-type, or J-type). To accomplish this task, your program should execute the following steps:
li $v0, 10
syscall
Thus, your program should repeat step 2’s loop until the machine instructions corresponding to this sequence are detected. (Be sure to count these machine instructions as well!)
THE SOLUTION FOR THIS ASSIGNMENT IS ACTUALLY ON CHEGG, BUT IT DOESN'T WORK. CAN YOU PLEASE HELP ME TO FIX THE ISSUE?
.data
#Inst: .#
CountR: .word 0
CountJ: .word 0
CountI: .word 0
CountR_char : .asciiz "CountR is "
CountJ_char : .asciiz "\nCountJ is "
CountI_char : .asciiz "\nCountI is "
TypeR: .word 0x00 #should be something like this but I don't
know what exactly it should be
TypeJ1: .word 0x02
TypeJ2: .word 0x03
EndI: .word 0x2402000A #last but one instruction as last instruction is syscall and not unique
#first instruction is stored in memory location 0x00400000
#and second instruction is stored in location 0x00400004 and so
on.
#Every instruction's type(op code) is stored as bit 31-26 in that
32bit
.text
main:
lw $t0, CountR
lw $t1, CountJ
lw $t2, CountI
lw $t4, TypeR
lw $t5, TypeJ1
lw $t6, TypeJ2
li $t3, 0x00400000 #starting address
li $s0, 0 #instruction code
lw $t7, EndI
Loop: #read instruction at address into $s0
lw $s0,0($t3) #Load complete word and rotate by
right by 26
#shift it right logical by 2 ,makes it 00xxxx, $s0=
bit 31-26
srl $s0,$s0,26
# check shifted instruction
beq $s0, $t4, typeR #if it's typeR, then jump to
typeR
beq $s0, $t5, typeJ #if it's typeJ1, then jump to
typeJ
beq $s0, $t6, typeJ #if it's typeJ2, then jump to
typeJ
TypeI: #none of above, then it's typeI
addi $t2, $t2, 1 #countI +1
j Next #jump to next
typeR: # bit (31-26) is 0x00 (type r)
addi $t0, $t0, 1 #countR +1
j Next #jump to next
typeJ: # bit (31-26) is 0x02 or 0x03
addi $t1, $t1, 1 #countJ +1
j Next #jump to next
Next: #after count, check if it's end instruction
lw $s0,0($t3)
beq $s0,$t7,Exit #if it's endI, jump to exit
#not end yet, add address to get another instruction
addi $t3,$t3,4 #address+4
j Loop
Exit: #one last syscall instruction is remaining that is Type R,
increment CountR
addi $t0, $t0, 1 #countR +1 for syscall
# store the result
sw $t0,CountR
sw $t1,CountJ
sw $t2,CountI
#print result
#----------
li $v0,4 #string print
la $a0, CountR_char
syscall
li $v0,1
add $a0, $t0 ,$0
syscall
li $v0,4 #string print
la $a0, CountJ_char
syscall
li $v0,1
add $a0, $t1 ,$0
syscall
li $v0,4 #string print
la $a0, CountI_char
syscall
li $v0,1
add $a0, $t2 ,$0
syscall
li $v0,10
syscall
.data
#Inst: .#
CountR: .word 0
CountJ: .word 0
CountI: .word 0
CountR_char : .asciiz "CountR is "
CountJ_char : .asciiz "\nCountJ is "
CountI_char : .asciiz "\nCountI is "
TypeR: .word 0x00 #should be something like this but I don't know what exactly it should be
TypeJ1: .word 0x02
TypeJ2: .word 0x03
EndI: .word 0x2402000A #last but one instruction as last instruction is syscall and not unique
#first instruction is stored in memory location 0x00400000
#and second instruction is stored in location 0x00400004 and so on.
#Every instruction's type(op code) is stored as bit 31-26 in that 32bit
.text
main:
lw $t0, CountR
lw $t1, CountJ
lw $t2, CountI
lw $t4, TypeR
lw $t5, TypeJ1
lw $t6, TypeJ2
li $t3, 0x00400000 #starting address
li $s0, 0 #instruction code
lw $t7, EndI
Loop: #read instruction at address into $s0
lw $s0,0($t3) #Load complete word and rotate by right by 26
#shift it right logical by 2 ,makes it 00xxxx, $s0= bit 31-26
srl $s0,$s0,26
# check shifted instruction
beq $s0, $t4, typeR #if it's typeR, then jump to typeR
beq $s0, $t5, typeJ #if it's typeJ1, then jump to typeJ
beq $s0, $t6, typeJ #if it's typeJ2, then jump to typeJ
TypeI: #none of above, then it's typeI
addi $t2, $t2, 1 #countI +1
j Next #jump to next
typeR: # bit (31-26) is 0x00 (type r)
addi $t0, $t0, 1 #countR +1
j Next #jump to next
typeJ: # bit (31-26) is 0x02 or 0x03
addi $t1, $t1, 1 #countJ +1
j Next #jump to next
Next: #after count, check if it's end instruction
lw $s0,0($t3)
beq $s0,$t7,Exit #if it's endI, jump to exit
#not end yet, add address to get another instruction
addi $t3,$t3,4 #address+4
j Loop
Exit: #one last syscall instruction is remaining that is Type R, increment CountR
addi $t0, $t0, 1 #countR +1 for syscall
# store the result
sw $t0,CountR
sw $t1,CountJ
sw $t2,CountI
#print result
#----------
li $v0,4 #string print
la $a0, CountR_char
syscall
li $v0,1
add $a0, $t0 ,$0
syscall
li $v0,4 #string print
la $a0, CountJ_char
syscall
li $v0,1
add $a0, $t1 ,$0
syscall
li $v0,4 #string print
la $a0, CountI_char
syscall
li $v0,1
add $a0, $t2 ,$0
syscall
#------------
li $v0,10
syscall
I have made the necessary modifications and print the results
using the system call.