SlideShare a Scribd company logo
TRIBHUVAN UNIVERSITY
INSTITUTE OF ENGINEERING
PULCHOWK CAMPUS
Lab Codes & Output of: Lab 5,6 & 7
Submitted to:
Bikal Adhikari Sir
Department of Electronics and Computer Engineering
Submitted by:
Javed Ansari (077BCT033)
➢ Lab 5: Non- restoring Division Algorithm
Code:
#include<iostream>
#include "logicaloperations.h"
using namespace std;
class Register
{
private:
string data;
public:
Register()
{
} //Default Constructor
Register(string str)
{
data = str;
string temp;
str[0] == '0'? temp = "0" : temp = "1";
for (int i = str.length(); i < 8; i++)
{
data.insert(0,temp);
}
}
Register operator+ (Register operand)
{
Register Result("0");
char carry = '0';
for (int i = 7; i >= 0; i--)
{
Result.data[i] = XOR((XOR(operand.data[i],data[i])),carry);
carry = OR(AND(operand.data[i],data[i]) , AND(carry,
XOR(operand.data[i],data[i])));
}
return Result;
}
Register compliment()
{
Register temp;
temp.data = data;
for (int i = 0; i <= 7; i++)
{
if (temp.data[i] == '1')
temp.data[i] = '0';
else
temp.data[i] = '1';
}
return temp;
}
Register twos_compliment()
{
Register temp("01");
return (compliment() + temp);
}
void Display()
{
cout<<data<<endl;
}
Register operator-(Register operand)
{
return ((*this) + operand.twos_compliment());
}
bool operator < (Register temp)
{
Register temp2 = (*this) - temp;
return temp2.data[0] == '1' ? true : false ;
}
bool operator > (Register temp)
{
Register temp2 = (*this) - temp;
return temp2.data[0] == '1' ? false : true ;
}
bool operator == (Register temp)
{
return data == temp.data ;
}
bool operator >= (Register temp)
{
return ((*this > temp) || (*this)==temp);
}
friend void left_shift(Register&, Register&);
friend void perform_non_restoring_division(Register&, Register&, Register&, int);
friend istream& operator >> (istream& , Register& );
friend ostream& operator << (ostream& , Register& );
};
istream& operator >> (istream& input, Register& value)
{
input >> value.data;
for (int i = (value.data).length(); i < 8; i++)
{
(value.data).insert(0,"0");
}
return input;
}
ostream& operator << (ostream& output, Register& value)
{
output << value.data;
return output;
}
void left_shift(Register& A, Register& Q)
{
for (int i = 0; i < 7; i++)
{
A.data[i] = A.data[i+1];
}
A.data[7] = Q.data[0];
for (int i = 0; i < 7; i++)
{
Q.data[i] = Q.data[i+1];
}
}
void perform_non_restoring_division(Register& A, Register& M, Register& Q, int n)
{
Register Zero("0");
int count = n;
do
{
left_shift(A , Q);
if (A > Zero || A == Zero)
{
A = A - M;
}
else
{
A = A + M;
}
if (A < Zero)
{
Q.data[7] = '0';
}
else
{
Q.data[7] = '1';
}
count--;
}while(count != 0);
if(A < Zero)
{A = A + M;}
}
int main()
{
//M -> divisor, Q -> Dividend, After algo completes, Q -> quotient, A -> Reminder
Register A("0");
Register Q("010110");
Register M("0101");
int n = 8;
perform_non_restoring_division(A, M, Q, n);
cout<<"Quotient:"<<Q<<"t,"<<"Reminder:"<<A;
return 0;
}
Input:
Register Q("010110");
Register M("0101");
Output:
➢ Lab 6: Page Replacement Algorithm
• Using FIFO
Code:
#include<iostream>
#include<queue>
using namespace std;
bool isData_in_frame(int* frame, int data)
{
int frame_size = sizeof(frame)/sizeof(frame[0]);
for(int i=0; i < frame_size;i++)
{
if (data == frame[i])
{
return true;
}
}
return false;
}
int main()
{
int frame_size = 4;
int page_sequence[] = {2,3,2,1,5,2,4,5,3,2,3,2};
int page_sequence_size = sizeof (page_sequence) / sizeof(page_sequence[0]);
int* frames = new int[frame_size];
int currentIndex = 0;
int hit = 0;
int miss = 0;
for (int i = 0; i < page_sequence_size; i++)
{
if(isData_in_frame(frames, page_sequence[i]))
{
hit++;
}
else
{
miss++;
frames[(currentIndex++) % frame_size] = page_sequence[i];
}
}
cout << "Hit: " << hit << endl;
cout << "Miss: " << miss << endl;
float hit_ratio = static_cast<float>(hit) / page_sequence_size;
float miss_ratio = static_cast<float>(miss) / page_sequence_size;
cout << "Hit Ratio: " << hit_ratio * 100 << "%" << endl;
cout << "Miss Ratio: " << miss_ratio * 100 << "%" << endl;
return 0;
}
Output:
• Using LRU
Code:
#include <iostream>
#include <unordered_map>
#include <list>
using namespace std;
int main()
{
int page_sequence[] = {2,3,2,1,5,2,4,5,3,2,3,2};
int page_sequence_size = sizeof(page_sequence) / sizeof(page_sequence[0]);
int frame_size = 4;
unordered_map<int, list<int>::iterator> page_map;
list<int> page_list;
int hit = 0;
int miss = 0;
for (int i = 0; i < page_sequence_size; i++)
{
int page = page_sequence[i];
// Check if page is in memory
if (page_map.find(page) != page_map.end())
{
// Page hit
hit++;
page_list.erase(page_map[page]);
}
else
{
// Page miss
miss++;
if (page_map.size() >= frame_size)
{
// Remove the least recently used page
int lru_page = page_list.back();
page_list.pop_back();
page_map.erase(lru_page);
}
}
// Add the current page as the most recently used
page_list.push_front(page);
page_map[page] = page_list.begin();
}
cout << "Hit: " << hit << endl;
cout << "Miss: " << miss << endl;
float hit_ratio = static_cast<float>(hit) / page_sequence_size;
float miss_ratio = static_cast<float>(miss) / page_sequence_size;
cout << "Hit Ratio: " << hit_ratio * 100 << "%" << endl;
cout << "Miss Ratio: " << miss_ratio * 100 << "%" << endl;
return 0;
}
Output:
For sample: {2,3,2,1,5,2,4,5,3,2,3,2};
For sample: {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1}
Conclusion: LRU has higher hit-ratio than FIFO.
➢ Lab 7: Booth Multiplication Algorithm for signed numbers
Code:
#include<iostream>
#include "logicaloperations.h"
using namespace std;
class Register
{
private:
string data;
public:
Register()
{
} //Default Constructor
Register(string str)
{
data = str;
string temp;
str[0] == '0'? temp = "0" : temp = "1";
for (int i = str.length(); i < 8; i++)
{
data.insert(0,temp);
}
}
Register operator+ (Register operand)
{
Register Result("0");
char carry = '0';
for (int i = 7; i >= 0; i--)
{
Result.data[i] = XOR((XOR(operand.data[i],data[i])),carry);
carry = OR(AND(operand.data[i],data[i]) , AND(carry,
XOR(operand.data[i],data[i])));
}
return Result;
}
Register compliment()
{
Register temp;
temp.data = data;
for (int i = 0; i <= 7; i++)
{
if (temp.data[i] == '1')
temp.data[i] = '0';
else
temp.data[i] = '1';
}
return temp;
}
Register twos_compliment()
{
Register temp("01");
return (compliment() + temp);
}
void Display()
{
cout<<data<<endl;
}
Register operator-(Register operand)
{
return ((*this) + operand.twos_compliment());
}
bool operator < (Register temp)
{
Register temp2 = (*this) - temp;
return temp2.data[0] == '1' ? true : false ;
}
friend void ASR(Register&, Register&, char&);
friend void perform_booth_multiplication(Register&, Register&, Register&, int);
friend istream& operator >> (istream& , Register& );
friend ostream& operator << (ostream& , Register& );
};
istream& operator >> (istream& input, Register& value)
{
input >> value.data;
for (int i = (value.data).length(); i < 8; i++)
{
(value.data).insert(0,"0");
}
return input;
}
ostream& operator << (ostream& output, Register& value)
{
output << value.data;
return output;
}
void ASR(Register& A, Register& Q, char& Qm)
{
Qm = Q.data[7];
for (int i = 7; i > 0; i--)
{
Q.data[i] = Q.data[i-1];
}
Q.data[0] = A.data[7];
for (int i = 7; i > 0; i--)
{
A.data[i] = A.data[i-1];
}
}
void perform_booth_multiplication(Register& A, Register& M, Register& Q, int n)
{
char Qm = '0';
int count = n;
while (count != 0)
{
if (Q.data[7] == '0' && Qm == '1')
{
A = A + M;
}
else if(Q.data[7] == '1' && Qm == '0')
{
A = A - M;
}
ASR(A , Q, Qm);
count--;
}
}
int main()
{
//M -> Multiplier, Q -> Multiplicand, After algo completes, Q -> quotient, A ->
Reminder
Register A("0");
Register Q("1101");
Register M("1011");
int n = 8;
perform_booth_multiplication(A, M, Q, n);
cout<<A<<"t"<<Q;
return 0;
}
Input:
Register Q("1101");
Register M("1011");
Output:

More Related Content

Similar to COA_remaining_lab_works_077BCT033.pdf

Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
C-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorC-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorNeeraj Kaushik
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
Vishvjeet Yadav
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
Prof. Dr. K. Adisesha
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
SWATIKUMARIRA2111030
 
sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdf
MuhammadMaazShaik
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
FareedIhsas
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng웅식 전
 
2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays
kinan keshkeh
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
Ramachendran Logarajah
 
Arrays
ArraysArrays
Programa.eje
Programa.ejePrograma.eje
Programa.ejeguapi387
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semesterDOSONKA Group
 
C++ L04-Array+String
C++ L04-Array+StringC++ L04-Array+String
C++ L04-Array+String
Mohammad Shaker
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
kkkseld
 

Similar to COA_remaining_lab_works_077BCT033.pdf (20)

Sorting programs
Sorting programsSorting programs
Sorting programs
 
C-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorC-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression Calculator
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
 
sodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdfsodapdf-converted into ppt presentation(1).pdf
sodapdf-converted into ppt presentation(1).pdf
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Vcs16
Vcs16Vcs16
Vcs16
 
Arrays
ArraysArrays
Arrays
 
Programa.eje
Programa.ejePrograma.eje
Programa.eje
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semester
 
Unix Programs
Unix ProgramsUnix Programs
Unix Programs
 
C++ L04-Array+String
C++ L04-Array+StringC++ L04-Array+String
C++ L04-Array+String
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
Insertion Sort Code
Insertion Sort CodeInsertion Sort Code
Insertion Sort Code
 

Recently uploaded

Skeem Saam in June 2024 available on Forum
Skeem Saam in June 2024 available on ForumSkeem Saam in June 2024 available on Forum
Skeem Saam in June 2024 available on Forum
Isaac More
 
Hollywood Actress - The 250 hottest gallery
Hollywood Actress - The 250 hottest galleryHollywood Actress - The 250 hottest gallery
Hollywood Actress - The 250 hottest gallery
Zsolt Nemeth
 
Tom Selleck Net Worth: A Comprehensive Analysis
Tom Selleck Net Worth: A Comprehensive AnalysisTom Selleck Net Worth: A Comprehensive Analysis
Tom Selleck Net Worth: A Comprehensive Analysis
greendigital
 
This Is The First All Category Quiz That I Made
This Is The First All Category Quiz That I MadeThis Is The First All Category Quiz That I Made
This Is The First All Category Quiz That I Made
Aarush Ghate
 
From Slave to Scourge: The Existential Choice of Django Unchained. The Philos...
From Slave to Scourge: The Existential Choice of Django Unchained. The Philos...From Slave to Scourge: The Existential Choice of Django Unchained. The Philos...
From Slave to Scourge: The Existential Choice of Django Unchained. The Philos...
Rodney Thomas Jr
 
Christina's Baby Shower Game June 2024.pptx
Christina's Baby Shower Game June 2024.pptxChristina's Baby Shower Game June 2024.pptx
Christina's Baby Shower Game June 2024.pptx
madeline604788
 
A TO Z INDIA Monthly Magazine - JUNE 2024
A TO Z INDIA Monthly Magazine - JUNE 2024A TO Z INDIA Monthly Magazine - JUNE 2024
A TO Z INDIA Monthly Magazine - JUNE 2024
Indira Srivatsa
 
Are the X-Men Marvel or DC An In-Depth Exploration.pdf
Are the X-Men Marvel or DC An In-Depth Exploration.pdfAre the X-Men Marvel or DC An In-Depth Exploration.pdf
Are the X-Men Marvel or DC An In-Depth Exploration.pdf
Xtreame HDTV
 
Maximizing Your Streaming Experience with XCIPTV- Tips for 2024.pdf
Maximizing Your Streaming Experience with XCIPTV- Tips for 2024.pdfMaximizing Your Streaming Experience with XCIPTV- Tips for 2024.pdf
Maximizing Your Streaming Experience with XCIPTV- Tips for 2024.pdf
Xtreame HDTV
 
Meet Crazyjamjam - A TikTok Sensation | Blog Eternal
Meet Crazyjamjam - A TikTok Sensation | Blog EternalMeet Crazyjamjam - A TikTok Sensation | Blog Eternal
Meet Crazyjamjam - A TikTok Sensation | Blog Eternal
Blog Eternal
 
The Evolution of Animation in Film - Mark Murphy Director
The Evolution of Animation in Film - Mark Murphy DirectorThe Evolution of Animation in Film - Mark Murphy Director
The Evolution of Animation in Film - Mark Murphy Director
Mark Murphy Director
 
Panchayat Season 3 - Official Trailer.pdf
Panchayat Season 3 - Official Trailer.pdfPanchayat Season 3 - Official Trailer.pdf
Panchayat Season 3 - Official Trailer.pdf
Suleman Rana
 
Reimagining Classics - What Makes a Remake a Success
Reimagining Classics - What Makes a Remake a SuccessReimagining Classics - What Makes a Remake a Success
Reimagining Classics - What Makes a Remake a Success
Mark Murphy Director
 
Scandal! Teasers June 2024 on etv Forum.co.za
Scandal! Teasers June 2024 on etv Forum.co.zaScandal! Teasers June 2024 on etv Forum.co.za
Scandal! Teasers June 2024 on etv Forum.co.za
Isaac More
 

Recently uploaded (14)

Skeem Saam in June 2024 available on Forum
Skeem Saam in June 2024 available on ForumSkeem Saam in June 2024 available on Forum
Skeem Saam in June 2024 available on Forum
 
Hollywood Actress - The 250 hottest gallery
Hollywood Actress - The 250 hottest galleryHollywood Actress - The 250 hottest gallery
Hollywood Actress - The 250 hottest gallery
 
Tom Selleck Net Worth: A Comprehensive Analysis
Tom Selleck Net Worth: A Comprehensive AnalysisTom Selleck Net Worth: A Comprehensive Analysis
Tom Selleck Net Worth: A Comprehensive Analysis
 
This Is The First All Category Quiz That I Made
This Is The First All Category Quiz That I MadeThis Is The First All Category Quiz That I Made
This Is The First All Category Quiz That I Made
 
From Slave to Scourge: The Existential Choice of Django Unchained. The Philos...
From Slave to Scourge: The Existential Choice of Django Unchained. The Philos...From Slave to Scourge: The Existential Choice of Django Unchained. The Philos...
From Slave to Scourge: The Existential Choice of Django Unchained. The Philos...
 
Christina's Baby Shower Game June 2024.pptx
Christina's Baby Shower Game June 2024.pptxChristina's Baby Shower Game June 2024.pptx
Christina's Baby Shower Game June 2024.pptx
 
A TO Z INDIA Monthly Magazine - JUNE 2024
A TO Z INDIA Monthly Magazine - JUNE 2024A TO Z INDIA Monthly Magazine - JUNE 2024
A TO Z INDIA Monthly Magazine - JUNE 2024
 
Are the X-Men Marvel or DC An In-Depth Exploration.pdf
Are the X-Men Marvel or DC An In-Depth Exploration.pdfAre the X-Men Marvel or DC An In-Depth Exploration.pdf
Are the X-Men Marvel or DC An In-Depth Exploration.pdf
 
Maximizing Your Streaming Experience with XCIPTV- Tips for 2024.pdf
Maximizing Your Streaming Experience with XCIPTV- Tips for 2024.pdfMaximizing Your Streaming Experience with XCIPTV- Tips for 2024.pdf
Maximizing Your Streaming Experience with XCIPTV- Tips for 2024.pdf
 
Meet Crazyjamjam - A TikTok Sensation | Blog Eternal
Meet Crazyjamjam - A TikTok Sensation | Blog EternalMeet Crazyjamjam - A TikTok Sensation | Blog Eternal
Meet Crazyjamjam - A TikTok Sensation | Blog Eternal
 
The Evolution of Animation in Film - Mark Murphy Director
The Evolution of Animation in Film - Mark Murphy DirectorThe Evolution of Animation in Film - Mark Murphy Director
The Evolution of Animation in Film - Mark Murphy Director
 
Panchayat Season 3 - Official Trailer.pdf
Panchayat Season 3 - Official Trailer.pdfPanchayat Season 3 - Official Trailer.pdf
Panchayat Season 3 - Official Trailer.pdf
 
Reimagining Classics - What Makes a Remake a Success
Reimagining Classics - What Makes a Remake a SuccessReimagining Classics - What Makes a Remake a Success
Reimagining Classics - What Makes a Remake a Success
 
Scandal! Teasers June 2024 on etv Forum.co.za
Scandal! Teasers June 2024 on etv Forum.co.zaScandal! Teasers June 2024 on etv Forum.co.za
Scandal! Teasers June 2024 on etv Forum.co.za
 

COA_remaining_lab_works_077BCT033.pdf

  • 1. TRIBHUVAN UNIVERSITY INSTITUTE OF ENGINEERING PULCHOWK CAMPUS Lab Codes & Output of: Lab 5,6 & 7 Submitted to: Bikal Adhikari Sir Department of Electronics and Computer Engineering Submitted by: Javed Ansari (077BCT033)
  • 2. ➢ Lab 5: Non- restoring Division Algorithm Code: #include<iostream> #include "logicaloperations.h" using namespace std; class Register { private: string data; public: Register() { } //Default Constructor Register(string str) { data = str; string temp; str[0] == '0'? temp = "0" : temp = "1"; for (int i = str.length(); i < 8; i++) { data.insert(0,temp); } } Register operator+ (Register operand) { Register Result("0"); char carry = '0'; for (int i = 7; i >= 0; i--) { Result.data[i] = XOR((XOR(operand.data[i],data[i])),carry); carry = OR(AND(operand.data[i],data[i]) , AND(carry, XOR(operand.data[i],data[i]))); } return Result; } Register compliment() {
  • 3. Register temp; temp.data = data; for (int i = 0; i <= 7; i++) { if (temp.data[i] == '1') temp.data[i] = '0'; else temp.data[i] = '1'; } return temp; } Register twos_compliment() { Register temp("01"); return (compliment() + temp); } void Display() { cout<<data<<endl; } Register operator-(Register operand) { return ((*this) + operand.twos_compliment()); } bool operator < (Register temp) { Register temp2 = (*this) - temp; return temp2.data[0] == '1' ? true : false ; } bool operator > (Register temp) { Register temp2 = (*this) - temp; return temp2.data[0] == '1' ? false : true ; } bool operator == (Register temp) { return data == temp.data ; } bool operator >= (Register temp) { return ((*this > temp) || (*this)==temp); }
  • 4. friend void left_shift(Register&, Register&); friend void perform_non_restoring_division(Register&, Register&, Register&, int); friend istream& operator >> (istream& , Register& ); friend ostream& operator << (ostream& , Register& ); }; istream& operator >> (istream& input, Register& value) { input >> value.data; for (int i = (value.data).length(); i < 8; i++) { (value.data).insert(0,"0"); } return input; } ostream& operator << (ostream& output, Register& value) { output << value.data; return output; } void left_shift(Register& A, Register& Q) { for (int i = 0; i < 7; i++) { A.data[i] = A.data[i+1]; } A.data[7] = Q.data[0]; for (int i = 0; i < 7; i++) { Q.data[i] = Q.data[i+1]; } } void perform_non_restoring_division(Register& A, Register& M, Register& Q, int n) { Register Zero("0"); int count = n; do { left_shift(A , Q); if (A > Zero || A == Zero) { A = A - M; } else { A = A + M;
  • 5. } if (A < Zero) { Q.data[7] = '0'; } else { Q.data[7] = '1'; } count--; }while(count != 0); if(A < Zero) {A = A + M;} } int main() { //M -> divisor, Q -> Dividend, After algo completes, Q -> quotient, A -> Reminder Register A("0"); Register Q("010110"); Register M("0101"); int n = 8; perform_non_restoring_division(A, M, Q, n); cout<<"Quotient:"<<Q<<"t,"<<"Reminder:"<<A; return 0; } Input: Register Q("010110"); Register M("0101"); Output:
  • 6. ➢ Lab 6: Page Replacement Algorithm • Using FIFO Code: #include<iostream> #include<queue> using namespace std; bool isData_in_frame(int* frame, int data) { int frame_size = sizeof(frame)/sizeof(frame[0]); for(int i=0; i < frame_size;i++) { if (data == frame[i]) { return true; } } return false; } int main() { int frame_size = 4; int page_sequence[] = {2,3,2,1,5,2,4,5,3,2,3,2}; int page_sequence_size = sizeof (page_sequence) / sizeof(page_sequence[0]); int* frames = new int[frame_size]; int currentIndex = 0; int hit = 0; int miss = 0; for (int i = 0; i < page_sequence_size; i++) { if(isData_in_frame(frames, page_sequence[i])) { hit++; } else { miss++; frames[(currentIndex++) % frame_size] = page_sequence[i]; } }
  • 7. cout << "Hit: " << hit << endl; cout << "Miss: " << miss << endl; float hit_ratio = static_cast<float>(hit) / page_sequence_size; float miss_ratio = static_cast<float>(miss) / page_sequence_size; cout << "Hit Ratio: " << hit_ratio * 100 << "%" << endl; cout << "Miss Ratio: " << miss_ratio * 100 << "%" << endl; return 0; } Output: • Using LRU Code: #include <iostream> #include <unordered_map> #include <list> using namespace std; int main() { int page_sequence[] = {2,3,2,1,5,2,4,5,3,2,3,2}; int page_sequence_size = sizeof(page_sequence) / sizeof(page_sequence[0]); int frame_size = 4; unordered_map<int, list<int>::iterator> page_map; list<int> page_list; int hit = 0; int miss = 0; for (int i = 0; i < page_sequence_size; i++) { int page = page_sequence[i]; // Check if page is in memory if (page_map.find(page) != page_map.end()) { // Page hit hit++;
  • 8. page_list.erase(page_map[page]); } else { // Page miss miss++; if (page_map.size() >= frame_size) { // Remove the least recently used page int lru_page = page_list.back(); page_list.pop_back(); page_map.erase(lru_page); } } // Add the current page as the most recently used page_list.push_front(page); page_map[page] = page_list.begin(); } cout << "Hit: " << hit << endl; cout << "Miss: " << miss << endl; float hit_ratio = static_cast<float>(hit) / page_sequence_size; float miss_ratio = static_cast<float>(miss) / page_sequence_size; cout << "Hit Ratio: " << hit_ratio * 100 << "%" << endl; cout << "Miss Ratio: " << miss_ratio * 100 << "%" << endl; return 0; } Output: For sample: {2,3,2,1,5,2,4,5,3,2,3,2}; For sample: {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1} Conclusion: LRU has higher hit-ratio than FIFO.
  • 9. ➢ Lab 7: Booth Multiplication Algorithm for signed numbers Code: #include<iostream> #include "logicaloperations.h" using namespace std; class Register { private: string data; public: Register() { } //Default Constructor Register(string str) { data = str; string temp; str[0] == '0'? temp = "0" : temp = "1"; for (int i = str.length(); i < 8; i++) { data.insert(0,temp); } } Register operator+ (Register operand) { Register Result("0"); char carry = '0'; for (int i = 7; i >= 0; i--) { Result.data[i] = XOR((XOR(operand.data[i],data[i])),carry); carry = OR(AND(operand.data[i],data[i]) , AND(carry, XOR(operand.data[i],data[i]))); } return Result; } Register compliment() { Register temp; temp.data = data; for (int i = 0; i <= 7; i++) { if (temp.data[i] == '1') temp.data[i] = '0'; else temp.data[i] = '1'; }
  • 10. return temp; } Register twos_compliment() { Register temp("01"); return (compliment() + temp); } void Display() { cout<<data<<endl; } Register operator-(Register operand) { return ((*this) + operand.twos_compliment()); } bool operator < (Register temp) { Register temp2 = (*this) - temp; return temp2.data[0] == '1' ? true : false ; } friend void ASR(Register&, Register&, char&); friend void perform_booth_multiplication(Register&, Register&, Register&, int); friend istream& operator >> (istream& , Register& ); friend ostream& operator << (ostream& , Register& ); }; istream& operator >> (istream& input, Register& value) { input >> value.data; for (int i = (value.data).length(); i < 8; i++) { (value.data).insert(0,"0"); } return input; } ostream& operator << (ostream& output, Register& value) { output << value.data; return output; } void ASR(Register& A, Register& Q, char& Qm) { Qm = Q.data[7]; for (int i = 7; i > 0; i--) { Q.data[i] = Q.data[i-1];
  • 11. } Q.data[0] = A.data[7]; for (int i = 7; i > 0; i--) { A.data[i] = A.data[i-1]; } } void perform_booth_multiplication(Register& A, Register& M, Register& Q, int n) { char Qm = '0'; int count = n; while (count != 0) { if (Q.data[7] == '0' && Qm == '1') { A = A + M; } else if(Q.data[7] == '1' && Qm == '0') { A = A - M; } ASR(A , Q, Qm); count--; } } int main() { //M -> Multiplier, Q -> Multiplicand, After algo completes, Q -> quotient, A -> Reminder Register A("0"); Register Q("1101"); Register M("1011"); int n = 8; perform_booth_multiplication(A, M, Q, n); cout<<A<<"t"<<Q; return 0; } Input: Register Q("1101"); Register M("1011"); Output: