Question: MIPS Assembly Language Program: Below is a convert hex to int function, how to add mips assembly code to not count leading zeros and show
MIPS Assembly Language Program:
Below is a convert hex to int function, how to add mips assembly code to not count leading zeros and show overflow if bigger than maximum positive integer.
For example: "0000000cde", input more than 8 bytes but not counting zero so it will convert cde to integer; "90000000" convertion to integer is bigger than max pos int so show overflow.
Convert:
li $t0, -1 #initialize count variable to find length of string move $t1, $a0 #load base address of string strlen: #loop to find out string length lb $t2, ($t1) #load one byte add $t1, $t1, 1 #increment pointer by one byte add $t0, $t0, 1 #increment count bnez $t2, strlen #check of \0 character and exit
bgt $t0, 8, overflow_error #overflow if length of string is > 8Bytes
sub $t0, $t0, 1 #strlen-1 for offset add $t1, $a0, $t0 #add offset to base address li $t2, 1 #initialise to 16^0 li $v0, 0 #initialise result to 0 li $v1, 0 #v1 = 0 for no overflow loop: #loop to convert string to int lb $t3, ($t1) #load one byte check_1: #check for 'a' - 'f' blt $t3, 97, check_2 #if not, check for 'A' - 'F' or '0' - '9' sub $t3, $t3, 39 #subtract 39 to get 10+48 - 15+48 b check_3 #go to check 3 check_2: #check for 'A' - 'F' blt $t3, 65, check_3 #if not go to check 3 sub $t3, $t3, 7 #subtract 7 to get 10+48 - 15+48 check_3: sub $t3, $t3, 48 #subtract 48 to get integers 0 to 15
mul $t3, $t3, $t2 #multiply with power of 16 add $v0, $v0, $t3 #add to result mul $t2, $t2, 16 #multiply with 16 to increment power sub $t1, $t1, 1 #decrement offset by 1 bge $t1, $a0, loop #repeat loop for all bytes
b return #go to return
overflow_error: #if overflow is there li $v0, 0x32B #write 0x32b in $v0 li $v1, 1 #write 1 in $v1
return: #return to caller jr $ra
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
