data
string: .asciiz "The String is\0"
string2:
.text
main:
#Subtract A and 10 from A to F
#Subtract 0 if between 0 to 9
#\0 : 5C30
sw $a0, string
str2int: # convert string to integer
li $t6, 0x30 # Temp variable contains $t6 = '0'
li $t7, 0x39 # Temp variable contains $t7 = '9'
li $v0, 0 # initialize value $v=0
move $t0, $a0 # $t0 = pointer to string
lb $t1, ($t0) # load $t1 = digit character
loop:
blt $t1, $t6, NoDigit # char < '0'
bgt $t1, $t7, NoDigit # char > '9'
subu $t1, $t1, $t6 # Convert char to integer
mul $v0, $v0, 10 # multiply value by 10
add $v0, $v0, $t1 # $v0 = $v0 x 10 + digit
addiu $t0, $t0, 1 # point to next char
lb $t1, ($t0) # load $t1 = next digit
bne $t1, $0, loop # branch if not end of string
jr $ra # return integer value
NoDigit:
li $v0, -1 # return -1 in $v0
jr $ra # return integer value
CheckBetween2to16: li $t0, 0X30
CheckNull: #Return 0
CheckLegal: #Check if
Converting:
您可以采取以下措施:
如果第二个参数不在2到16(含)范围内,则打印错误信息并返回零值。
所以我有一个将十六进制转换为整数的示例代码。但是对于这个给定的项目,我们需要实现一个程序,将空终止的字符串地址转换为整数值。