Help With MIPS Dec to Hex I found this code online but couldnt get it to work. A
ID: 3668782 • Letter: H
Question
Help With MIPS Dec to Hex
I found this code online but couldnt get it to work. Any suggestions?
#DecToHex
.data
msg1: .asciiz "Enter decimal number to convert: "
msg2: .asciiz " In hexadecimal: "
result: .space 8
.text
.global main
main:
# Print out the question string
la $a0, msg1
li $v0, 4
syscall
# Take in input
li $v0, 5
syscall
# Move
move $t2, $v0
# Print out string for hex representation
la $a0, msg2
li $v0, 4
syscall
li $t0, 8
la $t3, result # Answered stored here
loop1:
beqz $t0, Exit
rol $t2, $t2, 4
and $t4, $t2, (0xf) # Mask it
ble $t4, 9 sum # If less than or equal to 9, we summin fam
addi $t4, $t4, 55 # >10? add 55
b end
sum:
addi $t4, $t4, 48 # Add 48 to result
end:
sb $t4, 0($t3) # Store hex digit into result
addi $t3, $t3, 1 # Incement address counter
addi $t0, $t0, -1 # Decrement loop counter
Exit:
la $a0, result
li $v0, 4
syscall
li $v0, 10
syscall
Explanation / Answer
The program seems to be working with following modifications...there was minor syntax errors.. The modifications in the program are highlighted with bold and underline.
#DecToHex
.data
msg1: .asciiz "Enter decimal number to convert: "
msg2: .asciiz " In hexadecimal: "
result: .space 8
.text
.global main
main:
# Print out the question string
la $a0, msg1
li $v0, 4
syscall
# Take in input
li $v0, 5
syscall
# Move
move $t2, $v0
# Print out string for hex representation
la $a0, msg2
li $v0, 4
syscall
li $t0, 8
la $t3, result # Answered stored here
loop1:
beqz $t0, Exit
rol $t2, $t2, 4
and $t4, $t2, 0xf # Mask it removed paranthesis arounf hex value Oxf
ble $t4, 9 ,sum # If less than or equal to 9, we summin fam add comma(,) in between value 9 and sum
addi $t4, $t4, 55 # >10? add 55
b end
sum:
addi $t4, $t4, 48 # Add 48 to result
end:
sb $t4, 0($t3) # Store hex digit into result
addi $t3, $t3, 1 # Incement address counter
addi $t0, $t0, -1 # Decrement loop counter
Exit:
la $a0, result
li $v0, 4
syscall
li $v0, 10
syscall
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.