如何通过汇编(RISC-V)在字符串中出现双辅音时打印减号(-)



我有一个程序练习需要帮助。该程序是通过RARS 1.3创建的。到目前为止,这是我创建的每次读取和输入一个字符串的代码。程序需要在字符串中的所有双辅音之间加一个(-(。要做到这一点,我需要在字符串中循环搜索双辅音,并用第二个字符串打印结果。有什么建议吗?

.global _start      # Provide program starting address to linker
_start: 
la t1,sourcestring
li t2,10 #enter
loop:
addi a7,x0,12 #readchar
ecall
# input is in a0
sb a0,0(t1)
addi t1,t1,1
bne a0,t2,loop
sb x0,0(t1)

.data
sourcestring:      .word 30 #30
targetstring:      .word 40 #40

对您的代码进行了一些修改。

.global _start      # Provide program starting address to linker
_start: 
la t1,sourcestring
li t2,10 #enter
li t3,65 #A
li t4,45 #-
li t5,0xffff #initilaize t5 to something readchar is not supposed to return.
j label1
loop:
mv t5,a0 #put old a0 value in t5
label1: 
addi a7,x0,12 #readchar
ecall
# input is in a0
sb a0,0(t1)
addi t1,t1,1
beq a0,t3,loop
bne a0,t5,label2 #if a0 is different from old a0 value dont add -
sb  t4,0(t1)
addi t1,t1,1
label2: 
bne a0,t2,loop
sb x0,0(t1)

.data   
sourcestring:      .word 30 #30
targetstring:      .word 40 #40

在这个例子中,如果我们发现两次相同的字符(除了A(,我们会添加-。并且直接在源字符串中进行修改。如果您愿意,您可以将其修改为写入targetstring。

请注意,tagetstring和sourcesring在您的示例中只有一个单词(4个字节(,根据您的字符串,您可以超过此大小并覆盖不想覆盖的数据。

祝你的修改好运。

最新更新