##################################################################### # Program #1: Convert to Uppercase # 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 # Begin loop to look at each character in string # If character is null, go to End # If character's hex value is less than 'A', skip it # If character's hex value is less than 'Z', put in output string # If character's hex value is less than 'a', skip it # If character's hex value is greater than 'z', skip it # Convert character to uppercase # Go to Begin loop # End # Print string # Exit ###################################################################### # Register Usage: # $t0 - points to character in string being processed # $t1 - points to place where next character will be stored # $t2 - holds a character for processing # $t3 - holds hex value for character a # $t4 - holds hex value for character z # $t5 - holds hex value for character A # $t6 - holds hex value for character Z # #################### Data segment #################################### .data string_prompt: .asciiz "Please type a string: \n" char_prompt: .asciiz "Please type ONE character: \n" out_msg: .asciiz "The index of the character is: " fail_msg: .asciiz "The 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 < 1 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 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) addiu $sp, $sp, 4 jr $ra loop: lb $t4, 0($t0) beq $t4, $t3, match beqz $t4, no_result addi $t2, $t2, 1 addi $t0, $t0, 1 j loop match: 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