stalllucy
February 3rd, 2009, 02:10 PM
Hi everyone!
I am very new at Assembly and we are asked to do a few small program, I was able to do pretty much, but I am stuck now.
Project 1
-----------
Count a letter from a given sentence (letter is known, we simply execute the program)
EX: (letter e)
"The cat on the table"
e = 3
I am done with that, but I am stuck on Project 2
Project 2
-----------
Ask user a letter and then count from a given sentence
EX:
Enter a letter please
> a
"The cat on the table"
a = 2
I am stuck, I cannot turn project 1 into project 2, I am looking for help please!
Thanks
Project 1 codes:
##
## ALP: TP5 - count.asm [Indexed Addressing]
##
## Count the occurrences of a specific
## character (char) found in a string array "str".
## Indexing used to access each array element, in turn.
##
## t0 - holds each byte from string in turn
## t1 - index into array: k in str(k)
## t2 - count of occurences
## t3 - holds the character to search for
##
.text
.globl main
main: nop # initialise loop variables [no operation 'instruction']
li $t1,0 # $t1 will be the array index
li $t2,0 # $t2 will be the counter
lb $t3,char # $t3 holds the search-for character
#
loop:
lb $t0,str($t1) # fetch next character
# from location: str + ($t1) i.e. " str(k)"
# indexed addressing (= base + offset)
# (psuedo instruction; assembles as two actual instructions)
beqz $t0,strEnd # if it's a null, then exit loop
bne $t0,$t3,con # else not null; is it same as target char?
add $t2,$t2,1 # yes, so increment counter
con: add $t1,$t1,1 # and either way increment index: "k = k + 1"
j loop # then continue to work along string
#
strEnd: # string search all done so deliver output
la $a0,str # system call to print the string
li $v0,4
syscall
la $a0,txt1 # then print output message
syscall
la $a0,char # with target char in
syscall
la $a0,txt2 # then print output link message
syscall
move $a0,$t2 # system call to print [ = 'copy']
li $v0,1 # out the count worked out
syscall
la $a0,endl # system call to print
li $v0,4 # out a newline and identity sign-off
syscall
li $v0,10 # (usual quit)
syscall # au revoir...
###
.data
str: .asciiz "aybeeceedee*bach.beethoven.chopin/abracadabra$%!+" # {9 e's, 7 a's}
char: .asciiz "e" # can alter this (to "a", say) and run again
txt1: .asciiz "\n\n Count of "
txt2: .asciiz " in the above is "
endl: .asciiz "\n Cheerio from PJB \n" # (you!)
## end of file : TP5 - count.asm
I am very new at Assembly and we are asked to do a few small program, I was able to do pretty much, but I am stuck now.
Project 1
-----------
Count a letter from a given sentence (letter is known, we simply execute the program)
EX: (letter e)
"The cat on the table"
e = 3
I am done with that, but I am stuck on Project 2
Project 2
-----------
Ask user a letter and then count from a given sentence
EX:
Enter a letter please
> a
"The cat on the table"
a = 2
I am stuck, I cannot turn project 1 into project 2, I am looking for help please!
Thanks
Project 1 codes:
##
## ALP: TP5 - count.asm [Indexed Addressing]
##
## Count the occurrences of a specific
## character (char) found in a string array "str".
## Indexing used to access each array element, in turn.
##
## t0 - holds each byte from string in turn
## t1 - index into array: k in str(k)
## t2 - count of occurences
## t3 - holds the character to search for
##
.text
.globl main
main: nop # initialise loop variables [no operation 'instruction']
li $t1,0 # $t1 will be the array index
li $t2,0 # $t2 will be the counter
lb $t3,char # $t3 holds the search-for character
#
loop:
lb $t0,str($t1) # fetch next character
# from location: str + ($t1) i.e. " str(k)"
# indexed addressing (= base + offset)
# (psuedo instruction; assembles as two actual instructions)
beqz $t0,strEnd # if it's a null, then exit loop
bne $t0,$t3,con # else not null; is it same as target char?
add $t2,$t2,1 # yes, so increment counter
con: add $t1,$t1,1 # and either way increment index: "k = k + 1"
j loop # then continue to work along string
#
strEnd: # string search all done so deliver output
la $a0,str # system call to print the string
li $v0,4
syscall
la $a0,txt1 # then print output message
syscall
la $a0,char # with target char in
syscall
la $a0,txt2 # then print output link message
syscall
move $a0,$t2 # system call to print [ = 'copy']
li $v0,1 # out the count worked out
syscall
la $a0,endl # system call to print
li $v0,4 # out a newline and identity sign-off
syscall
li $v0,10 # (usual quit)
syscall # au revoir...
###
.data
str: .asciiz "aybeeceedee*bach.beethoven.chopin/abracadabra$%!+" # {9 e's, 7 a's}
char: .asciiz "e" # can alter this (to "a", say) and run again
txt1: .asciiz "\n\n Count of "
txt2: .asciiz " in the above is "
endl: .asciiz "\n Cheerio from PJB \n" # (you!)
## end of file : TP5 - count.asm