Question

A protocol is a set of rules that define some operation. For example, the Internet Protocol (IP)

specifies how messages are routed throughout the internet. In this problem, you are asked to

implement your 1st messaging protocol (MP1). MP1 is a binary protocol that is used to efficiently

encode a set of arithmetic operations using a 4-byte data type (e.g., int). The specification of the

protocol is as follow. Using a 4-byte (32-bit stream), MP1 messages encode the ‘+’, ‘-‘, ‘*’,

‘/’ arithmetic operations in the first two most significant bits of the bit stream as seen below.

Bits 31, 30 Description
0 0 Addition operation
0 1 Subtraction operation
1 0 Multiplication operation
1 1 Division operation

The arithmetic operations carried by an MP1 message (defined in Table) are executed using

two operands, a left-hand-side (lhs) operand, and a right-hand-side (rhs) operand, as seen below:

lhs <<operation from bits Table 1>> rhs

MP1 messages encode two 15-bit values used to represent both lhs and rhs operands. The lhs

numeric value is encoded in bits 29 – 15, while the rhs numeric value is encoded in bits 14 – 0.

Thus, the complete encoding of the MP1 message is as seen below.

/* Message Protocol (MP1): Val: lhs rhs Bit pos: Bit stream: Val Id: 31-30 29- 23 22 15 148 7- 0 The two most significant bits (zz) encode the arithmetic operations as seen below: 00} addition, 01 subtraction, 10 multiplication, 11 /} division The bits 29 15 (xxxxxxx xxxxxxxx) represent the lhs operand with maximum value of 2 15. The bits 14 0 (yyyyyyy yyyyyyyy) represent the rhs operand with maximum value of 2 15.

Design and implement a C++ program that uses the MP1 protocol to perform arithmetic computations on short data types. Your code shall include the following functions:
// This function receives a lhs operand, rhs operand, and operator and packs it into
// a 32-bit stream per the MP1 messaging protocol. The return value is the packed
// 32-bit (uint) stream that can be unpacked/decoded to execute arithmetic operations.
uint packMessage(ushort lhsNumber, ushort rhsNumber, char operation);
// This function receives an MP1 message (uint), unpacks/decodes it, and process it to // return the result of the operation carried within the message. For valid operation
// and data values, please refer to the MP1 protocol specification.
double processMessage(uint message);
In your implementation for both functions above, you shall use a switch statement. Every bit-mask used throughout the program shall be in the form of constant expression. Please note that each operand has a maximum value of 2^15, so your code must perform appropriate error checking before attempting to encode a number in the MP1 message. Your functions shall match exactly the declaration above.
When your program executes, it must do so infinitely, always prompting users to enter the lhs operand, followed by the operation, followed by the rhs operands, as seen below.

media%2F2e3%2F2e3647a3-bc3e-4700-b20f-5b

On each operand (lhs and rhs) your code shall implement error checks for the conditions presented as shown on figure:

media%2Fa35%2Fa35643a3-ebee-4bc1-a361-43

Similarly, your code shall check for invalid input on the operation, ensuring that only the valid values defined in the MP1 protocol are accepted, as seen in Figure:

media%2F755%2F7559e98f-dfe9-43eb-988d-83

Inside your main function, your result shall be displayed to the console using the following code.
// TODO: Add code here to request input (lhs/rhs/operation) from user and validate it.
// ...
// At this point, all inputs have been validated, so we can move forward with processing.
cout << "\nThe result of " << lhsOperand << " " << operation << " " << rhsOperand
<< " is " << processMessage( packMessage( lhsOperand, rhsOperand, operation) )
<< endl;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream.h>

#include<conio.h>

#include<math.h>

int main()

{

ushort lhs, rhs;

char op;

ushort limit;

uint message;

limit= pow(2,15);

cout<<"enter positive lhs operand";

cin>>lhs;

if(lhs > limit)

{

cout<<"please enter positive operand in valid range";

cin>>lhs;

}

cout<<"enter operator";

cin>>op;

if( op == "+" || op == "-" || op == "*" || op =="/")

continue;

else

cout<<"please enter calid operator from +,-,*,/";

cout<<"enter positive rhs operand";

cin>>rhs;

if(rhs > limit)

{

cout<<"please enter positive operand in valid range";

cin>>rhs;

message =packMeaage(lhs, rhs, op);

double r=processMessage(message);

cout<<"the result of "<<lhs <<op<<rhs<<"is:"<<r;

}

}

uint packMessage(ushort lhs, ushort rhs, char op)

{

uint result;

switch(op)

{

case '+' :

result = lhs + rhs;

break;

case '-':

result = lhs - rhs;

break;

case '*':

result = lhs * rhs;

break;

case '/':

result = lhs / rhs;

break;

default:

cout<<"wrong operator";

}

return result;

}

double processMessage(uint message)

{

double r1;

r1= double(message);

return r1;

}


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
A protocol is a set of rules that define some operation. For example, the Internet Protocol...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PROBLEM STATEMENT The mini-calculator will use a small ALU to perform arithmetic operations on two 4-bit values which are set using switches. The ALU operations described below are implemented with a...

    PROBLEM STATEMENT The mini-calculator will use a small ALU to perform arithmetic operations on two 4-bit values which are set using switches. The ALU operations described below are implemented with an Adder/Subtractor component. A pushbutton input allows the current arithmetic result to be saved. An upgraded mini-calculator allows the saved value to be used in place of B as one of the operands. The small ALU that you will design will use the 4-bit adder myadder4 to do several possible...

  • OUTCOMES After you finish this assignment, you will be able to do the following: Define an...

    OUTCOMES After you finish this assignment, you will be able to do the following: Define an abstract class Create concrete classes from an abstract class Overload an operator Split classes into .h and .cpp files Open files for reading Write to files Use output manipulators such as setw, fixed, and setprecision DESCRIPTION A binary arithmetic operation takes two double operands (left and right) to perform addition, subtraction, multiplication, or division on. For example, 10 + 11 is an addition (+)...

  • Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x....

    Write code to implement the following function: /* * Generate mask indicating leftmost 1 in x. Assume w=32. * For example 0xFF00 -> 0x8000, and 0x6600 --> 0x4000. * If x = 0,then return 0. */ int leftmost_one(unsigned x); Your function should follow the above bit-level integer coding rules, except that you may assume that data type int has w=32 bits. Your code should contain a total of at most 15 arithmetic, bit-wise, and logical operations. In C++ and has...

  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

  • Assembly language 64 bit please ! An example file for set up ==========+ ;| Data Segment...

    Assembly language 64 bit please ! An example file for set up ==========+ ;| Data Segment BEGINS Here | ;+======================================================================+ segment .data ;Code this expression: sum = num1+num2 num1 dq 0 ;left operand of the addition operation num2 dq 0 ;right operand of the addition operation sum dq 0 ;will hold the computed Sum value RetVal dq 0 ;Integer value RETURNED by function calls ;can be ignored or used as determined by the programmer ;Message string prompting for the keyboard...

  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

  • In Problem Set 7 you designed and implemented a Message class. This time, let's design and...

    In Problem Set 7 you designed and implemented a Message class. This time, let's design and implement a Mailbox class in a file named Mailbox java. Do the following with this class • You may use the Message class from PS 7. You will have to add new features to the Message class from PS 7 as you work through this problem. You are welcome to start with my sample solution if you wish • Suppose there are multiple mail...

  • 1.) a.) Using the simplified instruction set shown for part b, write code for the following....

    1.) a.) Using the simplified instruction set shown for part b, write code for the following. Suppose memory locations 1400 to 1449 contain 16-bit words. Each word represents 2 ASCII characters. Write code to read in and write out these 100 characters. Left-side character from location 1400 should be first, right-side character from location 1400 should be second, and remaining characters follow in numeric order. Assume you have access to 4 registers: R1, R2, R3, R4. Each register holds one...

  • Here is the code I have so far. I'm trying to figure out how to implement...

    Here is the code I have so far. I'm trying to figure out how to implement a boolean and use precedence. The 2nd expression should be 14 but it comes out as 28 so I'm definitely not understanding. #include <stack> #include <iostream> #include <string> using namespace std; // Function to find precedence of // operators. int precedence(char op) {    if (op == '+' || op == '-')        return 1;    if (op == '*' || op ==...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT