Replace specific character of string using mips

六月ゝ 毕业季﹏ 提交于 2020-04-17 19:28:27

问题


I was trying to replace specific character of string using mips. The program is like the user needs to enter the string which is limited to 40 characters and the program needs to ask the user if they want to replace any character of the string or not. In my code I was able to print only one character, here is my code:

.data
prompt0: .asciiz " Please Enter any String :"
prompt:.asciiz "\n Your current string is: "
prompt1:.asciiz "\n Do you want to make any changes to the string? (y/n): " 
prompt2: .asciiz "\n Please Enter the Character that you want to Search for :"
prompt3: .asciiz "\n Please Enter the New Character :  "
prompt4: .asciiz "\n Your Final string is: "
 yes: .asciiz "y"
 No :.asciiz "n"
 Buffer: .space 40
.text
 li $v0, 4
 la $a0, prompt0
 syscall 

 li $v0,8        # take in input
 la $a0, Buffer      # load byte space into address
 li $a1, 40          # allot the byte space for string
 move $t0,$a0        # save string to t0
 syscall

li $v0,4
la $a0,prompt        # load and print "you wrote" string
syscall

la $a0, Buffer       # reload byte space to primary address
move $a0,$t0         # primary address = t0 address (load pointer)
li $v0,4         # print string
syscall



Loop:
    # Ask if user want to chnge rthe string or not
li $v0, 4     # syscall 4 (print_str)
la $a0, prompt1  # argument: string
syscall

     # GET INPUT FROM USER
li   $v0, 8   # get input
la   $a0, Buffer    # load byte space into address
li   $a1, 2         # allot the byte space for string
 move $t0,$a0        # save string to t0
syscall
#EDIT
lb $t1, yes  
lb $t0, 0($t0)   

#END EDIT

bne $t0, $t1, Exit

#IF YES, PRINT MESSAGE
li $v0,4
la $a0,prompt2
syscall


li $v0, 8   #get input
la $a0, Buffer  #load byte space into address
li $a1, 2      # allot the byte space for string
sw $t0,($a0)    #save string to t0
syscall

li $v0,4
la $a0,prompt3
syscall

li $v0, 8   #get input
la $a0, Buffer  #load byte space into address
li $a1, 2      # allot the byte space for string
move $t0,$a0    #save string to t0
syscall


la $a0,prompt     #load and print "you wrote" string
li $v0,4
syscall


la $a0, Buffer  #reload byte space to primary address
move $a0,$t0    # primary address = t0 address (load pointer)
li $v0,4        # print string
syscall
j Loop


 Exit:

li $v0,10       #end program
syscall 
     jr $ra     

Here is my C program that i want to convert to mips:

#include <stdio.h>
#include <string.h>

int main()
{
    char str[40], ch, Newch;
    int i;

    printf("\n Please Enter any String :  ");
    gets(str);

    printf("\n Please Enter the Character that you want to Search for :  ");
    scanf("%c", &ch);

    getchar();

    printf("\n Please Enter the New Character :  ");
    scanf("%c", &Newch);

    for(i = 0; i <= strlen(str); i++)
    {
        if(str[i] == ch)  
        {
            str[i] = Newch;
        }
    }

    printf("\n The Final String after Replacing All Occurrences of '%c' with '%c' = %s ", ch, Newch, str);

    return 0;
}

回答1:


A for loop is translated into assembly as follows:

for ( initializer; condition; increment ) {
    body
}

first, transform that into a while loop in C:

initializer;
while ( condition ) {
    body
    increment
}

next, translate the while into assembly as follows:

LoopTop:
    if ( ! condition ) goto ExitTheLoop;
    body
    increment
    goto LoopTop;
ExitTheLoop:

The print part is simple, just print each individual component each with its own syscall.  Your print statement will break down into printing a constant string, followed by a variable character, followed by a constant string, another variable character, a constant string, your translated string, and finally a single space or constant string holding a space: 7 individual syscalls in all.



来源:https://stackoverflow.com/questions/60713720/replace-specific-character-of-string-using-mips

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!