Question: . data hex _ str: . asciiz 1 c # Example hexadecimal string newline: . asciiz # Newline for readability result

.data
hex_str: .asciiz "1c" # Example hexadecimal string
newline: .asciiz "
" # Newline for readability
result_msg: .asciiz "The decimal value is: " # Result message
error_msg: .asciiz "Invalid hex digit.
" # Error message for invalid input
.text
.globl main
# Main function
main:
# Load the hexadecimal string
la $a0, hex_str # Load the address of the hex string into $a0
# Call the calc function to convert hex to decimal
jal calc # Jump and link to calc (result will be in $v0)
# Check if the result is negative (indicating an error)
bltz $v0, handle_error # If $v0<0, jump to handle_error
# Print the result message
li $v0,4 # Syscall for printing string
la $a0, result_msg # Load address of result message
syscall # Print result message
# Print the decimal value
li $v0,1 # Syscall for printing integer
move $a0, $v0 # Move the return value from calc into $a0
syscall # Print the decimal value
# Exit program
j end_program # Jump to end_program
handle_error:
# Print the error message
li $v0,4 # Syscall for printing string
la $a0, error_msg # Load address of error message
syscall # Print error message
end_program:
li $v0,10 # Syscall to exit
syscall # Exit the program
# calc function: Convert hex string to decimal
# Arguments: $a0= address of hex string
# Returns: $v0= decimal value or -1 for error (invalid hex digit)
calc:
# Initialize registers
li $v0,0 # Initialize decimal_value (in $v0) to 0
li $t1,0 # Initialize loop counter i (for string index)
# Calculate the length of the hex string (up to the null terminator)
la $t2, hex_str # Load address of hex string into $t2
li $t3,0 # Initialize length counter
find_len:
lb $t4,0($t2) # Load a byte from the hex string
beqz $t4, start_conversion # If null terminator, break
addi $t3, $t3,1 # Increment length counter
addi $t2, $t2,1 # Move to the next character
j find_len # Repeat the loop
start_conversion:
# Reset address of the hex string
la $t2, hex_str # Reload address of hex string into $t2
convert_loop:
lb $t4,0($t2) # Load the current hex character into $t4
beqz $t4, end_calc # If null terminator, end conversion
# Convert hex character to decimal digit
li $t5,0 # Initialize decimal_digit ($t5)
li $s0,'0' # ASCII value of '0'
li $s1,'9' # ASCII value of '9'
li $s2,'a' # ASCII value of 'a'
li $s3,'f' # ASCII value of 'f'
li $s4,16 # Store base 16
# Check if the character is a number (0-9)
blt $t4, $s0, check_alpha # If $t4<'0', check for letters
bgt $t4, $s1, check_alpha # If $t4>'9', check for letters
sub $t5, $t4, $s0 # Convert '0'-'9' to 0-9
j calculate_value # Skip to calculate_value
check_alpha:
# Check if the character is a lowercase letter (a-f)
blt $t4, $s2, invalid_char # If $t4<'a', it's invalid
bgt $t4, $s3, invalid_char # If $t4>'f', it's invalid
sub $t5, $t4, $s2 # Convert 'a'-'f' to 0-5
addi $t5, $t5,10 # Add 10 to convert 'a'-'f' to 10-15
calculate_value:
# Shift current value by 4 bits (equivalent to multiplying by 16)
sll $v0, $v0,4 # Shift decimal_value left by 4 bits
add $v0, $v0, $t5 # Add the decimal_digit to decimal_value
# Move to the next character in the hex string
addi $t2, $t2,1 # Move to the next character
j convert_loop # Repeat the conversion loop
invalid_char:
li $v0,-1 # Set return value to -1 to indicate error
jr $ra # Return to caller (main)
end_calc:
jr $ra # Return to caller (main)
This is the kind of output I need but I aint getting it:
Enter a hexadecimal value.
1c
The hexadecimal number entered is the decimal value: 28
Enter a hexadecimal value.
fffffffe
The hexadecimal number entered is the decimal value: -2
Enter a hexadecimal value.
1g
Invalid hex digit: g
Enter a hexadecimal value.
123456789
More than 8 hex digits entered.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!