Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS prog
ID: 3803856 • Letter: P
Question
Please write MIPS program that runs in QtSpim (ex: MipsFile.s)
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
The program needs to be able to test 3 different cases and run in QtSpim like a .s file
here is a c program that shows the functionality
This must be MIPS program
Explanation / Answer
.data
msg1:
.asciiz "Please insert value (A > 0) : "
msg2:
.asciiz "Please insert the number system B you want to convert to (2<=B<=36): "
msg3:
.asciiz " Result: "
digits:
.asciiz "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
conversion_buffer:
.space 2
.text
.globl main
#
# Program entry point
#
main:
addi $s0,$zero,2 # lower boundary condition
addi $s1,$zero,36 # upper boundary condition
# output message to console
getA:
li $v0,4 #print_string
la $a0,msg1 #"Please insert value (A > 0) : "
syscall
# input user response
li $v0,5 #read_int
syscall
blt $v0,$zero,getA
move $t0,$v0
# output message to console
getB:
li $v0,4 #print_string
la $a0,msg2 #"Please insert the number system B you want to convert to (2<=B<=10): "
syscall
# input user response
li $v0,5 #read_int
syscall
blt $v0,$s0,getB
bgt $v0,$s1,getB
add $t1,$zero,$v0
# output message to console
li $v0,4 #print_string
la $a0,msg3 #" Result: "
syscall
add $a0,$zero,$t0
add $a1,$zero,$t1
jal convert
li $v0,10 #exit
syscall
#
# Subroutine - convert, and output, value A to number base B
#
#a0=A
#a1=B
convert:
addi $sp,$sp,-16 #reserve space on stack for 'locals'
sw $s3,12($sp) #counter,used to know how many times we will pop from stack
sw $s0,8($sp) #A
sw $s1,4($sp) #B
sw $ra,0($sp) #???return address???
add $s0,$zero,$a0
add $s1,$zero,$a1
beqz $s0,end
div $t4,$s0,$s1 #t4=A/B
rem $t3,$s0,$s1 #t3=A%B
add $sp,$sp,-4
sw $t3,0($sp) #save t3
add $a0,$zero,$t4 #pass A/B
add $a1,$zero,$s1 #pass B
addi $s3,$s3,1
jal convert #call (self) convert
end:
lw $ra,0($sp)
lw $s1,4($sp)
lw $s0,8($sp)
lw $s3,12($sp)
beqz $s3,done #check if anymore 'numbers' to print, if not skip don't print
# output integer to console
la $a0,digit
lw $t4,16($sp)
add $a0,$a0,$t4
lb $t4,($a0)
la $a0,conversion_buffer
sb $t4,($a0)
li $v0,4 #print_string
syscall
done:
addi $sp,$sp,20
jr $ra #returnz