#include /* Sign extends the given field to a 32-bit integer where field is * interpreted an n-bit integer. Look in test_utils.c for examples. */ int sign_extend_number( unsigned int field, unsigned int n : Notes">

NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include "utils.h"
#include <stdio.h>
#include <stdlib.h>

/* Sign extends the given field to a 32-bit integer where field is
* interpreted an n-bit integer. Look in test_utils.c for examples. */
int sign_extend_number( unsigned int field, unsigned int n) {
/* YOUR CODE HERE */
// makes the field have a sign
int first_bit = (field >> (n-1)) & 1;
if (first_bit == 1) {
// you want to fill field up in the front with 1's when making 32 bits .. can use or bitwise
return ((0xFFFFFFFF << n) | field);
}
else {
return field;
}
}

/* Unpacks the 32-bit machine code instruction given into the correct
* type within the instruction struct. Look at types.h */
Instruction parse_instruction(uint32_t instruction_bits) {
Instruction instruction = (Instruction) instruction_bits;
/* YOUR CODE HERE */
//r type
if (instruction.opcode == 0x33 || instruction.opcode == 0x3b) {
instruction.rtype.opcode = instruction.opcode;
}

//i type
if (instruction.opcode == 0x13 || instruction.opcode == 0x3 || instruction.opcode == 0x73 || instruction.opcode == 0xf || instruction.opcode == 0x1b || instruction.opcode == 0x67) {
instruction.itype.opcode = instruction.opcode;
}

//s type
if (instruction.opcode == 0x23) {
instruction.stype.opcode = instruction.opcode;
}


if (instruction.opcode == 0x63) {
instruction.sbtype.opcode = instruction.opcode;
}


//u type
if (instruction.opcode == 0x17 || instruction.opcode == 0x37) {
instruction.utype.opcode = instruction.opcode;
}

//uj type
if (instruction.opcode == 0x6F) {
instruction.ujtype.opcode = instruction.opcode;
}

return instruction;
}

/* Return the number of bytes (from the current PC) to the branch label using the given
* branch instruction */
int get_branch_offset(Instruction instruction) {
/* YOUR CODE HERE */
// so you want to put everything in order from 12, 11, 10, 9, ....., 1, 0
unsigned int imm7 = instruction.sbtype.imm7;
unsigned int imm5 = instruction.sbtype.imm5;

unsigned int twelfth = (imm7 >> 6); //twelfth is either 1 or 0
unsigned int eleventh = (imm5 & 1); // eleventh is either 1 or 0
unsigned int ten_to_5 = (imm7 & 0x3F); // leaves out the twelfth
unsigned int four_to_one = (imm5 >> 1); // shifts the eleventh our of the way

// don't forget about index 0!
//return sign_extend_number(((twelfth << 12) | (eleventh << 11) | (ten_to_5 << 5) | (four_to_one << 1)), 13); // returning the offset
return ((twelfth << 12) | (eleventh << 11) | (ten_to_5 << 5) | (four_to_one << 1)); // returning the offset
}

/* Returns the number of bytes (from the current PC) to the jump label using the given
* jump instruction */
int get_jump_offset(Instruction instruction) {
/* YOUR CODE HERE */
unsigned int imm = instruction.ujtype.imm;

unsigned int twentieth = (imm >> 19); // twentieth is either 1 or 0
unsigned int nineteen_to_12 = (imm & 0xFF);
unsigned int eleventh = ((imm >> 8) & 1); // eleventh is either 1 or 0
unsigned int tenth_to_1 = ((imm >> 9) & 0x3FF);

return sign_extend_number(((twentieth << 20) | (nineteen_to_12 << 12) | (eleventh << 11) | (tenth_to_1 << 1)), 21);
}

/* Returns the byte offset (from the address in rs2) for storing info using the given
* store instruction */
int get_store_offset(Instruction instruction) {
/* YOUR CODE HERE */
unsigned int imm7 = instruction.stype.imm7;
unsigned int imm5 = instruction.stype.imm5;
return sign_extend_number(((imm7 << 5) | (imm5)), 12);
}

void handle_invalid_instruction(Instruction instruction) {
printf("Invalid Instruction: 0x%08xn", instruction.bits);
exit(-1);
}

void handle_invalid_read(Address address) {
printf("Bad Read. Address: 0x%08xn", address);
exit(-1);
}

void handle_invalid_write(Address address) {
printf("Bad Write. Address: 0x%08xn", address);
exit(-1);
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.