Please refer to this link for the main instructions: http://www.co-pylit.org/cou
ID: 3586619 • Letter: P
Question
Please refer to this link for the main instructions: http://www.co-pylit.org/courses/cosc2325/Assignments/Lab2/index.html#lab2 ( all i got from it is we are supposed to complete the class and test it?) here's ALU.cpp #include "ALU.h" #include ALU::ALU(void) { } bool ALU::get_carry( void ) { return carry; } bool ALU::get_overflow( void ) { return overflow; } int16_t ALU::get_result( void ) { return result; } void ALU::add(uint16_t op1, uint16_t op2, int size) { } void ALU::sub(uint16_t op1, uint16_t op2, int size) { } ____________________________________________________________________ here's ALU.h: #pragma once #include class ALU { public: // constructor ALU( void ); int16_t get_result( void ); bool get_carry( void ); bool get_overflow( void ); void add(uint16_t op1, uint16_t op2, int size); void sub(uint16_t op1, uint16_t op2, int size); private: uint16_t result; bool carry; bool overflow; }; _________________________________________ for additional clarification about the files, see this git repo; https://github.com/rblack42/CPUsim Your help would be greatly appreciated.
Explanation / Answer
Carry is related to unsigned data types.
And overflow is related to signed data types.
for that you need two add & two sub functions. Modify the implementation as per that. Function overloading can help.
void add(uint16_t op1, uint16_t op2, int size);
void sub(uint16_t op1, uint16_t op2, int size);
void add(int16_t op1, int16_t op2, int size);
void sub(int16_t op1, int16_t op2, int size);
And modify the carry and overflow condition as per the rules in
http://www.co-pylit.org/courses/cosc2325/Assignments/Lab2/carry-rules.html#carry-rules
Please save these two files and compile. And make your own test cases to test
/********************************/
/*
* ALU.h
*
* Created on: 05-Oct-2017
* Author:
*/
#ifndef ALU_H_
#define ALU_H_
#pragma once
//#include <cstdint> //you can uncomment this line if your compiler supports.
typedef short int16_t; // you can comment this line if your toolchain supports int16_t
typedef unsigned short uint16_t; // you can comment this line if your toolchain supports uint16_t
class ALU {
public:
// constructor
ALU( void );
int16_t get_result( void );
bool get_carry( void );
bool get_overflow( void );
void add(uint16_t op1, uint16_t op2, int size);
void sub(uint16_t op1, uint16_t op2, int size);
private:
int16_t result;
bool carry;
bool overflow;
};
#endif /* ALU_H_ */
/********************************/
/*
* ALU.cpp
*
* Created on: 05-Oct-2017
* Author:
*/
#include <iostream>
#include "ALU.h"
//#include
using namespace std;
int main()
{
ALU alu;
alu.add(100,170,1);
cout<<"carry present: "<<alu.get_carry()<<"Result: "<<alu.get_result()<<endl;
alu.sub(100,170,1);
cout<<"overflow present: "<<alu.get_overflow()<<"Result: "<<alu.get_result()<<endl;
return 0;
}
ALU::ALU(void)
{
result = 0;
carry = false;
overflow = false;
}
bool ALU::get_carry( void )
{
return carry;
}
bool ALU::get_overflow( void )
{
return overflow;
}
int16_t ALU::get_result( void )
{
return result;
}
void ALU::add(uint16_t op1, uint16_t op2, int size)
{
int tmp;
if(size==1)
{
tmp=op1+op2;
if(tmp>0xff)
{
carry=true;
}
else
{
carry=false;
}
result=(unsigned char)tmp; // this will truncate the result after 255
}
else if(size==2)
{
tmp=op1+op2;
if(tmp>0xffff)
{
carry=true;
}
else
{
carry=false;
}
result=(unsigned short)tmp; // this will truncate the result after 65535
}
}
void ALU::sub(uint16_t op1, uint16_t op2, int size)
{
unsigned int tmp;
if(size==1)
{
tmp=op1-op2;
if(tmp>0x7f)
{
overflow=true;
}
else
{
overflow=false;
}
result=(char)tmp;
}
else if(size==2)
{
tmp=op1-op2;
if(tmp>0x7fff)
{
overflow=true;
}
else
{
overflow=false;
}
result=(int16_t)tmp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.