Untitled
mpasm
posted: Feb, 26th 2012 | jump to bottom
# Your code goes below this line charCounts: .word 0:26 # an array of 26 words, all initialized to 0 space: .asciiz ": " newline: .asciiz "\n" .text main: # Function prologue -- even main has one addiu $sp, $sp, -24 # allocate stack space -- default of 24 here sw $fp, 0($sp) # save caller's frame pointer sw $ra, 4($sp) # save return address addiu $fp, $sp, 20 # setup main's frame pointer la $t1, mainNumPhrases # load the addr of the num of phrases lw $t0, 0($t1) # $t1 = mainNumPhrases beq $t0, $zero, done # If the total count of phrases is 0, we are done. li $t5, 0 # $t5 = i = 0 li $t6, 'a' # $t6 = j = 'a' la $s6, charCounts # $s6 = addr of the array charcounts mainLoop: la $t2, mainPhrases # address of beginning of array sll $t3, $t5, 2 # $t5 = i * 4 (address offset) add $t2, $t2, $t3 # address of the array item we want lw $s0, 0($t2) # get the address of the string move $a0, $s0 # Point to the string addi $v0, $zero, 4 # syscall value for print_string syscall la $a0, newline # Point to the string addi $v0, $zero, 4 # syscall value for print_string syscall innerLoop: lb $s1, 0($s0) # load the first byte (character) of the string beq $s1, 0x0, printLoopStart # if this character is \0, move to the printing loop blt $s1, 'A', innerLoopIncrement # if this character is less than 'A', skip it and move on. blt $s1, '[', toLower # if this character is uppercase, change it to lower blt $s1, 'a', innerLoopIncrement # if this char is less than 'a', skip it and move on blt $s1, '{', innerLoopProcess # This is a valid character, process it. innerLoopIncrement: addi $s0, 1 # increment the address of the byte we are loading j innerLoop mainLoopIncrement: addi $t5, 1 # i++ slt $t4, $t5, $t0 # $t4 = i < mainNumPhrases bne $t4, $zero, mainLoop # if i<manNumPhrases, go back to the main loop, else quit. j done toLower: addi $s1, $s1, 32 # $s1 = toLower($s1) innerLoopProcess: # $s1 = current character li $s7, 'a' # $s7 = 'a' sub $s2, $s1, $s7 # $s2 = $s1 - $s7 (This gives us the offest base) sll $s2, $s2, 2 # $s2 = $s2 * 4 (multiply the offset by 4 to get the addr offset) add $s3, $s6, $s2 # $s3 = addr of array value (charCounts[char-'a']) lw $s4, 0($s3) # $s4 = current count of letter addi $s4, 1 # $s4++ sw $s4, 0($s3) # store the new value to memory j innerLoopIncrement printLoopStart: li $t6, 'a' # set $t6 = 'a' (our j) printLoop: bgt $t6, 'z', mainLoopIncrement # if $t6 > 'z', we are done printing, go to next phrase move $a0, $t6 # set $a0 = our character addi $v0, $zero, 11 # syscall value for print_character syscall # make it so! la $a0, space # set $a0 = ": " addi $v0, $zero, 4 # syscall value for print_string syscall # make it so! li $s7, 'a' # $s7 = 'a' sub $s2, $t6, $s7 # $s2 = $t6 - $s7 (This gives us the offest base) sll $s2, $s2, 2 # $s2 = $s2 * 4 (multiply the offset by 4 to get the addr offset) add $s3, $s6, $s2 # $s3 = addr of array value (charCounts[char-'a']) lw $a0, 0($s3) # load the count for that character into $a0 addi $v0, $zero, 1 # syscall value for print_int syscall # make it so! la $a0, newline # Point to the newline string addi $v0, $zero, 4 # syscall value for print_string syscall # make it so! addi $t6, $t6, 1 # $t6++ j printLoop done: # Epilogue for main -- restore stack & frame pointers and return lw $ra, 4($sp) # get return address from stack lw $fp, 0($sp) # restore the caller's frame pointer addiu $sp, $sp, 24 # restore the caller's stack pointer jr $ra # return to caller's code
9137 views




