##################################################################### # Program #1: Search # Programmer: Chris Brakebill # Due Date: Nov. 8, 2009 Course: ECE 355 # Last Modified: Nov. 8, 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 index of the character, or an error if the # character 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 exit the loop # If it's the end of string, set index to -1 # Otherwise increase index and pointer by 1 # Return that index to main function # If the index is < 0, print error # Otherwise print message and index of character # Exit ###################################################################### # Register Usage: # $t0 - points to input string # $t1 - points to input character # $t2 - holds the index for the current char # $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 index of the character 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 search 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 bltz $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 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, $t2 # the index will be in $t2, 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, no_result # if it's NULL - go to no_result function addi $t2, $t2, 1 addi $t0, $t0, 1 # otherwise increment the pointer, the index, and loop again j loop match: # if it's a match, return to search function jr $ra no_result: addi $t2, $0, -1 # if we reach NULL then the end of the string has come and there's no jr $ra