1. Engineering
  2. Computer Science
  3. i was wondering if you could help me with a...

Question: i was wondering if you could help me with a...

Question details

I was wondering if you could help me with a MIPS program, i Need to write a program that will convert from any base from 2-10 to decimal. I have a skeleton of the code i need to write which is :

###############################################################################
## Author: __________________
## Date: __________________
## Purpose: Convert a number in some base to its decimal equivalent
##
###############################################################################
   .data
imsg1:   .asciiz   "Enter a number in another base using at most 10 digits: "
imsg2:   .asciiz   "Enter the base number: "
omsg1:   .asciiz   "Decimal equivalent: "
newln:   .asciiz "\n"
num:   .space   12   
base:   .word   0
ans:   .word   0
len:   .word    0

   .text
START:  
              
   li   $v0, 4       # Print the 1st prompt
   la   $a0, imsg1
   syscall

   li   $v0, 8       # Get the digits from the user
   la   $a0, num   # Setup a pointer to the destination array
   li   $a1, 12       # there will be a newline
   syscall           # and a null char at the end

   li   $v0, 4       # Print the 2nd prompt
   la   $a0, imsg2
   syscall
  
   li   $v0, 5       # read integer
   syscall  
   la   $a1, base
   sw   $v0, 0($a1)   # store base to memory variable
  

   la    $a0, num     # Load the address of num
   la   $a1, base   # Load the address of the base
   lw   $t0, 0($a1)   # load the base number

## Add your code here  
## To convert to decimal we start a sum with the most-significant digit.
## Then for each subsequent digit we multiplying the sum by the base
## and add the next digit

# When done, print the converted number using syscall service 1

# Do not alter code below this point
   li   $v0, 4       # print newline
   la   $a0, newln
   syscall
# Quit
   li   $v0, 10
   syscall

a. Your program shall prompt a user to enter an ASCII string of digits, maximum 10 digits without spaces (the Enter key LF character followed by the null character 10 will terminate the string) b. Receive the input. You can assume the user enters a string composed of 10 or less digits. You need not check for non-digit characters or worry about strings that are too long (though they may be shorter than 10 digits). Note: We will not enter a number that results in 32-bit overflow e. is too big for its decimal representation to fit in 32-bits) c. Convert the number to decimal. Recall a number: a 1an-2...a1ao in base r can be converted to decimal through the formula m-1 n-2 m-1 a apr air i-0 However, this would require us to compute powers of r Instead we can use an iterative process to sum up the number multiplying by r each iteration. The above summation can equivalently be shown as: X10 n-1)+ a a a n-2 n-4)...) This can be summarized by the recursive formulation Let 10 (0) 0 X10 i 1) rX10(i) an-i-1 Essentially, if we have an n-digit number we can start a sum at 0 (i.e. X(0) 0) and then repeatedly multiply that value by r and add in the next digit (i.e. an-i 1) a total of n times. A C-code version of the program is attached at the end of this document. Note that we have received ASCII digits (i.e. 0 0x30, 1 0x31, etc.) and need to convert each digit to a number. d. You must also add a bit of error checking. Recall that valid digits in base r range from 0 to (r-1). You should check if the current digit being processed is out of range, and if so, start the sum back at 0 (essentially restart your conversion process with the remaining part of the string and discarding the effect of any prior digits) e. At the end of your computation output the message pointed to by label omsg1 followed by outputting the integer representing the decimal equivalent. You should do this with two separate syscall sequences loading the appropriate $v0, $a0 values prior to your syscall nstruction

Here are the verbatim procedures^

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution