Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I use assembly language to substitute one character from another in a st

ID: 3671528 • Letter: H

Question

How can I use assembly language to substitute one character from another in a string?

EXAMPLE:

Enter string: sassy
Enter character you want to replace: s
Enter replacement character: p
The string with replacements: pappy
Number of replacements: 3

Here is my code so far (in assembly language):

#
# substitute.s - substitute one character for another in a string
#
.data
string: .space 80
orig: .space 1
new: .space 1
sprompt: .asciiz "Enter string:"
oprompt: .asciiz "Enter character you want to replace:"
nprompt: .asciiz "Enter replacement character:"
rprompt: .asciiz "The string with replacements: "
cprompt: .asciiz "Number of replacements: "

.text
.globl main
main:
# get string
la $a0,sprompt
la $a1,string
li $a2,80
li $v0,54
syscall
# get original character
# since there is no 'inputdialogchar' syscall, use an inputdialogstring
# syscall. This will read a string but we will just use the first character
la $a0,oprompt
la $a1,orig
li $a2,4
li $v0,54
syscall
la $a0,nprompt
la $a1,new
li $a2,4
li $v0,54
syscall
#
# now we are ready to do the real work of substituting every instance of
# 'orig' with 'new' in 'string'
# HINT: before you start, initialize the following registers:
# a0 = address of the string
# a1 = char to look for
# a2 = char to replace with
# a3 = count of replacements (initialize to zero)
# Have fun!
#
#
# int i,count=0;
# for (i=0;string[i]!=0;i++)
# if (string[i] == orig) {
# string[i]=new;
# count++;
# }
#
# INSERT YOUR CODE HERE. Make sure the number of replacements
# gets stored in $a3.
#
  


# this code will output the string. now, it will be the string entered.
# once you've added your code, you should see the string with replacements
li $v0,4
la $a0,rprompt
syscall
  
li $v0, 4   
la $a0,string
syscall
  
# this code will output the count of replacements,
# which must be stored in $a3
li $v0, 4
la $a0,cprompt
syscall
  
li $v0, 1   
move $a0, $a3
syscall
  

Explanation / Answer

You can assign a string variable to all or part of a line of assembly language code. A string variable can contain numeric and logical variables.

Substitution occurs in the same way as anywhere else.Substitution does not occur within vertical bars, except that vertical bars within double quotes do not affect substitution.Below i included an example for your reference,

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote