Question

In: Computer Science

For this assignment, you are required to write a MIPS program that reads its own instructions...

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:

  1. First, your program should load the address of its first instruction (0x400000) into a register, and initialize three separate instruction class counters to zero.
  2. 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.)
  3. 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!)

  1. 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.)

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

Solutions

Expert Solution

.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.


Related Solutions

Write a program that reads a file (provided as attachment to this assignment) and write the...
Write a program that reads a file (provided as attachment to this assignment) and write the file to a different file with line numbers inserted at the beginning of each line. Such as Example File Input: This is a test Example File Output 1. This is a test. (Please comment and document your code and take your time no rush).
I have to do the following MIPS coding assignment. Question 1 Write a MIPS program that...
I have to do the following MIPS coding assignment. Question 1 Write a MIPS program that meets the following requirements (NOTE: your program statements should follow the order of the requirements and each requirement should correspond to only one assembly instruction): Loads the hexadecimal equivalent of 23710 into register 13 using an immediate bitwise OR instruction Loads the hexadecimal equivalent of 183410 into register 17 using an immediate bitwise OR instruction Performs the bitwise AND operation on the operands stored...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv),...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv), which contains passenger counts for February 2019 on 200 international flights. The data set (attached below) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation. You write a program that give some summary statistics on this data set. Create a header file named flights.h. In this file, you...
Do Not Use Pseudo Insturctions or li la instructions, etc... Write and test a MIPS program...
Do Not Use Pseudo Insturctions or li la instructions, etc... Write and test a MIPS program consisting of four functions. In the following descriptions, the symbol & means “address of”. void main(): The main function must 1) print your name 2) call the readData function 3) call count function 4) complete the program. The main function must set up all parameters before calling each of the functions. int readData (&array): The starting address of an array is passed to the...
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS)...
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS) which works as a simple calculator. The program will get two integer numbers, and based on the requested operation, the result should be shown to the user. a. The program should print a meaningful phrase for each input, and the result. i. “Enter the first number” ii. “Enter the second number” iii. “Enter the operation type” iv. “The result is” b. The user should...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays the sum and the difference. In the .data section, define two variables num1 and num2 both words. Initialize num1 to 92413 10 and num2 to D4B 16 (use 0xD4B to initialize, Note that D4B is a hexadecimal number). Your main procedure/function should load the values of num1 and num2 into two temporary registers, and display them on the console window. Then add the values...
PART 1: Design a program (using only native MIPS instructions, no pseudo instructions) that will prompt...
PART 1: Design a program (using only native MIPS instructions, no pseudo instructions) that will prompt user to enter three decimal numbers. Each decimal number will not exceed six digits The program can't ask user how many digits will be entered. Both numbers must be stored in memory as NULL terminated strings. The first number must be stored at the memory address 0x10000000. The second number must be stored in the memory 0x10000008. The third number must be stored at...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to...
Assignment #2 will be the construction of a program that reads in an unspecified number of...
Assignment #2 will be the construction of a program that reads in an unspecified number of integers from standard input, performs some calculations on the inputted numbers, and outputs the results of those calculations to standard output. The numbers could be delimited by any kind of whitespace, i.e. tabs, spaces, and lines (Note that if you use the Scanner class, you do not have to worry about these delimiters. They will be taken care of). Your program will continue to...
Assignment Description and Instructions: As a group of prospective entrepreneurs, you are required to prepare a...
Assignment Description and Instructions: As a group of prospective entrepreneurs, you are required to prepare a business plan for a new skin care business (not an existing business/company) to acquire financing. ALL group members are expected to collaborate and contribute to the business plan. Each member will be required to complete peer evaluation of the other team members using the rubric provided. The plan is expected to include: Title page. This should include the company’s name, logo, address and contact...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT