I need help...This is what I have so far. And isnt giving me the right result. .
ID: 3648782 • Letter: I
Question
I need help...This is what I have so far. And isnt giving me the right result..data
Prompt:
.asciiz "Enter an integer: "
Result1:
.asciiz "The decimal number "
Result2:
.asciiz " is 0x"
Result3:
.asciiz " in hexadecimal."
.text
# Get input from user. First, issue a prompt using the
# system call that will print out a string of characters.
la $a0, Prompt # Put the address of the string in $a0
li $v0, 4
syscall
# Next, make the system call that will wait for the user to input
# an integer.
li $v0, 5 # Code for input integer
syscall
# Integer input comes back in $v0
# Save the inputted integer in a saved register - important!
# It cannot stay in $v0 as we need to reuse $v0.
move $s1, $v0
# Next, begin to output the result message. This is done in several
# steps, including outputting strings and the original integer.
# Output first string.
li $v0, 4
la $a0, Result1
syscall
# Output original integer
move $a0, $s1 # Remember, $s1 contains the input number.
li $v0, 1
syscall
# Output second string.
la $a0, Result2
li $v0, 4
syscall
# Output the hexadecimal number. (This is done by isolating four bits
# at a time, adding them to the appropriate ASCII codes, and outputting
# the character. It is important that the digits are output in
# most-to-least significant bit order.
li $s2, 8 # Counter
la $t3, Result2 #Where the result is stored
# Copy the input into a different register for manipulation
# Set up a loop counter
Loop:
# Roll the bits left by four bits - wraps highest bits to lowest bits (where we need them!)
# Mask off low bits (logical AND with 000...01111)
# Determine if the bits represent a number less than 10 (slti)
# If not (if greater than 9), go make a high digit
rol $s1, $s1, 4
and $t0, $s1, 0xf
slti $t1, $t0, 10
beq $t1, $zero, MakeHighDigit
MakeLowDigit:
# Combine it with ASCII code for '0', becomes 0..9
# Go output the digit
add $t0, $t0, 48
j DigitOut
MakeHighDigit:
# Subtract 10 from low bits
# Add them to the code for 'A' (65), becomes A..F
sub $t2, $t1, 10
add $t2, $t2, 65
DigitOut:
move $a0, $t0 # Output the ASCII character that you placed in $t0
li $v0, 11
syscall
sb $t2, 0($t3)
addi $s2, $s2, -1 # Decrement loop counter
bne $s2, $zero, Loop # Keep looping if loop counter is not zero
# Output third string.
Exit:
la $a0, Result3
li $v0, 4
syscall
# Done, exit the program
li $v0, 10 # This system call will terminate the program gracefully.
syscall
Explanation / Answer
please rate - thanks
sorry I started from scratch, it was easier
3 separate runs
.text
.globl main
main:
la $a0,endl
li $v0,4
syscall
la $a0, input1 #Asks for the number
li $v0, 4
syscall
li $v0, 5 #Inputs number
syscall
move $t0,$v0 #send number in a0
li $t4,16 #convert to base 16
la $a3,newnum #send address of new number
li $a1,32 #send max digits
jal convert
la $a0,answer
li $v0,4
syscall
li $t2,128 #how far to go in buffer 32*4
li $v0,4 #going to print
li $t0,-4 #start before beginning of buffer
check:
addi $t0,4 #go to next location
la $a0,newnum
#keep going till get a non zero
printloop:
syscall
j end;
convert: li $t5,9
move $t1,$a1 #get number of digits
# sll $t1,$t1,2 #multiply by 4 to change to bytes
add $t3,$a3,$t1 #get last spot in input buffer
keepgoing:
addi $t3,-1 #start in output buffer
blt $t3,$a3,done #done?
div $t0,$t4 #divide
mfhi $t2 #get remainder
ble $t2,$t5, skip
addi $t2,7
skip: addi $t2,48
sb $t2,($t3) #move into answer
mflo $t0 #get answer
beqz $t0,done #answer=0-done?
j keepgoing
done:
addi $t3,-1 #start in output buffer
blt $t3,$a3,return #done?
# sw $t0,($t3) #fill buffer with 0's
j done
return: jr $ra
end:
la $a0,endl
li $v0,4
syscall