Write a MIPS assembly program using MARS which allows the user to convert number
ID: 3547484 • Letter: W
Question
Write a MIPS assembly program using MARS which allows the user to convert numbers between bases
Inputs from the user
1 - The old base (entered in base 10)
2 - The number to be converted.?
3 - The new base (entered in base 10)
Output
The number in the new base
Specification
Use uppercase letters to represent digits above 9.
A = 10
B = 11
....?
Z = 35
You may assume that bases will be between 2 and 36.?
Your program should handle any value that will fit into a 32 bit integer.?
You may assume that only positive numbers will be entered.?
If an illegal digit is entered, print an error message and allow the user to begin another conversion.
(for example, a base 8 number cannot include the digits 8 or 9)?
Design the program so that multiple conversions can be done with a single run.
Use at least three functions for reasonable tasks.?
Pass at least one value parameter and one reference parameter in your program.
(NOTE: value is by copy and reference is by address.
Sample Run
Enter the old base (2 - 36) - 36
Enter the number to be converted - 1Z
Enter the new base - 5
241?
Again (y or n) ? n
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.