如何从字符串输入

时间:2018-06-11 20:03:19

标签: assembly mips

我正在使用MIPS本机指令编写程序,这将允许用户输入三个十进制数字作为空终止字符串。

每个号码不能超过六位数。如果用户输入逗号(例如123,456),则从内存中删除逗号。

存储在存储器地址0x10000000中的第一个数字,第二个存储在0x10000008中,第三个存储在0x10000010中。

  • 我的代码只是在firstNumCounterChr

  • 一遍又一遍地循环
  • 代码不会增加$ t0

  • 代码不会递增地址
  • 中的下一个字节

我的代码效率低下。我已经知道了,但我觉得这比使用子程序更容易,因为我几乎不了解MIPS。

请建议。

代码

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

.data  

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

main:    

#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
addi $t1, 0               #$t1 is the counter set to 0  
add $t3, 0x2c             # 0x2c is acsii of "," in hex

firstNumCountChr:

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, firstNumIncrem     #branch if symbol doesn't equal ","
or $0, $0, $0
add $t4, $a0, $0          #$t4 will save position of "," 

firstNumIncrem:

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


main2:

#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 $t0, $0, 1           #increment address
addi $t1, $t1, 1          #increment counter
j secondNumCountChr               #loop    

main3:                                                                                                                                                                  

#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 $t0, $0, 1           #increment address
addi $t1, $t1, 1          #increment counter
j thirdNumIncrem               #loop

end:
add $0, $0, $0

0 个答案:

没有答案