我需要一个存储为Hexa编号的字符串数组,我需要将其转换为基数10(十进制)。
.data
inputNumberArray: .space 32
使用
读取此数组la $a0, inputNumberArray # load inputBase address to a0
li $v0, 8 # read_string syscall code = 8
li $a1, 32 # space allocated for inputBase
syscall
所以我有一个类似
的数组 inputNumberArray = 3E8
我需要输出
outputArray = 1000
谢谢:D
答案 0 :(得分:1)
最后我开始工作了!
我在inputNumberArray
### HEXA -> DECIMAL
fromHexaStringToDecimal:
# start counter
la $t2, inputNumberArray # load inputNumber address to t2
li $t8, 1 # start our counter
li $a0, 0 # output number
j hexaStringToDecimalLoop
hexaStringToDecimalLoop:
lb $t7, 0($t2)
ble $t7, '9', inputSub48 # if t7 less than or equal to char '9' inputSub48
addi $t7, $t7, -55 # convert from string (ABCDEF) to int
j inputHexaNormalized
inputHexaNormalized:
blt $t7, $zero, convertFinish # print int if t7 < 0
li $t6, 16 # load 16 to t6
mul $a0, $a0, $t6 # t8 = t8 * t6
add $a0, $a0, $t7 # add t7 to a0
addi $t2, $t2, 1 # increment array position
j hexaStringToDecimalLoop
inputSub48:
addi $t7, $t7, -48 # convert from string (ABCDEF) to int
j inputHexaNormalized
所以最后我将把十进制数存储在$ a0。
诀窍是检查数字是否有ABCEDF字符,在这种情况下总和是-55,否则为-48。
示例:
'9' -48 = 9 # '9' char to 9 int
'A' -55 = 10 # 'A' char to 10 int
感谢@wallyk。我开始在python中再次编写它然后转换为MIPS。