.data option: .asciiz "R. I. P. TRAYVON MARTIN\nENTER 0 for DECIMAL to HEXADECIMAL \nENTER 1 for HEXADECIMAL to DECIMAL\n" choice1: .asciiz " YOU ARE CONVERTING FROM HEXADECIMAL TO DECIMAL please CAPITALIZE A - F\n" choice2: .asciiz " YOU ARE CONVERTING FROM DECIMAL TO HEXADECIMAL \n" prompt1: .asciiz "Enter Hex \n" .align 2 input1: .space 9 # can only take up to 8 characters .align 2 bad: .asciiz "You Entered a Wrong Hex ..Make sure you CAPITALIZE A - F\n" result1: .asciiz "Your Decimal Equivalent is : " prompt: .asciiz "Enter the DECIMAL number : \n" ans: .asciiz "Your HEXADECIMAL Equivalent is : " result: .space 8 .text main: ################# prompt user for which conversion they wanna do #################### la $a0, option li $v0,4 syscall li $v0, 5 syscall beqz $v0, convertToHex convertToDec: ############ Announce which conversion you are doing ############## la $a0, choice1 li $v0, 4 syscall ########### Hex To Decimal ############## la $a0, prompt1 ### prompt the user for input li $v0, 4 syscall la $a0, input1 ### receives user's input li $a1, 10 li $v0, 8 syscall li $t9, -1 ## something to check for wrong hex input li $s0, 0 ## initialize where to add up the result to 0 li $t3, 1 ## 16 power to 0 li $t4, 16 ## 16 power to 1 li $t5, 256 ## 16 power to 2 li $t6, 4096 ## 16 power to 3 li $t7, 65536 ## 16 power to 4 li $t8, 1048576 ## 16 power to 5 li $s3, 16777216 ## 16 power to 6 li $s4, 268435456 ## 16 power to 7 li, $v1, 1 ## something to use later to get multiplier jal getLenght ### get lenght of string $s7 = lenght jal getMultiplier ### multiplier = $s6 li $v1, 1 looop: jal hextodec beq $v0, $t9, badHex ### tell user they entered a wrong hex alphabeth addUp: li $s5, 0 mul $s5, $v0, $s6 add $s0, $s0, $s5 addi $a0, $a0, 1 addi $s7, $s7, -1 ### decremnt lenght by i beqz $s7, finishUp jal getMultiplier ### get multiplier for that particular lenght li $v1, 1 j looop finishUp: la $a0, result1 li $v0, 4 syscall move $a0, $s0 li $v0, 1 syscall j ending ############ Ending program ############# ending: li $v0, 10 syscall ############ Each Character Convertion to Decimal ############# hextodec: lbu $t0,0($a0) #load byte from argument li $t1,0X30 li $t2,0x39 andi $t1,$t1,0x000000ff #Cast to word for comparison. andi $t2,$t2,0x000000ff bltu $t0,$t1,ERROR #error if lower than 0x30 bgt $t0,$t2,dohex #if greater than 0x39, test for A -F addiu $t0,$t0,-0x30 #OK, char between 48 and 55. Subtract 48. b return dohex: li $t1,0x41 li $t2,0x46 andi $t1,$t1,0x000000ff #Cast to word for comparison. andi $t2,$t2,0x000000ff bltu $t0,$t1,ERROR #error if lower than 0x41 bgt $t0,$t2,ERROR #error if greater than 0x46 ishex: addiu $t0,$t0,-0x37 #subtract 55 from hex char ('A'- 'F') b return ERROR: addiu $t0,$zero,-1 #return -1. return: move $v0,$t0 #move return value to register v0 jr $ra getLenght: ## a function to get the lenght of the Hex input li $s7, 0 ## $t2 is what we want to return in the end move $s1, $a0 ## get the base address and put in $s0 len: lb $t0, 0($s1) beq $t0, $zero, exit addi $s7, $s7, 1 ## Count = Count + 1 addi $s1, $s1, 1 ## i = i + 1 j len exit: addi $s7, $s7, -1 jr $ra getMultiplier: ###### setting the multiplier ####### beq $s7, $v1, power0 addi $v1, $v1, 1 beq $s7, $v1, power1 addi $v1, $v1, 1 beq $s7, $v1, power2 addi $v1, $v1, 1 beq $s7, $v1, power3 addi $v1, $v1, 1 beq $s7, $v1, power4 addi $v1, $v1, 1 beq $s7, $v1, power5 addi $v1, $v1, 1 beq $s7, $v1, power6 addi $v1, $v1, 1 beq $s7, $v1, power7 j ending power0: move $s6, $t3 jr $ra power1: move $s6, $t4 jr $ra power2: move $s6, $t5 jr $ra power3: move $s6, $t6 jr $ra power4: move $s6, $t7 jr $ra power5: move $s6, $t8 jr $ra power6: move $s6, $s3 jr $ra power7: move $s6, $s4 jr $ra badHex: la $a0, bad li $v0, 4 syscall j ending convertToHex: ############ Announce which conversion you are doing ############## la $a0, choice2 li $v0, 4 syscall ########### Decimal To Hex ############## la $a0, prompt ## prompt the user for input li $v0, 4 syscall li $v0, 5 syscall move $t2, $v0 la $a0, ans li $v0, 4 syscall li $t0, 8 la $t3, result # where result will be stored Loop: beqz $t0, Exit # branch to exit if counter is equal to zero rol $t2, $t2, 4 # rotate 4 bits to the left and $t4, $t2, 0xf # mask with 1111 ble $t4, 9, Sum # if less than or equal to nine, branch to sum addi $t4, $t4, 55 # if greater than nine, 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 # increment address counter addi $t0, $t0, -1 # decrement loop counter j Loop Exit: la $a0, result li $v0, 4 syscall la $v0, 10 syscall