How to remove a comma from string input

2019-07-12 16:25发布

I'm working on a program using MIPS native instructions, that will allow user to input three decimal numbers as null terminated strings.

Each number can't exceed six digits. If the user inputs a comma (e.g 123,456) remove the comma from memory.

The first number stored in memory address 0x10000000, the second in 0x10000008, and the third in 0x10000010.

  • My code just keeps looping over and over again at the firstNumCounterChr

  • Code doesn't increment $t0

  • Code doesn't increment next byte in address

My code is inefficient. I know that already, but I find it easier like this than using subroutines cause I hardly understand MIPS.

Please suggest.

Code

.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

标签: assembly mips
0条回答
登录 后发表回答