This homework tests your ability to evaluate potential pipeline hazards. The fol
ID: 3843035 • Letter: T
Question
This homework tests your ability to evaluate potential pipeline hazards. The following program is used as a model. It computes vehicle sales by large (>50000) and small makes.
// WarRawWawRarSample.cpp : Sample program for Homework 5
#include <iostream>
#include <string>
#define makes 29
int main(int argc, char* argv[])
{
// Vehicle sales from
// http://online.wsj.com/mdc/public/page/2_3022-autosales.html#autosalesD
char makers[makes][12] = {
"GM", "Ford", "Chrysler", "Toyota",
"Honda", "Nissan", "Hyundai", "Mazda",
"Mitsubishi", "Kia", "Subaru", "Mercedes",
"Volvo", "Volkswagen", "Audi", "BMW",
"Porsche", "Fiat", "Tesla", "Jaguar",
"Land Rover", "Alfa Romeo", "Mini", "Smart",
"Ferrari", "Maserati", "Bentley", "Lamborghini",
"Rolls Royce"};
int sales[makes] = {
259557, 229739, 196526, 211125,
148829, 123861, 62213, 26195,
9674, 56508, 50380, 31825,
6168, 27112, 17801, 24951,
5410, 3045, 2900, 1087,
5188, 60, 4796, 466,
217, 1066, 110, 89,
79};
int i, j, large = 0, small = 0, largetot = 0, smalltot = 0;
const int lim = 50000;
for (i = 0; i < makes; i++) {
if (sales[i] > lim) {
large++;
largetot += sales[i];
}
else {
small++;
smalltot += sales[i];
}
}
std::cout << "Large:" << large << " vehicles:" << largetot << " Small:" << small << " vehicles:" << smalltot << ' ';
return 0;
}
On the following page, you will find the assembly code generated by the for loop above. List the type of hazard (WAR, WAW, RAW, RAR, Control, or none) and the data item causing the hazard in the spaces provided. These lines will be identified by an arrow ( ). Ignore lines marked N/A. The associated C++ code is interspersed. Note that the dword ptr [i] syntax refers to i as a variable, not as a subscript.
Address
Opcode
Operands
Hazard Type
Caused By
for (i = 0; i < makes; i++) {
010369EF
mov
dword ptr [i],0
N/A
010369F9
jmp
main+5EAh (01036A0Ah)
N/A
010369FB
mov
eax,dword ptr [i]
01036A01
add
eax,1
01036A04
mov
dword ptr [i],eax
01036A0A
cmp
dword ptr [i],1Dh
N/A
01036A11
jge
main+65Ah (01036A7Ah)
N/A
if (sales[i] > lim) {
01036A13
mov
eax,dword ptr [i]
01036A19
cmp
dword ptr sales[eax*4],0C350h
N/A
01036A24
jle
main+630h (01036A50h)
N/A
large++;
01036A26
mov
eax,dword ptr [large]
N/A
01036A2C
add
eax,1
N/A
01036A2F
mov
dword ptr [large],eax
N/A
largetot += sales[i];
01036A35
mov
eax,dword ptr [i]
01036A3B
mov
ecx,dword ptr [largetot]
01036A41
add
ecx,dword ptr sales[eax*4]
01036A48
mov
dword ptr [largetot],ecx
}
else {
01036A4E
jmp
main+658h (01036A78h)
N/A
small++;
01036A50
mov
eax,dword ptr [small]
01036A56
add
eax,1
N/A
01036A59
mov
dword ptr [small],eax
N/A
smalltot += sales[i];
01036A5F
mov
eax,dword ptr [i]
N/A
01036A65
mov
ecx,dword ptr [smalltot]
N/A
01036A6B
add
ecx,dword ptr sales[eax*4]
N/A
01036A72
mov
dword ptr [smalltot],ecx
}
}
01036A78
jmp
main+5DBh (010369FBh)
N/A
Which hazard type listed on the previous page (not none) is not really a hazard? ___________________
Address
Opcode
Operands
Hazard Type
Caused By
for (i = 0; i < makes; i++) {
010369EF
mov
dword ptr [i],0
N/A
010369F9
jmp
main+5EAh (01036A0Ah)
N/A
010369FB
mov
eax,dword ptr [i]
01036A01
add
eax,1
01036A04
mov
dword ptr [i],eax
01036A0A
cmp
dword ptr [i],1Dh
N/A
01036A11
jge
main+65Ah (01036A7Ah)
N/A
if (sales[i] > lim) {
01036A13
mov
eax,dword ptr [i]
01036A19
cmp
dword ptr sales[eax*4],0C350h
N/A
01036A24
jle
main+630h (01036A50h)
N/A
large++;
01036A26
mov
eax,dword ptr [large]
N/A
01036A2C
add
eax,1
N/A
01036A2F
mov
dword ptr [large],eax
N/A
largetot += sales[i];
01036A35
mov
eax,dword ptr [i]
01036A3B
mov
ecx,dword ptr [largetot]
01036A41
add
ecx,dword ptr sales[eax*4]
01036A48
mov
dword ptr [largetot],ecx
}
else {
01036A4E
jmp
main+658h (01036A78h)
N/A
small++;
01036A50
mov
eax,dword ptr [small]
01036A56
add
eax,1
N/A
01036A59
mov
dword ptr [small],eax
N/A
smalltot += sales[i];
01036A5F
mov
eax,dword ptr [i]
N/A
01036A65
mov
ecx,dword ptr [smalltot]
N/A
01036A6B
add
ecx,dword ptr sales[eax*4]
N/A
01036A72
mov
dword ptr [smalltot],ecx
}
}
01036A78
jmp
main+5DBh (010369FBh)
N/A
Explanation / Answer
#include <iostream>
#include "car.h"
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
using namespace std;
class Dealer
{ vector<Car> cars;
double balance;
public:
Dealer ();
void show_inventory ();
void show_balance ();
void buy_car ();
vector<Car>::iterator FindCarByName (const string & car_name);
void sell_car ();
void paint_car ();
void load_file ();
void save_file ();
};
Dealer::Dealer ()
{ balance = 10000;
}
void Dealer::show_inventory ()
{ for (unsigned i=0; i<cars.size(); i++)
cout << cars[i].toString () << endl;
}
void Dealer::show_balance ()
{ cout << "$" << balance << endl;
}
void Dealer::buy_car ()
{ string name;
string color;
double price;
cout << "Please enter the name, color, and price of the car you would like to buy." << endl;
cin >> name;
cin >> color;
cin >> price;
if (price > balance)
{ cout << "You cannot afford that car." << endl;
return;
}
cout << "Car has been purchased." << endl;
balance = balance - price;
cars.push_back (Car(name,color,price));
}
vector<Car>::iterator Dealer::FindCarByName (const string & car_name)
{ vector<Car>::iterator car_iter;
// Find car in factor, return iterator
return car_iter;
void Dealer::sell_car ()
{ string name;
vector<Car>::iterator car_iter;
cout << "Please enter the name of the car you want to sell." << endl;
cin >> name;
car_iter = FindCarByName (name);
balance = balance + car_iter->getPrice();
cars.erase (car_iter);
}
void Dealer::paint_car ()
{ string name;
string new_color;
vector<Car>::iterator car_iter;
cout << "Please enter the name of the car you want to paint." << endl;
cin >> name;
cout << "Please enter the new color: ";
cin >> new_color;
car_iter = FindCarByName (name);
car_iter->paint (new_color);
balance += 1000;
}
void Dealer::load_file ()
{ cars.clear();
}
void Dealer::save_file ()
{
}
int main()
{ int option;
Dealer dealer;
while (true)
{ cout << "Please select an option:" << endl;
cout << "1 - Show current inventory." << endl;
cout << "2 - Show current balance." << endl;
cout << "3 - Buy a car." << endl;
cout << "4 - Sell a car." << endl;
cout << "5 - Paint a car." << endl;
cout << "6 - Load file." << endl;
cout << "7 - Save file." << endl;
cout << "8 - Quit program." << endl;
cout << " ";
cin >> option;
switch (option)
{
case 1: dealer.show_inventory ();
break;
case 2: dealer.show_balance ();
break;
case 3: dealer.buy_car ();
break;
case 4: dealer.sell_car ();
break;
case 5: dealer.paint_car ();
break;
case 6: dealer.load_file ();
break;
case 7: dealer.save_file ();
break;
case 8: return 0;
default: cout << "Please enter a number from 1-8." << endl;
}
cout << " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.