使用 mips 替换字符串的特定字符
Replace specific character of string using mips
我正在尝试使用 mips 替换字符串的特定字符。该程序就像用户需要输入限制为 40 个字符的字符串,并且程序需要询问用户是否要替换字符串中的任何字符。在我的代码中,我只能打印一个字符,这是我的代码:
.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
这是我要转换为 mips 的 C 程序:
#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;
}
一个for循环翻译成汇编如下:
for ( initializer; condition; increment ) {
body
}
首先,将其转换为 C:
中的 while 循环
initializer;
while ( condition ) {
body
increment
}
接下来,将while翻译成汇编如下:
LoopTop:
if ( ! condition ) goto ExitTheLoop;
body
increment
goto LoopTop;
ExitTheLoop:
打印部分很简单,只需打印每个单独的组件,每个组件都有自己的系统调用。您的 print 语句将分解为打印一个常量字符串,后跟一个可变字符,然后是一个常量字符串,另一个可变字符,一个常量字符串,您翻译的字符串,最后是一个 space 或常量字符串,其中包含一个space:总共 7 个单独的系统调用。
我正在尝试使用 mips 替换字符串的特定字符。该程序就像用户需要输入限制为 40 个字符的字符串,并且程序需要询问用户是否要替换字符串中的任何字符。在我的代码中,我只能打印一个字符,这是我的代码:
.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
这是我要转换为 mips 的 C 程序:
#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;
}
一个for循环翻译成汇编如下:
for ( initializer; condition; increment ) {
body
}
首先,将其转换为 C:
中的 while 循环initializer;
while ( condition ) {
body
increment
}
接下来,将while翻译成汇编如下:
LoopTop:
if ( ! condition ) goto ExitTheLoop;
body
increment
goto LoopTop;
ExitTheLoop:
打印部分很简单,只需打印每个单独的组件,每个组件都有自己的系统调用。您的 print 语句将分解为打印一个常量字符串,后跟一个可变字符,然后是一个常量字符串,另一个可变字符,一个常量字符串,您翻译的字符串,最后是一个 space 或常量字符串,其中包含一个space:总共 7 个单独的系统调用。