如何使用本机mips

时间:2018-06-13 18:05:18

标签: mips

这是我之前编辑的问题的后续内容: How to remove a comma from string input

我设法修复了无限循环,并找到了通过字符串递增的方法。我的代码现在可以遍历一个字符串(例如123,456)找到“,”的地址并存储在$ t4中。

但我无法弄清楚的是如何将所有数字向左移动以从字符串中删除“,”(在内存中应该看起来像这样:123,456 - > 123456)

我正在使用原生MIPS指令。如果你可以这样帮助我,我会非常感激。

.globl main
.globl main2
.globl main3
.globl firstNumCountChr
.globl firstNumIncrem
.globl secondNumCountChr
.globl secondNumIncrem
.globl thirdNumCountChr
.globl thirdNumInCrem

.data  
prompt1: .asciiz "Enter first number:"
prompt2: .asciiz "Enter second number:"
prompt3: .asciiz "Enter third number:"

.text              
# 0x10000000 will store first number        
# 0x10000008 will store second number      
# 0x10000010 will store third number

main:  

#display prompt1
addi $v0, $0, 4
lui $a0, 0x1000
syscall

#Input first number                                                                                                                                                                                                                                                                                                                                          
lui $a0, 0x1000                                                                                                                                                                
ori $a0, 0x0000             #reads number into memory(0x10000000)                                                                                                                                                                                                                                           
addi $a1, $0, 8             #7 characters 
addi $v0, $0, 8                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
syscall   

#removing comma of first number
add $t1, $0, $0               #$t1 is the counter set to 0  
add $t3, $0, 0x2c             # 0x2c is acsii of "," in hex

firstNumCountChr:

lb $t2, 0($a0)            #load first byte from address in $a0
beq $t2, $0, firstNumRemoveComma     #if $t2 == 0 go to firstNumRemoveComma
or $0, $0, $0             #NOP
bne $t2, $t3, firstNumIncrem     #branch if symbol doesn't equal ","
or $0, $0, $0
add $t4, $a0, $0          #$t4 will save position of "," 

firstNumIncrem:

addi $a0, $a0, 1           #increment address
addi $t1, $t1, 1          #increment counter
j firstNumCountChr              #loop

firstNumRemoveComma:

beq $t4, $0, main2        #branch if $t4 == 0 (no comma) to main2
or $0, $0, $0             #NOP


main2:

#display prompt2
addi $v0, $0, 4
lui $a0, 0x1000
addi $a0, $a0, 20
syscall

#inputing second number                                                                                                                                                                                                                                                                                                                                                                                        
lui $a0, 0x1000                                                                                                                                                          
ori $a0, 0x0008             #reads number into memory(0x10000008)                                                                                           
addi $a1,$0, 8              #7 characters   
addi $v0, $0, 8                                                                                                                                                                                                                                                                                                                                  
syscall 

#removing comma of second number
addi $t1, 0               # $t1 is the counter set to 0  
add $t3, 0x2c             # 0x2c is acsii of "," in hex

secondNumCountChr:

lb $t2, 0($a0)            # load first byte from address in $a0
beq $t2, $0, end          # if $t2 == 0 go to end
or $0, $0, $0             # NOP
bne $t2, $t3, secondNumIncrem     # branch if symbol doesn't equal ","
or $0, $0, $0             # NOP
add $t4, $a0, $0          # $t4 will save position of ","

secondNumIncrem:

addi $a0, $a0, 1           #increment address
addi $t1, $t1, 1          #increment counter
j secondNumCountChr               #loop    

main3:  

#display prompt3
addi $v0, $0, 4
lui $a0, 0x1000
addi $a0, $a0, 41
syscall                                                                                                                                                                

#inputting third number                                                                                                                                                                                                                                                                                                                                                                                  
lui $a0, 0x1000                                                                                                                                                        
ori $a0, 0x0010              #reads number into memory(0x10000010)                                                                  
addi $a1, $0, 8              #7 characters  
addi $v0,$0, 8                                                                                                                                                                                                      
syscall  

#removing comma of third number
addi $t1, 0               #$t1 is the counter set to 0  
add $t3, 0x2c             # 0x2c is acsii of "," in hex

thirdNumCountChr:

lb $t2, 0($a0)            #load first byte from address in $a0
beq $t2, $0, end          # if $t2 == 0 go to end
or $0, $0, $0
bne $t2, $t3, thirdNumInCrem     # branch if symbol doesn't equal ","
or $0, $0, $0
add $t4, $a0, $0          # $t4 will save position of ","

thirdNumInCrem: 

addi $a0, $a0, 1           #increment address
addi $t1, $t1, 1          #increment counter
j thirdNumCountChr               #loop

end:
add $0, $0, $0

0 个答案:

没有答案