MIPS 字符串切片 |检测字符串中的给定单词

发布于 2025-01-20 01:59:12 字数 755 浏览 2 评论 0原文

我正在尝试制作一个函数,以检测字符串的一部分是否等于给定单词。

我想过要用嵌套的循环穿过字符串,并检测发现的子字符串是否等于给定的单词。但是,我不知道要用嵌套循环切片。我只能提出一个程序,该程序能够通过循环浏览字符串是否等于给定字符的字符。

这是我所做的:

    la $a0,string #load address of string
    lb $a1,orig   #load original character
    lb $a2,new    #load new character
    li $a3,0      #load replace with zero first
    li $t0,0      #load index
    loop:
    lb $t0,0($a0) #load character
    beqz $t0,exit #exit when loop finish

    bne $t0,$a1,skip #compare with original character
    sb $a2,0($a0)    #replace character
    add $a3,$a3,1    #increase replace count by one
    skip:
    add $a0,$a0,1    #increase count by one
    j loop
    exit:

    end:
    li $v0,4
    la $a0,rprompt
    syscall

我真的很感谢任何帮助!

I am trying to make a function that detects if a part of a string is equal to a given word.

I thought of going through the string with a nested loop and detecting if the substring found would be equal to the word given. However, I don't know of to slice a string with a nested loop. I am only able to come up with a program that is able to detect if a character of a string is equal to a given character by looping through it.

Here is what I made:

    la $a0,string #load address of string
    lb $a1,orig   #load original character
    lb $a2,new    #load new character
    li $a3,0      #load replace with zero first
    li $t0,0      #load index
    loop:
    lb $t0,0($a0) #load character
    beqz $t0,exit #exit when loop finish

    bne $t0,$a1,skip #compare with original character
    sb $a2,0($a0)    #replace character
    add $a3,$a3,1    #increase replace count by one
    skip:
    add $a0,$a0,1    #increase count by one
    j loop
    exit:

    end:
    li $v0,4
    la $a0,rprompt
    syscall

I would really appreciate any help!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文