Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1 (40 points) Write a MIPS program (which must be named as “ hw4_prob1.s

ID: 3805514 • Letter: P

Question

Problem 1 (40 points) Write a MIPS program (which must be named as “hw4_prob1.s) 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.

I would appreciate some comments for me to understand the code.

Also I have done the following code which takes the base and the number, but I still need to do the function for the convertion.

.data
str: .asciiz "Enter a base (between 2 and 36
in decimal): "
str1: .asciiz "Enter a number in base "
str2: .asciiz "The value in decimal is:"
str3: .asciiz ":"
str4: .asciiz "Error, wrong value"
.text
Main:
    li $v0, 4       # $system call code for print_str
    la $a0, str     # $address of string to print
    syscall         # print the string

    li $v0, 5       # $system call code for read_int
    syscall         # read the int
    move $s0, $v0   #store value
   li $t1, 37
   slt $t0, $s0, $t1
   beq $t0,1, IF
   lit $v0, 4
   la $a0, str4
   syscall
   j Main

IF:
        li $t2, 1
   slt $t3, $t2, $s0
   beq $t0,1, STR
   lit $v0, 4
   la $a0, str4
   syscall
   j Main

STR:  
    li $v0,4
   la $a0,str1
   syscall
   move $a0,$s0
   li $v0, 1
   syscall
   li $v0, 4
   la $a0, str3
   syscall
   li $v0, 5
   syscall
   move $s1, $a0
   j CONV
CONV:

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 = &num;
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 = &num;
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
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote