In: Computer Science
How to write an assembly program that simply counts how many A’s, C’s, T’s, and G’s the input string contain.
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
.data
prompt: .asciiz "\nEnter a string: " #prompt for string
input: .space 1000 #string butter of 100 digit
.text
main:
li $v0, 4
la $a0, prompt #prompt for string
syscall
li $v0, 8
la $a0, input #ask for input
li $a1, 1000
syscall
move $s0, $a0 #move data to s0
li $t0,0 #count of C
li $t1,0 #count of T
li $t2,0 #count of A
li $t3,0 #count of G
loop: #loop while end of string
lb $a0, 0($s0) #load first character to a0
addi $s0, $s0, 1 #add index by one
beq $a0, $zero, done #else print character
sge $s5,$a0,'a' #check for lower case range
sle $s6,$a0,'z'
and $s5,$s5,$s6
beq $s5,1,lowercaseCount
j calculate
lowercaseCount:
sub $a0,$a0,32
calculate:
beq $a0,'C',cCount
beq $a0,'T',tCount
beq $a0,'A',aCount
beq $a0,'G',gCount
j exitCount
gCount:
add $t3,$t3,1 #c count ++
j exitCount
cCount:
add $t0,$t0,1 #c count ++
j exitCount
aCount:
add $t2,$t2,1 #c count ++
j exitCount
tCount:
add $t1,$t1,1 #c count ++
exitCount:
j loop
done:
li $a0,'C'
move $a1,$t0
jal printCount
li $a0,'T'
move $a1,$t1
jal printCount
li $a0,'A'
move $a1,$t2
jal printCount
li $a0,'G'
move $a1,$t3
jal printCount
li $v0,10
syscall
printCount:
move $v0,$a0 #print character
li $v0,11
syscall
li $a0,':' #print character
li $v0,11
syscall
move $a0,$a1 #print count
li $v0,1
syscall
li $a0,'\n' #print character
li $v0,11
syscall
jr $ra