我正在尝试使用MIPS程序集实现字符串到int的转换。我使用MARS Simulator软件作为我的编码平台。下面你可以看到我取得的成就。堆栈和子程序/函数的使用是我想要做的事情(按照规范),我的问题可能与那些有关。我认为。
如果字符串不超过10位,则此程序将字符串成功转换为int。 “1234567890”工作正常。但是,“12345678901”没有,它返回整数-539222987。因为它是否定的,我认为我正在经历某种溢出。但是,我仍然没有任何线索,以及如何解决它。
.data
STRING: .asciiz "1234567890"
.text
main:
la $t0, STRING
addi $sp, $sp, -4
sw $t0, 0($sp) # Let's save STRING to a stack
jal string2int # Let's do the conversion in a subprogram
# Here we have returned from the subprogram and we have a int in 4($sp). However, the issue is that it gets wrong when string has more than 10 digits.
lw $s7, 4($sp)
add $a0, $s7, $zero
jal print_int # Test print the int as integer.
addi $v0, $zero, 10
syscall # Shutdown
string2int:
addi $sp, $sp, -4
sw $ra, 0($sp) # Save the return address to the beginning of the stack $ra
lw $s5, 4($sp) # Load the string from the stack into s5.
#la $a0,($s5) #
#jal print_string # Print the string
addi $t0, $zero, 10
addi $s2, $zero, 0
loop:
lbu $t1, ($s5) # Load unsigned char from array into t1
beq $t1, $0, end # NULL terminator found
blt $t1, 48, error # Check if char is not a digit (ascii<'0')
bgt $t1, 57, error # Check if char is not a digit (ascii>'9')
addi $t1, $t1, -48 # Converts t1's ascii value to dec value
mul $s2, $s2, $t0 # Sum *= 10
add $s2, $s2, $t1 # Sum += array[s1]-'0'
addi $s5, $s5, 1 # Increment array address
j loop # Jump to start of loop
end:
#add $a0, $s2, $zero
#jal print_int # Let's put s2 into a0 and print is as integer.
# HERE WE HAVE THE INT IN S2
sw $s2, 4($sp) # Save it into stack
lw $ra, 0($sp)
#addi $sp, $sp, 4
jr $ra
error:
addi $v0, $zero, 10
syscall # Shutdown
print_string:
addi $v0, $zero, 4
syscall
jr $ra
print_int:
addi $v0, $zero, 1
syscall
jr $ra
对于格式化的小混乱感到抱歉,我无法解决此问题。在我的IDE中,它没问题。 :)
编辑:
我现在开始做的是尝试创建一个循环,该循环只通过字符串中的10个第一个数字,将它们保存到寄存器中,然后跳转到另一个循环,开始将下一个10保存到另一个寄存器中。然后我有两个10位整数的寄存器,它们一起代表20(或19)位数。我仍然无法知道这是否会起作用,或者我在这里有一些基本问题,但是现在我的编辑器快速粘贴:
addi $t0, $zero, 10
addi $s2, $zero, 0 # Low part
addi $t4, $zero, 0
addi $s3, $zero, 0 # High part
addi $t5, $zero, 0
lp:
lbu $t1, ($s5) #load unsigned char from array into t1
beq $t1, $0, FIN #NULL terminator found
blt $t1, 48, error #check if char is not a digit (ascii<'0')
bgt $t1, 57, error #check if char is not a digit (ascii>'9')
addi $t1, $t1, -48 #converts t1's ascii value to dec value
mul $s2, $s2, $t0 #sum *= 10
add $s2, $s2, $t1 #sum += array[s1]-'0'
addi $s5, $s5, 1 #increment array address
addi $t4, $t4, 1
beq $t4, 10, loop2
j lp #jump to start of loop
loop2:
lbu $t1, ($s5) #load unsigned char from array into t1
beq $t1, $0, FIN #NULL terminator found
blt $t1, 48, error #check if char is not a digit (ascii<'0')
bgt $t1, 57, error #check if char is not a digit (ascii>'9')
addi $t1, $t1, -48 #converts t1's ascii value to dec value
mul $s3, $s3, $t0 #sum *= 10
add $s3, $s3, $t1 #sum += array[s1]-'0'
addi $s5, $s5, 1 #increment array address
addi $t5, $t5, 1
beq $t5, 9, FIN
j loop2 #jump to start of loop
FIN:
# add $a0, $s2, $zero
# addi $v0, $zero, 1
# syscall #Testing
## HERE WE SHOULD HAVE INTEGERS PARTLY IN S2 AND PARTLY IN S3.
sw $s2, 4($sp) # Saving to stack at 4($sp)
sw $s3, 8($sp) # Saving to stack at 8($sp)
我现在很想知道如果保存到像这样的堆栈就可以作为代码使用了。