Question
The following code is a skeleton for a program for part B. The code calls the function decode_instruction (B.1) that will decode the first instruction
The following code is a skeleton for a program for part B. The code calls the function decode_instruction (B.1) that will decode the first instruction of function func, and print its fields. Then, the program calls function encode_instruction (B.2) that encodes a different instruction and prints its hexadecimal value. The remaining code simply calls the func function and prints its return value.
Note that the function label func: is a label just like the ones you declare variables with. As such, when you insert it in the code it works just like a variable would. E.g. la a0, func, sets the value of register a0 to the memory address of the first instruction of the func function.
.data returned: .asciiz "The function returned " .text .globl main main: # Decode the first instruction in function "func" (addi v0, zero, 0x1337) la a0, func jal decode_instruction # Encode the instruction addi v0, zero, 0x1234 li a0, 8 li a1, 0 li a2, 2 li a3, 0x1234 jal encode_instruction # Print string la a0, returned li v0, 4 syscall # Call function jal func # Print return value move a0, v0 li v0, 34 syscall # Exit li v0, 10 syscall func: addi v0, zero, 0x1337 jr ra # Prints the different fields of an I-type instruction # decode_instruction(a0: memory address of instruction) decode_instruction: # Implement B.1 here jr ra # Encodes the fields of an I-type instruction and returns it # encode_instruction(a0: opcode, a1: rs, a2: rt, a3: immediate) encode_instruction: # Implement B.2 here jr ra
B.1: Using the skeleton code above, implement the decode_instruction function. The only input parameter is the memory address of the instruction. The function decodes the memory and prints each field. The expected output is as follows: |
opcode = 8 rs = 0 rt = 2 immediate = 0x00001337
Note: To print an hex number use syscall number 34.
Note 2: MARS does not allow you (by default) to access the program memory! For a good reason!!!! But we are feeling adventurous (right? :)! To be able to read the contents of the program memory, select the option in Settings > Self-modifying code.
B.2: Using the skeleton code above, implement the encode_instruction function. The inputs are the opcode, rs, rt, and the 16-bit immediate. The function encodes the fields into an instruction and returns it. The function should also print the instruction, as such: |
0x20021234
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started