# Lab 9 Name: M V Sonith Roll No.: CS22B036 --- ## Question 1 **Code** ```verilog= module ALU(a,b,s,out1); input [31:0]a; input [31:0]b; input [1:0]s; output [31:0] out1; assign out1 = s[1] ? (s[0] ? a+b : a-b) : (s[0] ? a&b : a|b); endmodule module TB(); reg [31:0]a; reg [31:0]b; reg [1:0]s; wire [31:0]ou; ALU h0(a,b,s,ou); initial begin $monitor("A=%b,B=%b,C=%b,S=%b",a,b,s,ou); a = 32'b00000000000000000000000000000110;b=32'b00000000000000000000000000000101;s=2'b00; #100 a = 32'b00000000000000000000000000000110;b=32'b00000000000000000000000000000101;s=2'b01; #100 a = 32'b00000000000000000000000000000110;b=32'b00000000000000000000000000000101;s=2'b10; #100 a = 32'b00000000000000000000000000000110;b=32'b00000000000000000000000000000101;s=2'b11; end endmodule ``` **Output** ![Screenshot from 2024-04-02 16-34-19](https://hackmd.io/_uploads/BJ0XvvtkC.png) --- ## Question 2 **Code** ```verilog= module simple_processor ( input [7:0] instruction ); reg [7:0] memory [255:0]; reg [7:0] reg_file [15:0]; reg [7:0] immed; reg [7:0] val; always@(*) begin if (instruction[7] == 1'b1) begin val = instruction[6:5]; immed = instruction[4:2]+reg_file[instruction[1:0]]; reg_file[val] = memory[immed]; end else begin val = instruction[6:5]; immed = instruction[4:2]+reg_file[instruction[1:0]]; memory[immed] = reg_file[val]; end end endmodule ``` --- ## Question 3 **Content of inscount0.log** ``` Count 683183 ``` **Code in inscount0.cpp** ```c++= /* * 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; } ``` **C Code** ```c= #include <stdio.h> unsigned long long factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } int main() { int num = 20; unsigned long long fact = factorial(num); printf("Factorial of %d = %llu\n", num, fact); return 0; } ``` **Content of inscount0.log for code** ``` Count 135444 ```