# Lab 9 Name: I.Kalyan Anudeep Roll No.: CS22B025 --- ## Question 1 **Code** (if required) ```assembly= module alu(input [3:0] A,B, input [1:0] c, output reg [3:0] result); always @(*) begin case(c) 2'b00: result = A + B; 2'b01: result = A - B; 2'b10: result = A & B; 2'b11: result = A | B; default: result=4'b0; endcase end endmodule module mux4to1(input [3:0] in0,in1,in2,in3, input [1:0] sel, output reg [3:0] out); always @(*) begin case(sel) 2'b00: out=in0; 2'b01: out=in1; 2'b10: out=in2; 2'b11: out=in3; default: 4'b0; endcase end endmodule module top(input [3:0] A,B, input [1:0] c, output reg [3:0] result); wire [3:0] alu_result; alu alu_inst(A,B,c,alu_result); mux4to1 mux_inst(alu_result,4'b0,4'b0,4'b0,c,result); endmodule ``` ## Question 2 **Code** (if required) ```assembly= module simple_processor ( input [7:0] instruction ); reg [7:0] memory [255:0]; reg [7:0] reg_file [15:0]; reg [2:0] opcode; reg [3:0] rs; reg [3:0] rd; reg [2:0] offset; reg [7:0] data; reg [7:0] addr; reg [7:0] base_addr; assign opcode = instruction[7:5]; assign rs = instruction[4:2]; assign rd = instruction[1:0]; assign offset = instruction[4:2]; always @(*) begin case(opcode) 3'b000: begin // Load operation (lw) addr = base_addr + offset; data = memory[addr]; reg_file[rd] = data; end 3'b001: begin // Store operation (sw) addr = base_addr + offset; memory[addr] = reg_file[rd]; end endcase end endmodule ``` ## Question 3 **Code** (if required) ```assembly= ~contents of inscount0.log count 712683 ~code of inscount0.cpp /* * Copyright (C) 2004-2021 Intel Corporation. * SPDX-License-Identifier: MIT */ #include <iostream> #include <fstream> #include "pin.H" using std::cerr; using std::endl; using std::ios; using std::ofstream; using std::string; ofstream OutFile; // The running count of instructions is kept here // make it static to help the compiler optimize docount static UINT64 icount = 0; // This function is called before every instruction is executed VOID docount() { icount++; } // Pin calls this function every time a new instruction is encountered VOID Instruction(INS ins, VOID* v) { // Insert a call to docount before every instruction, no arguments are passed INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END); } KNOB< string > KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "inscount.out", "specify output file name"); // This function is called when the application exits VOID Fini(INT32 code, VOID* v) { // Write to a file since cout and cerr maybe closed by the application OutFile.setf(ios::showbase); OutFile << "Count " << icount << endl; OutFile.close(); } /* ===================================================================== */ /* Print Help Message */ /* ===================================================================== */ INT32 Usage() { cerr << "This tool counts the number of dynamic instructions executed" << endl; cerr << endl << KNOB_BASE::StringKnobSummary() << endl; return -1; } /* ===================================================================== */ /* Main */ /* ===================================================================== */ /* argc, argv are the entire command line: pin -t <toolname> -- ... */ /* ===================================================================== */ int main(int argc, char* argv[]) { // Initialize pin if (PIN_Init(argc, argv)) return Usage(); OutFile.open(KnobOutputFile.Value().c_str()); // Register Instruction to be called to instrument instructions INS_AddInstrumentFunction(Instruction, 0); // Register Fini to be called when the application exits PIN_AddFiniFunction(Fini, 0); // Start the program, never returns PIN_StartProgram(); return 0; } ~My c++ code is #include<iostream> using namespace std; int main() { int sum=0; for(int i=1;i<=10;i++) { sum=sum+i; } } ~After running my c++ file number of instructions or instruction count is 2150298 ```