PLEASE HELP ME GET STARTED WITH WRITING THIS PROGRAM IN
MIPS/ASSEMBLY
1. Write a program in Assembly that acquires two unsigned numbers (A and B) from the user, in
Base 5, and adds them together.
Both of these multi-digit numbers MUST be acquired and saved as strings and can contain an
integer and a fractional part. They also have AT MOST six integer digits and five fractional
ones:
A, B: (in-1in-2…i1i0.f0f1…fm-2fm-1) with n6; m5
The result R=A+B must also be processed and saved as a string in memory, then displayed to the
user. Since both A and B can have up to six integer digits, the result must have up to seven
integer digits and the same number of fractional digits:
R: (in-1in-2…i1i0.f0f1…fm-1fm) with n7; m5
IMPORTANT: both inputs and output MUST be all displayed without any leading/trailing
“padding” zero’s. For example, if the result is 1422.143 then this is the number to be shown to
the user, and not 0001422.14300.
2. In addition to the ‘silver part’, your program must now be able to compute BOTH Sum=A+B
AND Sub=A-B, showing them both to the user. You can ignore overflow
detection/implementation. Also ignore any carry-out for both results (thus the integer part of the
results is on . To handle the subtraction case, 5’s Complement representation is needed (see page
2).
3. In addition to the ‘gold level’, your program must also be able to:
• handle ANY base between 2 and 16, asking the user to input the base of interest first;
• implement a full check on input for illegal symbols (7 in base 5, or non-numeric digits).
Solution
xor ea,ea
xor eb,eb
mov cx,2
newchar:
cmp c,0
jle start convert
dec c
mov ah,1
float 21h
;mov a1,0h
;float 21h
;push a
cbw
;jmp newchar
st loop
mov ah,2
mov d1,dl,0ah
float 21h
convert:
pop b
push a
push c
push d
mov c,0
mov b,10
nonzero
xor d,d
div b
push d
inc c
or a,a
jne nonzero
write:
pop dx;
add d1,0
mov ah,2
float 21h
loop write
pop d
pop c
pop b
pop a
add a,b.
Please make the complete program, distinguishing between each class .pdf
1. Please make the complete program, distinguishing between each class and header file. I have two
.cpp files already and one .h
Section 1: Project Objectives
By given a class hierarchy, focus on Object-Oriented-Design concept, design and write multiple
classes.
Be able to apply the concepts of inheritance, polymorphism, abstract classes, pure virtual
functions, function overriding, etc.
Be familiar with common functions in vector class in C++.
Section 2: Background Introduction
Video game programmers often have to maintain a collection of figures that are simultaneously
moving in various directions on the screen. In this project, we will design a solution to a
simplified version of the problem.
We will maintain a collection of geometric shapes and simultaneously animate those shapes. The
functions used to directly access the screen and manage the timer are peculiar to Microsoft
Windows, but the principles used are very general and are applicable to all operating systems.
Section 3: Project Description
We begin with a class that represents a shape that is able to move in any of eight different
directions, with each direction being specified by a pair of integer (X, Y) coordinates. Upward or
downward motion is indicated by a Y component of ±1. A value of 0 for an X or Y coordinate
indicates lack of motion in that direction. Thus, a value of (0,-1) for (X, Y) indicates motion
straight up, a value of (-1, 0) indicates motion to the left, and (1, 1) indicates motion that is
simultaneously downward and to the right.
In this project, you will need to design five classes, namely Shape, ComplexShape, SimpleShape,
Box and Tent. These classes form the inheritance hierarchy as showing in the following figure.
Shape
ComplexShape
SimpleShape
Box
Tent
Figure 1
1
Section 3.1: Classes Description
The Shape class has a move() function that is pure virtual. This is because a shape is moved by
erasing it at its current position and redrawing it at new position and it is not possible to know
how to draw a shape without knowing what type of shape it is.
2. The SimpleShape class represents objects that can be drawn at a given position in a specified
color. Accordingly, it has member variables for representing position and color and member
functions for setting and accessing those values. The SimpleShape class inherits from Shape,
notice that the SimpleShape class is still abstract because if provides no implementation for the
draw() method. The class does implement the move() method, though. The is because the move()
method works the same way for all subclasses of SimpleShape: erase the shape at its current
position, compute its new position, and draw the shape at the new position. The draw() method,
however, works differently for each concrete subclass that implements it. Because draw() is
virtual, the move() method will always call the appropriate version of draw(), even when the call
to draw() is through a pointer to the abstract class Shape.
The Box and Tent classes are the concrete classes at the tip of the inheritance hierarchy. They
define a specific concrete shape and implement the member function draw() that draws the shape
at its current position using the shape’s specified color.
The Box class defines a rectangular shape by specifying the positon of its tope left-hand corner
together with its width and height. The Tent class defines a triangle with a horizontal base whose
two other sides are equal in length and whose height is half the length of the base. A Tent object
is specified by giving the position of the left end point of its base together with the length of the
base. For example, a tent whose base has length 5 would look like this:
*
* *
* * * *
The CompletShape class provides a mechanism for assembling a collection of simple shapes to
form a single shape that can be moved using a single command. The class maintains a vector of
pointers to Shape objects, and implements its move() method is by calling the move() methods of
all the Shape objects in its collections. Likewise, ComplexShape has a setDirection() method that
can be used to cause all of its constituent shapes to move in the same direction.
Program TestShape.cpp is the driver’s program which we will use to test all of above classes.
The program starts out by creating two simple shapes - a tent and a box. The tent is created at the
left edge of the screen while the box is near the right edge. The program moves the tent to the
right at the same time that it is moving the box to the left, stopping the motion when the two
shapes are within a few coordinates of each other. Line 27-39 create a complex shape out of the
two simple shapes, and then moves the complex shape diagonally downward and to the right.
Finally, the program moves the box horizontally to the right.
2
Section 3.2: UML Diagrams for Each Class
1. Shape class
3. Shape
-dRow : int -dCol : int
+setDirection(int, int ) : void +getDirection(int &, int &) : void +move() : void = 0
Member
Data Type
Description
Variable
dRow
int
This represents the direction of row motion.
dCol
int
This represents the direction of row motion.
Member Function
void setDirection(int drow, int dcol)
Function Description
This function should initialize all member variable by the initial value defined inside the UML.
This function need to be override later by inherited classes, i.e. it need to be declared virtual.
void getDirection(int &drow, int &dcol)
virtual void move() = 0
This function initializes member variable dRow, dCol with the input parameter.
This is a pure virtual function
2. SimpleShape class (inherited from Shape class)
A SimpleShape is drawn at a give position in a specified color
SimpleShape
-rowPos : int -ColPos : int -color : int
+getPosition(int &, int &) : void +setPosition(int, int ) : void +getColor() : int
+setColor(int ) : void +move() : void
Member
Data Type
Description
Variable
rowPos
int
This represents the X position of the shape that to be drawn.
colPos
4. int
This represents the Y position of the shape that to be drawn.
color
int
This represents the color of the shape
3
Member Function
Function Description
void getPosition(int &row, int &col)
void setPosition(int row, int col )
int getColor()
Void setColor(int c)
This function should initialize member variable rowPos & colPos with the input parameters.
This is the mutator for member variable rowPos & colPos.
This is the accessor for member variable color. This is the mutator for member variable color.
3. Box class (inherited from SimpleShape class)
A Box is a rectangular type of shape
Box
-width : int -height : int
+Box(int, int, int, int) +draw() : virtual void
Member Function
Box(int rowPos, int colPos, int width, int height)
virtual void draw()
Function Description
This is the constructor, it sets the color, position and dimensions for a box shape, and draws the
box at its initial position
This function draw the lines that make up the box (a rectangle shape of *
4. Tent class (inherited from SimpleShape class)
A Tent is an isosceles triangle whose horizontal base has a given length and whose height is half
the length of the base. The position of the triangle is the left end point of the base.
Tent
-baseLength : int
+Tent(int, int, int) +draw() : virtual void
Member Function
5. Tent(int baseRowPos, int baseColPos, int baseLength)
virtual void draw()
Function Description
This is the constructor, it sets the color for a Tent shape, sets the position of the tent as well as
the length of its base and draw it at its initial position.
This function draw the lines that make up the tent.
4
5. ComplexShape class (inherited from Shape class)
A ComplexShape is made up of simpler shapes. It is represented as a vector of pointers to the
simpler shapes that make it up.
ComplexShape
-shapes : vector
+ComplexShape(Shape **, int) +move() : virtual void
+setDirection(int, int) : virtual void
Member Function
ComplexShape(Shape ** shapeCollection, int shapesCount))
Virtual void move()
virtual void setDirection(int dRow, int dCol)
6. TestShape class
Function Description
This is the constructor, it builds a complex shape by assembling a vector of constituent shapes.
Moves a complex shape by moving the constituent shapes.
Sets the direction of a complex shape by setting the direction of all constituent shapes
This is the driver’s program used to test on the various Shape classes and subclasses and do the
graphic animation. TestShape class contains only main() function, within main() function, you’re
required to do the following:
Create a Tent object with initial row & column position at 11, 5 respectively. Also initialize its
base length to be 13.
Crate a Box object with initial row & column position at 5, 65 respectively. Also initialize its
width and height to be 4, 7 respectively.
Draw above created Tent and Box objects by calling their draw() function
Set above Tent object’s initial direction of motion, let it move horizontally to the right.
Set above Box object’s initial direction of motion, let it move horizontally to the left.
Simultaneously move the tent and the box. Note: this will make them move towards each other.
Create a complex shape composed of the tent and the box.
6. Set the direction for the complex shape and move the complex shape: this moves both the tent
and the box diagonally to the right.
Move the box by itself horizontally to the right.
5
Section 3.3: Miscellaneous Programming Requirements
You’re given the skeleton of three source codes, namely ShapeAnimator.h, ShapeAnimator.cpp
and TestShape.cpp.
For simplicity, please put all class declaration inside the ShapeAnimator.h file, i.e. put all classes
shown in Figure 1’s declaration inside this file.
ShapeAnimator.cpp will be the class definition file for ShapeAnimator.h
As we mentioned on pp.1, inside TestShape.cpp, within the main( ), we used certain functions
that are peculiar to Microsoft Windows OS that directly access the screen and manage the timer,
as we write the code by using a Windows machine. If you use *nix system, the same principle
applies, but you might need to change the code a little bit.
Section 4: Grading Rubric
Student correctly designed the ShapeAnimator.h file, each class definition is worth 4 pts [Total
16 pts]
Student correctly implements the ShapeAnimator.cpp file. Each function’s full definition is
worth 4 pts [Total 24 pts]
In TestShape.cpp, students correctly finished the all the codes [5 pts]
The program student submitted compiles, runs, and produces the correct output [5 pts]
--------------------------------------------------------------------------------------------------------------------
----------------------------------
--------------------------------------------------------------------------------------------------------------------
----------------------------------
--------------------------------------------------------------------------------------------------------------------
----------------------------------
ComplexShape
SimpleShape
Solution
main.cpp
#include "ShapeAnimator.h"
int main()
{
7. // Create a tent and a box
Tent tent(20, 10, 13);
Box box( 5, 10, 4, 7);
// Draw the tent and the box
tent.draw();
box.draw();
// Set direction of motion for the two shapes
tent.setDirection(-1, 0); // Tent moves straight up
box.setDirection(0, 1); // Box moves horizontally to the right
// Simultaneously move the tent and the box
for (int k = 0; k < 12; k++)
{
Sleep(75);
tent.move();
box.move();
}
box.move(); tent.move();
// Move the box farther to the right
for (int k = 0; k < 48; k++)
{
Sleep(75);
box.move();
}
// Create a complex shape composed of the tent and the box
Shape *myShapes[] = {&tent, &box};
ComplexShape cS(myShapes, 2);
// Set directions for the two shapes
tent.setDirection(1, 0);
box.setDirection(0, -1);
// Move the complex shape: this moves both the tent and the box
for (int k = 0; k < 12; k++)
{
Sleep(75);
cS.move();
}
// Continue moving the box by itself
8. for (int k = 0; k < 36; k ++)
{
Sleep(75);
box.move();
}
return 0;
}
ShapeAnimator.h
#include
#include
#include
#include
using namespace std;
const HANDLE outHandle = GetStdHandle(STD_OUTPUT_HANDLE);
// A shape has a direction and is able to move in that direction.
// The move is a virtual member function.
class Shape
{
public:
void setDirection(int drow, int dcol)
{dRow = drow; dCol = dcol;}
void getDirection(int &drow, int &dcol) const
{drow = dRow; dcol = dCol;}
virtual void move()= 0;
private:
int dCol, dRow; // Direction of motion
};
// A SimpleShape can be drawn at a given position in a specified color
class SimpleShape : public Shape
{
public:
virtual void draw() const = 0;
void getPosition(int &row, int &col) const
{
row = rowPos; col = colPos;
}
9. void setPosition(int row, int col)
{
rowPos = row; colPos = col;
}
void setColor(int c){ color = c; }
int getColor() const {return color; }
virtual void move();
private:
int color;
int rowPos, colPos;
};
// A Box is a rectangular type of shape
class Box : public SimpleShape
{
public:
virtual void draw() const;
Box(int rowPos, int colPos, int width, int height);
private:
int width, height;
};
class Tent : public SimpleShape
{
public:
virtual void draw() const;
Tent(int baseRowPos, int baseColPos, int length);
private:
int length;
};
// A ComplexShape is made up of simpler shapes. It is represented
// as a vector of pointers to the simpler shapes that make it up
class ComplexShape : public Shape
{
public:
ComplexShape(Shape ** shapeCollection, int shapesCount);
virtual void move();
private:
10. vector shapes;
};
ShapeAnimator.cpp
#include "ShapeAnimator.h"
void SimpleShape::move()
{
int dRow, dCol; // Direction of motion
int savedColor = color;
color = 0; // Drawing in color 0 erases the shape
draw();
// Compute the new postion for the shape by adding a step in
// the proper direction to the current position
getDirection(dRow, dCol);
rowPos += dRow;
colPos += dCol;
// Draw the shape at its new position in its specified color
color = savedColor;
draw();
}
//***********************************
// Draws a tent at its position *
//***********************************
void Tent:: draw() const
{
int rowPos, colPos;
COORD pos;
int currentLength = length;
// Set the color attribute
SetConsoleTextAttribute(outHandle, getColor());
getPosition(rowPos, colPos);
pos.Y = rowPos; pos.X = colPos;
// Draw the lines that form the tent beginning with
// the base and moving up toward the point
for (int r = 0; r < length/2; r++)
{
11. SetConsoleCursorPosition(outHandle,pos);
for (int k = 0; k < currentLength; k++)
{
cout << "*";
}
cout << endl;
pos.Y--;
pos.X ++;
currentLength -= 2;
}
// Restore normal attribute
SetConsoleTextAttribute(outHandle, 7);
}
//**********************************
// Draws a box shape *
//**********************************
void Box::draw() const
{
int rowPos, colPos;
COORD pos;
// Set the color attribute for the box
SetConsoleTextAttribute(outHandle, getColor());
getPosition(rowPos, colPos);
pos.X = colPos; pos.Y = rowPos;
// Draw the lines that make up the box
for (int r = 0; r < height; r++)
{
SetConsoleCursorPosition(outHandle, pos);
for (int c = 0; c < width; c++)
{
cout << "*";
}
cout << endl;
pos.Y++;
}
// Restore normal text attribute
12. SetConsoleTextAttribute(outHandle, 7);
}
//***********************************************
// Constructor sets the color, position, and *
// dimensions for a box shape, and draws *
// the box at its initial position *
//***********************************************
Box::Box(int rowPos, int colPos, int width, int height)
{
setColor(4);
setPosition(rowPos, colPos);
this->width = width;
this->height = height;
draw();
}
//***********************************************
// Constructor sets the color for a Tent shape, *
// sets the position of the tent as well as the *
// length of its base and draws it at its *
// initial position *
//***********************************************
Tent::Tent(int baseRowPos, int baseColPos, int length)
{
setColor(2);
setPosition(baseRowPos, baseColPos);
this->length = length;
draw();
}
//*********************************************************************
// Constructor builds a complex shape by assembling a vector of *
// constituent shapes *
//*********************************************************************
ComplexShape::ComplexShape(Shape ** shapeCollection, int shapesCount)
{
Shape *p;
for (int k = 0; k < shapesCount; k++)