I need help wtih Assembly Language Programming using the MARS MIPS Simulator: Wr
ID: 3591782 • Letter: I
Question
I need help wtih Assembly Language Programming using the MARS MIPS Simulator:
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:
First, your program should load the address of its first instruction (0x400000) into a register, and initialize three separate instruction class counters to zero.
Next, your program should enter a loop that reads each instruction from memory. For each instruction, examine the “op code” field to determine whether it is an R-type, I-type, or J-type instruction, and increment the appropriate instruction class counter. (For the purpose of this assignment, you may assume that all instructions with an op code of 0 are R-type instructions; all instructions with an op code equal to either 2 or 3 are J-type instructions; and all other instructions are I-type instructions.)
Your source program’s final two instructions should be
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!)
Finally, save the values of your instruction class counters to variables declared in the data segment of your program. (For testing purposes, I recommend also using syscalls to display the three integer counts to the screen; if you do, be careful not to halt on these additional syscall instructions.)
Explanation / Answer
.data
#Inst: .#
CountR: .word 0
CountJ: .word 0
CountI: .word 0
CountR_char : .asciiz "CountR is "
CountJ_char : .asciiz " CountJ is "
CountI_char : .asciiz " CountI 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.
PLease let me know if you need any thing else.
View comments (2)
More Answers
iitkgp123
iitkgp123 answered this Was this answer helpful?
0
0
25 answers
.data
#Inst: .#
CountR: .word 0
CountJ: .word 0
CountI: .word 0
TypeR: .word 0 #should be something like this but I don't know what exactly it should be
TypeJ1: .word 2
TypeJ2: .word 3
EndI: .word 0
#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)
#shift it right logical by 26 ,makes it 0000xxxx, $s0= bits 31-26 only
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
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: #print result
#----------
li $v0,4
la $a0, CountR
syscall
li $v0,4
la $a0, CountJ
syscall
li $v0,4
la $a0, CountI
syscall
#------------
li $v0,10
syscall