urgent help required. - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

urgent help required.

I need to convert a hexadecimal number to an unsigned integer.
I'm a beginner in MIPS. I've been trying to figure out what's wrong in my code, I just can't figure it out. Can anyone help me out?
.data
PROMPT: .asciiz "Enter Hex: "

.text
main:
##################################
# INITIALIZE REGISTERS #
##################################
li $s0, 0 #INPUT HEX
li $s1, 0 #TEMP BYTE STORAGE
li $s2, 0 #I
li $s4, 1 #PRODUCT TO STORE THE POWER OF 16
li $t4,0 #STORE FINAL DECIMAL NUMBER

##################################
# MAIN #
##################################
j INPUT #PROMT FOR HEX AND STORE IT
START: j HIN #HEX INPUT TO DECIMAL



###################################
# READ AND STORE INPUT HEX #
###################################
INPUT: la $a0, PROMPT #LOAD THE ADDRESS OF PROMPT FOR HEX
li $v0, 4 #SYSTEM CALL FOR THE PRINT STRING
syscall #PRINT THE PROMPT
li $v0, 8 #SYSTEM CALL TO READ IN A STRING
move $a0,$s6
syscall #READ IN HEX
move $s0, $v0 #COPY ORIGIONAL HEX VALUE INTO s0
j START #CONVERT TO DECIMAL

###################################
# HEX INPUT #
###################################

HIN:
beq $s2,8,PRINT #IF END OF HEX INPUT(8), BREAK
lb $s1,0($s0) #STORE CURRENT HEX CHAR IN $s1

beq $s1,'0',IF0 #IF CHAR= 0
beq $s1,'1',IF1 #IF CHAR= 1
beq $s1,'2',IF2 #IF CHAR= 2
beq $s1,'3',IF3 #IF CHAR= 3
beq $s1,'4',IF4 #IF CHAR= 4
beq $s1,'5',IF5 #IF CHAR= 5
beq $s1,'6',IF6 #IF CHAR= 6
beq $s1,'7',IF7 #IF CHAR= 7
beq $s1,'8',IF8 #IF CHAR= 8
beq $s1,'9',IF9 #IF CHAR= 9
beq $s1,'a',IFA #IF CHAR= a
beq $s1,'b',IFB #IF CHAR= b
beq $s1,'c',IFC #IF CHAR= c
beq $s1,'d',IFD #IF CHAR= d
beq $s1,'e',IFE #IF CHAR= e
beq $s1,'f',IFF #IF CHAR= f
beq $s1,'A',IFA #IF CHAR= A
beq $s1,'B',IFB #IF CHAR= B
beq $s1,'C',IFC #IF CHAR= C
beq $s1,'D',IFD #IF CHAR= D
beq $s1,'E',IFE #IF CHAR= E
beq $s1,'F',IFF #IF CHAR= F
IF0: addi $s3,$zero,0
j RUTGER
IF1: addi $s3,$zero,1
j RUTGER
IF2: addi $s3,$zero,2
j RUTGER
IF3: addi $s3,$zero,3
j RUTGER
IF4: addi $s3,$zero,4
j RUTGER
IF5: addi $s3,$zero,5
j RUTGER
IF6: addi $s3,$zero,6
j RUTGER
IF7: addi $s3,$zero,7
j RUTGER
IF8: addi $s3,$zero,8
j RUTGER
IF9: addi $s3,$zero,9
j RUTGER
IFA: addi $s3,$zero,10
j RUTGER
IFB: addi $s3,$zero,11
j RUTGER
IFC: addi $s3,$zero,12
j RUTGER
IFD: addi $s3,$zero,13
j RUTGER
IFE: addi $s3,$zero,14
j RUTGER
IFF: addi $s3,$zero,15
j RUTGER

RUTGER:
rol $s4,$s4,4 #OBTAINING THE CORRESPONDING POWER OF 16 WITHOUT ANOTHER LOOP
mul $t2,$s4,$s3 #MULTIPLYING THE HEX CHARACTER'S DECIMAL VALUE WITH THE APPROPRIATE POWER OF 16
add $t4,$t4,$t2 #STORING THE RESULT IN $t4
addi $s2,$s2,1 #INCREASE I
addi $s0,$s0,1 #INCREASE POSITION
j HIN #RESTART LOOP

PRINT: move $a0,$t4 #MOVING CONTENTS OF RESULT REGISTER TO PRINT
li $v0,1 #SYSTEM CALL FOR PRINT INTEGER
syscall #PRINT THE RESULT



It's a bit clumsy code. Its giving me an error(in MARS 3.6):
Runtime exception at 0x00400048: address out of range 0x00000008
Go: execution terminated with errors.


Sign In or Register to comment.