Write a MIPS program that will read in a base (as an integer) and a value (nonne
ID: 3678236 • Letter: W
Question
Write a MIPS program that will read in a base (as an integer) and a value (nonnegative integer but as an ASCII string) in that base and print out the decimal value; you must implement a function (which accepts a base and an address for a string as parameters, and returns the value) and call the function from the main program. The base will be given in decimal and will be between 2 and 36, and the digits for the values will be in set [0…9, a…z]. You can assume that no overflow will occur and the value is valid in the given base.
An example run will be: Enter a base (between 2 and 36 in decimal): 2
Enter a number in base 2: 101
The value in decimal is: 5
Explanation / Answer
the c equivalent of the above MIPS code:
//#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
// #include <conio.h>
#include <math.h>
void convert(long base, long num) {
long indexVar = 0;
long valueDeci = 0;
long *pointer, nonPointer, yes;
long divisor = 10;
long peak = 1;
pointer = #
while(nonPointer != 0 ) {
yes = nonPointer % divisor;
nonPointer = nonPointer / divisor;
valueDeci = valueDeci + yes * peak; //*pointer%10)*pow(base, indexVar);
//*pointer = *pointer / 10;
//indexVar++;
peak = peak * base;
} // end while
printf(" The value in decimal is: %ld", valueDeci);
} // end function convert
int main()
{
long base;
long num;
printf(" Enter a base (between 2 and 36 in decimal): ");
scanf("%ld", &base);
printf(" Enter a number in base %d :", base);
scanf("%ld", &num);
convert(base, num);
return 0; // exit
} // end of main()
/*
void oldConvert(long base, long num) {
long indexVar = 0;
long valueDeci;
long *pointer;
long divisor = 10;
pointer = #
while(*pointer > 0 ) {
valueDeci = valueDeci + (*pointer%10)*pow(base, indexVar);
*pointer = *pointer / 10;
indexVar++;
} // end while
printf(" The value in decimal is: %lld", valueDeci);
} // end function oldConvert
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.