MIPS Assembly Language Program: String Manipulation and Output

Verified

Added on  2019/09/23

|5
|571
|26
Homework Assignment
AI Summary
This MIPS assembly language program is designed to manipulate strings based on specific character replacements. The program begins by prompting the user to input a string. It then iterates through the string, identifying and replacing specific characters with others: '#' is replaced with 'A', '%' with 'G', '$' with 'C', and '&' with 'T'. The program utilizes registers to store indices, characters, and the output string. The core logic involves conditional branching to check for these special characters. When a match is found, the character is replaced in the output string. Finally, the program outputs the modified string to the console after processing the entire input string. The solution demonstrates fundamental assembly language concepts, including string input/output, character comparison, conditional branching, and string manipulation techniques.
Document Page
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
.data
str: .space 40
fstr: .space 150
prompt: .asciiz "Input a string:"
output: .asciiz "Output string:"
newline: .asciiz "\n"
.text
.globl main
main:
# display prompt
li $v0, 4
la $a0, prompt
syscall
# accept input string
li $v0, 8
la $a0, str
li $a1, 64
syscall
li $t1, 0 # initiate index
li $t7, 0 # output index
loop:
li $t6 , 0
lb $t0 str($t1)
beqz $t0 exit
# check if "#"
li $t2, '#'
beq $t0, $t2, hash_to_a
nop
# check if "%"
li $t2, '%'
beq $t0, $t2, percent_to_g
nop
# check if "$"
li $t2, '$'
Document Page
beq $t0, $t2, doller_to_c
nop
# check if "&"
li $t2, '&'
beq $t0, $t2, and_to_t
nop
#add $s0,$t1,$t7
sb $t0,fstr($t7)
addi $t7,$t7,1
addi $t1,$t1,1
# if not a "a", store it at current index in string less "a"
count
j loop
nop
replace:
sub $t5,$a1,32
beq $t5,$zero,space
bge $t6,$t5,loop
sb $a2,fstr($t7)
addi $t6,$t6,1
addi $t7,$t7,1
j replace
space :
#li $a2, '$'
subi $t1,$t1,1
subi $t1,$t1,1
lb $a2, str($t1)
sb $a2,fstr($t7)
addi $t7,$t7,1
addi $t1,$t1,1
addi $t1,$t1,1
jal loop
hash_to_a:
Document Page
addi $t1,$t1,1
lb $a1 str($t1)
subi $t1,$t1,1
li $a2, 'A'
addi $t1,$t1,1
addi $t1,$t1,1
jal replace
percent_to_g:
addi $t1,$t1,1
lb $a1 str($t1)
subi $t1,$t1,1
li $a2, 'G'
addi $t1,$t1,1
addi $t1,$t1,1
jal replace
doller_to_c:
addi $t1,$t1,1
lb $a1 str($t1)
subi $t1,$t1,1
li $a2, 'C'
addi $t1,$t1,1
addi $t1,$t1,1
jal replace
and_to_t:
addi $t1,$t1,1
lb $a1 str($t1)
subi $t1,$t1,1
li $a2, 'T'
addi $t1,$t1,1
addi $t1,$t1,1
jal replace
exit:
li $v0, 4 # output a newline
la $a0, newline
syscall
li $v0, 4
la $a0, output
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
syscall
li $v0, 4
la $a0, fstr
syscall
li $v0, 10 # exit program
syscall
nop
chevron_up_icon
1 out of 5
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]