Note:- Modified the 'reverseNum' function.
#include <iostream>
using std::cout;
using std::cin; //Allows the program to perform input and output
using std::endl;
//necessary to convert character 'buffer' to integer 'num' with 'atoi'
#include <cstdlib>
//function templates
int reverseNum(int number, int len);
int main()
{
//print my name and this assignment's title
cout << "Rounding Digits [ Reverse.cpp ]\n";
cout << "Programmer:\n";
cout << "Editor(s) used: Sublime Text 3\n";
cout << "Compiler(s) used: Visual Studio 2015\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
//Program starts here:
//variables to store the input in the buffer and integers necessary to store the number from the user
//and display leading zeros if the user puts a digit with zeros
char buf[100];
int num, num1, len, temp, len1, ret;
while (true)
{
//prompt user for input
//the user puts a series of numbers less than 6 digits to do the reverse
//and they are stored in the buffer, to later convert it to integer and sotre the numbers
//in the variable called 'num'
cout << "Enter the number to perform reverse (q or Q to quit): ";
cin >> buf; num = atoi(buf);
//calculate length
len = 0;
while (buf[len] != '\0') //determines where in the 'buffer' variable the null character determinate
len++; //the end of C-style strings and calculate the length of the digits gathered from the user
if (len < 1 || len > 6) //if length is < 1 or more than 6, ask input again
{
cout << "Length of input should be 1 to 6 digits" << endl;
continue; //if the length of input is more than 6 digits, the program displays
} //"Length of input should be 1 to 6 digits" and the program continues
//quit program if user entered a q
if (buf[0] == 'q' || buf[0] == 'Q')
break;
cout << "Entered number: " << num << endl;
ret = reverseNum(num,len); //ret gets the return type from 'reverseNum' function
temp = ret; //the temp variable gets the information stored in the 'ret variable'
len1 = 0; //'len1' variable stores 0 to display correctly the leading zeros
//with the loop from below
while (temp > 0) //find the length of the reversed number
{
temp = temp / 10; //formula necessary to display the leading zeros if the user puts
len1++; //a series of digits with leading zeros or has zero on the last character
}
//the program display the reversed number with leading zeros
cout << "Reversed number With leading zeros : ";
for (int i = 0; i < (len - len1); i++) //loop necessary to display the leading zeros if the user
cout << 0; //puts 6 digits with leading zeros or has zero on the last
//the last character
//display the reverse number after the leading zeros are displayed in the same line
//to show the leading zeros, otherwise it will display only the leading zeros
cout << ret << endl;
}
}
//function integer 'reverseNum' necessary to do the reverse of the function
//and necessary to display the zeros of the user entered numbers that are
//leading with zeros
//modified the function to accept the length of the number so that to include the zeros
int reverseNum(int number,int len)
{
int reversedNum = 0;
int reminder;
int zeros = 0;
bool firsttime = true;
int len1=0;
while (number > 0)
{
reminder = number % 10;
//If reminder is 0 and the first non zero number is not yet encountered
if (reminder == 0 && firsttime)
{
zeros++;
}
//If first non zero digit is encounreted, zeros encountered later shouldn't be counted
else
{
firsttime = false;
}
reversedNum = reversedNum * 10 + reminder; //formula to display the reverse number
number /= 10; //the number gathered from the user is
//mutiplied by 10 and added with the reminder.
len1++;
} //later the 'number' is divided by ten and added
//everything to 'reversedNum'
//Added code here
if(len-len1 != 0){
for(int i=0;i<len-len1;i++){
reversedNum= reversedNum*10;
}
}
return reversedNum; //the 'reversedNum' is returned to returned to the
//main function with the calculations properly calculated
//and stored
}
OUTPUT:
Assignment 3 Translate the following MIPS code to C. Assume that the variables f, g, h,...
1. Assume that variables f,g,h,ij are assigned to registers Ss0 to Ss4 (in the given order). Assume that the base addresses of arrays A and B are in $s6 and Ss7. Registers St0 to St2 are used to hold temporary values. What does this code do? (A is a label, assembler will replace this label with inbstruction addresses as needed in generating machine code) add St0,Szero,Ss0 addi St0,St0,400 A: lw $s7, O(St0) addi $s7, Ss7,1 sw $s7,0 (St1) subi Sto,...
7. Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a,b, i and j are in registers Ss0, Ss1, St0, and St1, respectively. Also, assume that register SS2 holds the base address of the array D. for(i-0; i<a; i++) for(j=0 ; j<b; j++)
IN MIPS AND MUST RUN IN QTSPIM Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i and j are stored in registers Ss0, Ss1, St0 and Stl, respectively. Also assume that register Ss2 holds the base address of the array D. for (i=0; i<a; itt) for (i-0j<b:jt+)
Translate the following C code to MIPS assembly. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also assume that $s2 holds the base address of the array D. for (i = 0: i < a: i++) for (j = 0: j < b: j++) D[2 * j] = i + j;
Problem 2 Points (40) The following problems deal with translating from C to MIPS. Assume that the variables g, h, j, and j are assigned to registers Ss0, Ss1, Ss2, Ss3, and Ss4, respectively. Assume that the base address of the arrays A and B are in registers Ss6 and Ss7 respectively 1. Provide the MIPS assembly code for the above C statements. Use a minimal number of MIPS assembly instructions 2. For the C statements above, how many MIPS...
2.9 5 $2.2, 2.3> Translate the following C code to MIPS. Assume that the variables f, g, h, i, and j are assigned to registers $s0, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively. Assume that the elements of the arrays A and B are 4-byte words: fAlBg
Question 3 (10 points) Convert the following MIPS assembly code into machine language. Write the instruction in hexadecimal. The opcode for sw 43 (101011). sw St1, -4(St3) Question 4 (10 points) Consider the following MIPS assembly code: addi $s3, $0, 5 addi $s1, S0, 3 addi Ss1, $s1, 2 beq Ss3, Ssl, target addi Ss1, Ss1, 1 target: add Ss3, Ss1, Ssl a. After running the code, what is the value of Ss3? b. If the memory address of the...
(10pts) (A) Identify hazards (including type of the hazard) in the following code. Write hazards next to each instruction. Write none if there is no hazard. Assume that each instruction could have more than one hazard and I5 does not create a control hazard, Type of Hazards Instructions I: LABEL:lw Ss2, 0(Ss0) none 12: 13: 14 15: 16: add Ss1, Ss6, $sl add Stl, Ss0, $s2 and St1, St, $s3 sw St1 0(Ss0) beq St1, St7, LABEL (B) How many...
2 performance (30pt) You are the project lead at Micro Performance. The company wants you to select which option would be best for their new Micro Speed processor: Type Arithmetic 1 Branch Memory Clock Original Fast Clock Fast Mem Percent by inst. type 40% 20% 40% 1 2 4 1 1.8GHZ 3.6GH2 1.8GHz 1. Double clock speed (Arithmet io ond branch are twice as fast, mem is the same) 2. Clock the same, add a high nerformance bus and larger...
2. The table below holds MIPS assembly code fragments with different branch instructions LOOP addi $s2. $s2. 2 subi $t1. st1. 1 bne t1. 0. LOOP DONE: LOOP: it st2. $0. stl beq t2. 0. DONE addi $s2. Ss2. 2 LOOP DONE: For the loops written in MIPS assembly in the above table, assume that the register Şt1 is initialized to the value of 10. What is the value in register $s2 assuming that $s2 initially has a value of...