############################################################################################### # MIPS example: Manipulating character strings # Function: Program to count upper case letters in a string stored in non-compacted form # i. e. 1 character in each consecutive 32-bit memory word # # # The ASCII code interval corresponding to the upper case letter is [65..90] (in decimal) # ############################################################################################### .data # add what follows to the data segment of the program str: .word 0x41 0x6C 0x6F 0x20 0x4D 0x61 0x6D 0x61 0x65 0x0 # Loads the string "Alo Mamae" in memory # case: .word 0x0 # Variable to contain the value of upper/lower case letters in string .text # Add what follows to the text segment of the program .globl main # Declare the label main to be a global one main: la $t0,str # register $t0 contains the address of the string lw $t1,0($t0) # get first character la $t3,case # get address of uppercase counter lw $t4,0($t3) # get value of counter. Initially, it should be zero loop: blez $t1,end # if char is 0, end of string sltiu $t2,$t1,65 # if char is smaller than letter "A" than it is not uppercase bne $t2,$zero,nxt_ch# if smaller than "A" get next char sltiu $t2,$t1,91 # if smaller than "Z"+1, than it is an uppercase letter beq $t2,$zero,nxt_ch# if bigger than "Z", get next char # addiu $t4,$t4,1 # if here, Bingo!, found one uppercase char. Increment case counter nxt_ch: addiu $t0,$t0,4 # increment char pointer lw $t1,0($t0) # get next char j loop # go back to test end: sw $t4,0($t3) # just store counter of uppercase letters in case # The program is finished. Exit. li $v0,10 # system call for exit syscall # Exit!