So for my assembly code (MIPS using MARS), I have to prompt the user to enter a
ID: 654693 • Letter: S
Question
So for my assembly code (MIPS using MARS), I have to prompt the user to enter a number between 2 and 36 to signify which base the number they want to convert is in. Then I prompt the user to enter the number they want to convert. Then I prompt the user to enter a number between 2 and 36 to signify which base they want to convert a number to. Then I'm supposed to take the number and convert it to the new base they specified and print that number.
[For clarification, I am using a code from my previous project that was similar, but the user would imput their number only in decimal (base 10) rather than any number from any base.]
My code keeps breaking and I have no idea what's wrong. The code is due at 5 PM CST.
.text
.globl main
main:
move $t0, $zero
move $t1, $zero
redoLoop:
li $v0, 4
la $a0, prompt # print "enter old base"
syscall
li $v0, 5
syscall
move $t0, $v0 #store in $t0
li $v0, 4
la $a0, convert # print "enter number to convert"
syscall
li $v0, 5
syscall # take number entered and store in inbuf
la $v0, inbuf
li $v0, 4
la $a0, base # print "enter new base"
syscall
li $v0, 5
syscall
move $t1, $v0 # store new base in $t1
#------ take number from buffer one byte at time
move $t2, $zero # set number storage = to 0
move $t3, $zero # offset into the buffer array
top:
lb $a0, inbuf($t3) # loads index of inbuf to $a0
beq $a0, $zero, out # exit loop when null char found
beq $a0, 10, out # exit loop if new line found
#------ subtract 48 or 55
blt $a0, 58, nineorless
blt $a0, 91, alphabet
nineorless:
subi $a0, $a0, 48
add $t2, $t2, $a0
j increment
alphabet:
subi $a0, $a0, 55
add $t2, $t2, $a0
j increment
increment:
addi $t3, $t3, 1
beq $a0, $zero, out # exit loop when null char found
beq $a0, 10, out # exit loop if new line found
mul $t2, $t2, $t0 # if another character found multiply number by old base
j top
out:
li $v0, 4 # print_string
la $a0, enter
syscall
jal findBase
############################################
findBase:
# la $a0, ($t2)
# move $t2, $a0
move $t4, $zero
rem $t4, $t2, $t1
div $t2, $t2, $t1
#addi $s6, $s6, 1
#addi $a2, $a2, -1
bgt $t4, 9, great
ble $t4, 9, less
return:
bge $t2, 1, findBase
li $v0, 4 # print_string
la $a0, outbuf
syscall
addi $sp, $sp, -4
sw $ra, 0($sp)
jal spacePrint
lw $ra, 0($sp)
addi $sp, $sp, 4
addi $sp, $sp, -4
sw $ra, 0($sp)
jal resetBuf
lw $ra, 0($sp)
addi $sp, $sp, 4
addi $a1, $a1, 4
addi $t0, $t0, 1
move $a2, $s1
blt $t0, 10, findBase
move $a0, $zero
move $t4, $zero
move $t5, $zero
move $t7, $zero
move $t6, $zero
la $a1, inbuf
la $a2, ($s1)
jr $ra
great:
addi $t3, $t3, 55
sb $t3, outbuf($a1) #stores remainer in outbuf ## not sure about $a2
addi $a1, $a1, 1
j return
less:
addi $t4, $t4, 48
sb $t4, outbuf($a1) #stores remainder in outbuf ## not sure about $a2
addi $a1, $a1, 1
j return
spacePrint:
lb $a0, outbuf($a3)
beq $a0, '', lap
li $v0, 11
syscall
lap:
addi $a3, $a3, 1
ble $a3, 10, spacePrint
end:
move $a3, $zero
move $s6, $zero
jr $ra
resetBuf:
sb $zero, outbuf($t1)
addi $t1, $t1, 1
blt $t1, 10, resetBuf
move $t1, $zero
jr $ra
li $v0, 4
la $a0, again
syscall
li $v0, 8 # read string
syscall
la $a0, redoEntry # original input is in userEntry
move $t9, $zero
lb $a0, redoEntry($t9) # char to print is in $a0
beq $a0, 121, redoLoop
li $v0, 10 # using service 10, terminate
syscall
# ------------------------------------------------------
.data # beginning of data area
prompt:
.asciiz "Enter the old base (2 - 36) - "
convert:
.asciiz " Enter the number to be converted - "
base:
.asciiz " Enter the new base - "
again:
.asciiz " Again (y or n) ? "
redoEntry:
.word 0
enter:
.asciiz " "
outbuf:
.space 36
inbuf:
.space 36
# ------------------------------------------------------
Explanation / Answer
You're missing a .text directive. That should probably go after your newline line and before main:.
You have a $ao where you probably want a $a0 near the end of the program.
You didn't ever print str3 - you need to add:
la $a0,str3 #address of str3 syscall
after your li $v0,4 #system call code for print string line.
You should make sure to add an exit syscall at the end of your program:
li $v0,10 syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.