Question

Assignment 3 Translate the following MIPS code to C. Assume that the variables f, g, h, i 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 $s7, respectively. addi St0, Ss6, 4 add $t1, $s6, $0 #register $0 always holds 320s sw St1, 0(Sto) add Ss0, St1, Sto

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

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:

CAUsers Namburi Ramesh Documents reverse.exe Rounding Digits [ Reverse.cpp ] Programmer Editor(s) used: Sublime Text 3 Compil

Add a comment
Know the answer?
Add Answer to:
Assignment 3 Translate the following MIPS code to C. Assume that the variables f, g, h,...
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
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
ADVERTISEMENT