I was wondering if you could help me with a MIPS program, i Need to write a prog
ID: 3807586 • Letter: I
Question
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 " "
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
Here are the verbatim procedures^
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 nstructionExplanation / Answer
Ans: MIPS program
.data
msg1:
.asciiz "Enter no. in base_2 (-2 to quit): "
msg2:
.asciiz " Result: "
allOnes:
.asciiz "1111111111111111"
empty:
.space 16
newLine:
.asciiz " "
sum:
.space 16
sumMsg:
.asciiz " SUM: "
oneFound:
.asciiz " One found "
zeroFound:
.asciiz " Zero found "
.text
.globl main
main:
getNum:
li $v0,4 # Print string system_call
la $a0,msg1 #"Please insert value (A > 0) : "
syscall
la $a0, empty
li $a1, 16 # load 16 as max_length to read into $a1
li $v0,8 # 8 is string system_cal
syscall
la $a0, empty
li $v0, 4 # prints string
syscall
li $t4, 0 # initialize tye sum to 0
startConvert:
la $t1, empty
li $t9, 16 # initialize the counter to 16
firstByte:
lb $a0, ($t1) # load the first-byte
blt $a0, 48, printSum
addi $t1, $t1, 1 # increment offset
subi $a0, $a0, 48 # subtract 48 to convert to int value
subi $t9, $t9, 1 # decrement counter
beq $a0, 0, isZero
beq $a0, 1, isOne
j convert #
isZero:
j firstByte
isOne: # do 2^counter
li $t8, 1
sllv $t5, $t8, $t9 # shift left by counter = 1 * 2^counter, store in $t5
add $t4, $t4, $t5 # add sum to previous sum
move $a0, $t4 # load sum
li $v0, 1 # print int
syscall
j firstByte
convert:
printSum:
srlv $t4, $t4, $t9
la $a0, sumMsg
li $v0, 4
syscall
move $a0, $t4 # load sum
li $v0, 1 # print int
syscall
exit:
li $v0, 10 # exit system call
syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.