##################################################################### # Program #4: Count # Programmer: Chris Brakebill # Due Date: Nov. 16, 2009 Course: ECE 355 # Last Modified: Nov. 16, 2009 # ##################################################################### # Functional Description: # Search function searches a null terminated string for the # specified character # Input: Requests a string and a character to search for in the # string. # Output: Outputs the the number of times the character is in the string # or an error message if it is not in the string # ##################################################################### # Pseudocode: # Prompt for input string of maximum 255 characters # Read input as string # Prompt for input character to search for # Read input # Initialize two pointers to first character and input character # Load the ASCII value of input char into a register # Cycle through each char in the input string comparing to char # If Equal return a pointer to the character # If it's the end of string, return a pointer to the NULL character # Otherwise increase index and pointer by 1 # Return that index to count function # If we're not at the end of the string increment string pointer # by 1 and jump back to the search loop # If the count is = 0, print error # Otherwise print message and count of character # Exit ###################################################################### # Register Usage: # $t0 - points to input string # $t1 - points to input character # $t2 - holds the count for the character # $t3 - contains the ASCII value of input char # $t4 - contains the ASCII value of next char in string # $t6 - contains the index value, retrieved from return of search # #################### Data segment #################################### .data string_prompt: .asciiz "Please type a string: \n" char_prompt: .asciiz "Please type ONE character: \n" out_msg: .asciiz "\nThe number of times the character appears in the string is: " fail_msg: .asciiz "\nThe character was not found in the string.\n" in_string: .space 256 character: .space 1 ################### Code segment ##################### .text .globl main main: la $a0,string_prompt # prompt user for input string li $v0,4 syscall la $a0,in_string # read user input string li $a1,256 li $v0,8 syscall la $a0,char_prompt # prompt user for input character li $v0,4 syscall la $a0,character # read user input character li $a1,2 li $v0,8 syscall la $a0,in_string # Store pointers to string and character la $a1,character # in a registers to pass to the search function addiu $sp, $sp, -4 # Allocate space on the stack and store the return sw $ra, 0($sp) # address before calling the search function jal count lw $ra, 0($sp) # restore the return address off of the stack addiu $sp, $sp, 4 move $t6, $v0 # move the returned index value into a t register beqz $t6, fail # if the index is < 0 then it failed to find the char la $a0,out_msg # write output message li $v0,4 syscall move $a0, $t6 # output the index of the char li $v0, 1 syscall li $v0, 10 # exit the program syscall fail: la $a0,fail_msg # write output message telling user the string wasn't found li $v0,4 syscall li $v0, 10 # exit syscall # Count function takes a pointer count: addiu $sp, $sp, -4 # Make space on stack and store return address sw $ra, 0($sp) jal search lw $ra, 0($sp) # Get return address of the stack and clear space addiu $sp, $sp, 4 # on the stack lb $t4, 0($v0) # Load the return value into $t4 beqz $t4, end_count # If it's null, we've reached the end of the string addiu $t0, $t0, 1 # Increment the pointer and put it into $a0 for calling move $a0, $t0 # Move the incremented address into $a0 addiu $t2, $t2, 1 # Increment the count j count # Jump back to count end_count: move $v0, $t2 # Move the count into $v0 to return it jr $ra # jump back to $ra # Search function takes a pointer to string and character as arguments in # $a0 and $a1 registers and returns the address of the first occurence of that # character, or of the terminating null character if it doesn't exist search: move $t0, $a0 # Pointer to the string move $t1, $a1 # Pointer to the character lb $t3, 0($t1) # Put the specific character in a register addiu $sp, $sp, -4 # Allocate space on the stack for return address sw $ra, 0($sp) jal loop # Jump to the loop and let it cycle through searching move $v0, $t0 # the address will be in $t0, set it as a return value lw $ra, 0($sp) # Restore the return address from the stack and jump addiu $sp, $sp, 4 # back to main function jr $ra loop: lb $t4, 0($t0) # load the next character from the input string beq $t4, $t3, match # if it's the input character - go to match function beqz $t4, match # if it's NULL - go to match function addi $t0, $t0, 1 # otherwise increment the pointer and loop again j loop match: # if it's a match, return to search function jr $ra