Question: . data hex _ str: . asciiz 1 c # Example hexadecimal string newline: . asciiz # Newline for readability result
data
hexstr: asciiz c # Example hexadecimal string
newline: asciiz
# Newline for readability
resultmsg: asciiz "The decimal value is: # Result message
errormsg: asciiz "Invalid hex digit.
# Error message for invalid input
text
globl main
# Main function
main:
# Load the hexadecimal string
la $a hexstr # Load the address of the hex string into $a
# Call the calc function to convert hex to decimal
jal calc # Jump and link to calc result will be in $v
# Check if the result is negative indicating an error
bltz $v handleerror # If $v jump to handleerror
# Print the result message
li $v # Syscall for printing string
la $a resultmsg # Load address of result message
syscall # Print result message
# Print the decimal value
li $v # Syscall for printing integer
move $a $v # Move the return value from calc into $a
syscall # Print the decimal value
# Exit program
j endprogram # Jump to endprogram
handleerror:
# Print the error message
li $v # Syscall for printing string
la $a errormsg # Load address of error message
syscall # Print error message
endprogram:
li $v # Syscall to exit
syscall # Exit the program
# calc function: Convert hex string to decimal
# Arguments: $a address of hex string
# Returns: $v decimal value or for error invalid hex digit
calc:
# Initialize registers
li $v # Initialize decimalvalue in $v to
li $t # Initialize loop counter i for string index
# Calculate the length of the hex string up to the null terminator
la $t hexstr # Load address of hex string into $t
li $t # Initialize length counter
findlen:
lb $t$t # Load a byte from the hex string
beqz $t startconversion # If null terminator, break
addi $t $t # Increment length counter
addi $t $t # Move to the next character
j findlen # Repeat the loop
startconversion:
# Reset address of the hex string
la $t hexstr # Reload address of hex string into $t
convertloop:
lb $t$t # Load the current hex character into $t
beqz $t endcalc # If null terminator, end conversion
# Convert hex character to decimal digit
li $t # Initialize decimaldigit $t
li $s # ASCII value of
li $s # ASCII value of
li $sa # ASCII value of a
li $sf # ASCII value of f
li $s # Store base
# Check if the character is a number
blt $t $s checkalpha # If $t check for letters
bgt $t $s checkalpha # If $t check for letters
sub $t $t $s # Convert to
j calculatevalue # Skip to calculatevalue
checkalpha:
# Check if the character is a lowercase letter af
blt $t $s invalidchar # If $ta it's invalid
bgt $t $s invalidchar # If $tf it's invalid
sub $t $t $s # Convert af to
addi $t $t # Add to convert af to
calculatevalue:
# Shift current value by bits equivalent to multiplying by
sll $v $v # Shift decimalvalue left by bits
add $v $v $t # Add the decimaldigit to decimalvalue
# Move to the next character in the hex string
addi $t $t # Move to the next character
j convertloop # Repeat the conversion loop
invalidchar:
li $v # Set return value to to indicate error
jr $ra # Return to caller main
endcalc:
jr $ra # Return to caller main
This is the kind of output I need but I aint getting it:
Enter a hexadecimal value.
c
The hexadecimal number entered is the decimal value:
Enter a hexadecimal value.
fffffffe
The hexadecimal number entered is the decimal value:
Enter a hexadecimal value.
g
Invalid hex digit: g
Enter a hexadecimal value.
More than hex digits entered.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
