在 MIPS 汇编中反转字符串

发布于 2024-12-17 01:34:05 字数 1576 浏览 2 评论 0原文

我试图提示用户输入字符串的长度,为该字符串分配空间,然后反向打印出来。

对于我的一生,我不明白为什么这不起作用..

Sample Output:
(spim) run    
Please enter an integer:    
7    
string
(spim)

现在“字符串”的长度应该是 6 对吗? + 应该使它成为 7 的空终止字符。任何人都可以发现我的方法哪里出了问题吗?

.data
    nl: .asciiz "\n"
    inputPrompt: .asciiz "Please enter an integer:\n"

    theString: .space 32
    theInteger: .word 1

.text
main: 
    la $a0, inputPrompt #load address a0 with prompt
    li $v0, 4       #load system call, print string into v0
    syscall 

    li $v0, 5       #load system call, read int into v0
    syscall
    sw $v0, theInteger  #store saved int into $t0

    li $v0, 8           #load system call, read string with mem address
    la $a0, theString   #load address of reserved string space
    lw $a1, theInteger  #load address of saved int length for string    
    syscall

    lw $t0, theInteger
    add $a1,$zero,$t0   #pass lenght of string
    jal stringreverse   #reverse the string

stringreverse:
    add $t0,$a0,$zero   #starting address
    add $t1,$zero,$zero     #i = 0
    addi $t2,$a1,-1     #j = length-1

loop:
    add $t3,$t0,$t1
    lb $t4,0($t3)   #the lb string[i]
    add $t5,$t0,$t2
    lb $t6,0($t5)   #the lb string[j]
    sb $t4,0($t5)   #string[j] = string[i]
    sb $t6,0($t3)   #string[i] = string[j]
    addi $t1,$t1,1  #i++
    addi $t2,$t2,-1     #j--

    slt $t6,$t2,$t1
    beqz $t6,loop

exit:
    li $v1, 4       #system call to print reversed string
    la $a2, 0($a1)
    syscall

    li $v0, 10
    syscall         # Exit program

I'm trying to prompt the user for the length of a string, allocate space for that string, then print it out in reverse.

For the life of me, I can't figure out why this isn't working..

Sample Output:
(spim) run    
Please enter an integer:    
7    
string
(spim)

Now the length of "string" should be 6 right? + the null terminating character which should make it 7. Can anyone spot where I'm going wrong with my approach?

.data
    nl: .asciiz "\n"
    inputPrompt: .asciiz "Please enter an integer:\n"

    theString: .space 32
    theInteger: .word 1

.text
main: 
    la $a0, inputPrompt #load address a0 with prompt
    li $v0, 4       #load system call, print string into v0
    syscall 

    li $v0, 5       #load system call, read int into v0
    syscall
    sw $v0, theInteger  #store saved int into $t0

    li $v0, 8           #load system call, read string with mem address
    la $a0, theString   #load address of reserved string space
    lw $a1, theInteger  #load address of saved int length for string    
    syscall

    lw $t0, theInteger
    add $a1,$zero,$t0   #pass lenght of string
    jal stringreverse   #reverse the string

stringreverse:
    add $t0,$a0,$zero   #starting address
    add $t1,$zero,$zero     #i = 0
    addi $t2,$a1,-1     #j = length-1

loop:
    add $t3,$t0,$t1
    lb $t4,0($t3)   #the lb string[i]
    add $t5,$t0,$t2
    lb $t6,0($t5)   #the lb string[j]
    sb $t4,0($t5)   #string[j] = string[i]
    sb $t6,0($t3)   #string[i] = string[j]
    addi $t1,$t1,1  #i++
    addi $t2,$t2,-1     #j--

    slt $t6,$t2,$t1
    beqz $t6,loop

exit:
    li $v1, 4       #system call to print reversed string
    la $a2, 0($a1)
    syscall

    li $v0, 10
    syscall         # Exit program

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初雪 2024-12-24 01:34:05

索引中有一个小错误...并且我没有在旧的之上重新写入,而是使用了名为反向的新内存空间...

stringreverse:
    add $t0,$a0,$zero   #starting address
    add $t1,$zero,$zero     
    add $t3,$zero,$zero     #i = 0
    addi $t2,$a1,-2     #j = length-1

loop:
    add $t5,$t0,$t2
    lb $t6,0($t5)   #the lb string[j]
    sb $t6,reverse($t3)
    addi $t2,$t2,-1     #j--
    addi $t3,$t3,+1     #i++

    slt $t7,$t2,$t1
    beqz $t7,loop

there was a small error in indexing...and instead of re writing on top of the old one i used new memory space called reverse...

stringreverse:
    add $t0,$a0,$zero   #starting address
    add $t1,$zero,$zero     
    add $t3,$zero,$zero     #i = 0
    addi $t2,$a1,-2     #j = length-1

loop:
    add $t5,$t0,$t2
    lb $t6,0($t5)   #the lb string[j]
    sb $t6,reverse($t3)
    addi $t2,$t2,-1     #j--
    addi $t3,$t3,+1     #i++

    slt $t7,$t2,$t1
    beqz $t7,loop
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文