Assembly 如何从字符串输入中删除逗号
我正在开发一个使用MIPS本机指令的程序,该程序允许用户输入三个十进制数作为以null结尾的字符串 每个数字不能超过六位数。如果用户输入逗号(例如123456),请从内存中删除该逗号 第一个数字存储在内存地址0x10000000中,第二个数字存储在0x10000008中,第三个数字存储在0x1000010中Assembly 如何从字符串输入中删除逗号,assembly,mips,Assembly,Mips,我正在开发一个使用MIPS本机指令的程序,该程序允许用户输入三个十进制数作为以null结尾的字符串 每个数字不能超过六位数。如果用户输入逗号(例如123456),请从内存中删除该逗号 第一个数字存储在内存地址0x10000000中,第二个数字存储在0x10000008中,第三个数字存储在0x1000010中 我的代码在firstNumCounterChr 代码不增加$t0 代码不增加地址中的下一个字节 我的代码效率很低。我已经知道了,但我发现这样比使用子程序更容易,因为我几乎不理解MIPS
- 我的代码在
firstNumCounterChr
- 代码不增加$t0
- 代码不增加地址中的下一个字节
.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
用户不能输入超过1个逗号?这将使您的8个字节溢出,包括0终止符。另外,不要将读/写数据放入
.text
部分;这是针对代码和只读数据的。在.bss
部分中放置标签和.skip 8
,或在.data
中保留空间。您不需要子程序,但可以使用循环执行第一、第二和第三个数字。使用一个不同的寄存器来跟踪起始地址,并在一个外部循环中将其递增8bne
在另一个寄存器中的结束地址上,因此您有一个do{…string_buf+=8;}while(string_buf!=endp)代码>循环。你有没有试过用C或其他语言设计算法?像tmp=*src++;如果(tmp!=',')*dst++=tmp代码>,您可以通过从dst=src开始执行此操作。如果你在商店之后才检查0
addi$t1,0
@Michael,我的天啊,我本想做addi$t1,0,0的话,它会免费复制nul终结者。谢谢知道如何遍历字符串吗?