【问题标题】:String manipulation code gives no output字符串操作代码没有输出
【发布时间】:2017-10-01 15:51:29
【问题描述】:

所以基本上,我必须在不知道大小的情况下将字符串翻转到位。 所以我写了代码来找到大小,然后从两边替换字符串中的字符,直到我们到达中间,我们就完成了。但是,我的代码没有输出,我的大脑现在正在沸腾 有人可以帮忙吗

    # a program to reverse a string in memory (without allocating additional memory)
.data
string: .asciiz "CMPS 255"

.text

#find the length of the string
li $s0, 0 #max index
li $s1, 0 #initial index

findNull:lb $t0,string($s0)
    beq $t0,$zero,foundNull
    add $s0,$s0,1
    j findNull

foundNull:

    beq $s0, $s1, done
    lb $t1, string($s0) #right value
    lb $t2, string($s1) #left value
    sb $t1, string($s1) #switched
    sb $t2, string($s0) #switched
    addi $s1, $s1, 1
    subi $s0, $s0, 1
    j foundNull

done:

la $a0,string
li $v0, 4

syscall

【问题讨论】:

  • 你试过使用调试器吗?
  • 我正在使用mar进行编码,它运行正常只是没有输出
  • 我的意思是说我正在使用火星,所以是的,我有一个调试器
  • 那么当你到达syscall指令时,string的内容是什么?
  • 我应该反向获取字符串我所做的只是将字符串地址中的字符保存在临时寄存器中,并将它们从最左侧交换到最右侧,直到我到达中间并终止

标签: arrays string assembly mips


【解决方案1】:

所以,我花了一点时间为这个算法写了一些伪代码...... 我按照上次的确切步骤进行操作,但这次我的工作更有条理,这是我的最终答案。我希望它对找到它的人有所帮助

 .data
string: .asciiz "hello"

.text
la $a1, string
li $t9,0 #counter for the size

loop:
lb $a2, ($a1)
beq $a2, $zero, found
addi $a1, $a1, 1
add $t9, $t9, 1
j loop

found: #its time to reverse
sub $t9, $t9, 1 #max index
li $t8 , 0 #staring index

looptwo:
beq $t8, $t9, done
lb $t1, string($t8) #left most
lb $t2, string($t9)#right most
sb $t1, string($t9)
sb $t2, string($t8)
add $t8, $t8 , 1
sub $t9, $t9 , 1
j looptwo

done:
li $v0 , 4
la $a0, string
syscall

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多