rendered paste body.data
mainNumStrings:
.word 5
mainStrings:
.word mainStr1
.word mainStr2
.word mainStr3
.word mainStr4
.word mainStr5
mainStr1: .asciiz "One here With some CapitALS"
mainStr2: .asciiz "in various Places WithIn the line"
mainStr3: .asciiz "AND ONE THAT IS ALL CAPS"
mainStr4: .asciiz "and one that is all lower"
mainStr5: .asciiz " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
mainNewline:
.asciiz "\n"
.text
main:
# Function prologue -- even main has one
subu $sp, $sp, 24 # allocate stack space -- default of 24
sw $fp, 0($sp) # save caller's frame pointer
sw $ra, 4($sp) # save return address
addiu $fp, $sp, 24 # setup main's frame pointer
# for (i = 0; i < mainNumStrings; i++ )
# load address of string into $a0
# call processString
# $s0 = i
addi $s0, $zero, 0
# $s1 = mainNumStrings
la $t0, mainNumStrings
lw $s1, 0($t0)
# $s2 = address of mainStrings
la $s2, mainStrings
MainLoopBegin:
slt $t0, $s0, $s1 # $t0 = $s0 < $s1
beq $t0, $zero, MainLoopEnd
lw $a0, 0($s2) # $a0 = addr of a string
jal processString
la $a0, mainNewline
li $v0, 4
syscall
addi $s0, $s0, 1 # i++
addi $s2, $s2, 4 # $s2 += 4, to get addr of next string
j MainLoopBegin
MainLoopEnd:
MainDone:
# 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
countLower:
# Function prologue
subu $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
sw $a0, 8($sp) # preserve $a0
addiu $fp, $sp, 24 # setup countLower's frame pointer
# count = 0
# for (i = 0; buf[i] != '\0'; i++)
# if ( buf[i] >= 'a' && buf[i] <= 'z' )
# count++
addi $t0, $zero, 0 # $t0 = i = 0
addi $t1, $zero, 0 # $t1 = count = 0
addi $t2, $zero, 0x61 # $t2 = 'a'
addi $t3, $zero, 0x7a # $t3 = 'z'
countLowerLoopBegin:
lb $t4, 0($a0) # $t4 = buf[i]
beq $t4, $zero, countLowerLoopEnd
# is the character < 'a'?
slt $t6, $t4, $t2 # $t6 = $t4 < $t2, $t6 = buf[i] < 'a'
bne $t6, $zero, countLowerLoopBottom
# is the character <= 'z'?
addi $t5, $t3, 1 # want 'z' + 1 for comparison
slt $t6, $t4, $t5 # $t6 = $t4 < $t5, $t6 = buf[i] <= 'z'
beq $t6, $zero, countLowerLoopBottom
addi $t1, $t1, 1 # count++
countLowerLoopBottom:
addi $t0, $t0, 1 # i++
addi $a0, $a0, 1
j countLowerLoopBegin
countLowerLoopEnd:
addi $v0, $t1, 0 # $v0 = count
# Epilogue for countLower -- restore stack & frame pointers
lw $a0, 8($sp) # restore $a0
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
# Your code goes below this line
countUpper:
# Prologue
subu $sp, $sp, 24
sw $fp, 0($sp)
sw $ra, 4($sp)
sw $a0, 8($sp)
addiu $fp, $sp, 24
# set up $t0 as counter, $t1 as upperCaseCounter,and $t2 and $t3 as A and Z
add $t0, $zero, $zero # counter
add $t1, $zero, $zero # upperCaseCounter
add $t2, $zero, 0x41 # 'A'
add $t3, $zero, 0x5a # 'Z'
countUpperLoopBegin:
lb $t4, 0($a0) # load byte into $t4
beq $t4, $zero, countUpperLoopEnd
# is the character < 'A'?
slt $t6, $t4, $t2 # $t6 = $t4 < $t2, $t6 = buf[i] < 'A'
bne $t6, $zero, countUpperLoopBottom
# is the character <= 'Z'?
addi $t5, $t3, 1 # want 'Z' + 1 for comparison
slt $t6, $t4, $t5 # $t6 = $t4 < $t5, $t6 = buf[i] <= 'Z'
beq $t6, $zero, countUpperLoopBottom
addi $t1, $t1, 1 # count++
countUpperLoopBottom:
addi $t0, $t0, 1 # i++
addi $a0, $a0, 1
j countUpperLoopBegin
countUpperLoopEnd:
addi $v0, $t1, 0 # $v0 = count
# Epilogue for countUpper -- restore stack & frame pointers
lw $a0, 8($sp) # restore $a0
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
.data
countLowerText: .asciiz "Count of lower-case letters is "
countUpperText: .asciiz "Count of upper-case letters is "
.text
processString:
# Prologue
subu $sp, $sp, 24
sw $fp, 0($sp)
sw $ra, 4($sp)
sw $a0, 8($sp)
addiu $fp, $sp, 24
# load $a0 into $s3 and $s4
la $s3, 0($a0) # read the text
la $s4, 0($a0) # write text
# $s3 = current character in $a0
lb $s3, 0($a0)
# $t1 = 'A', $t2 = 'Z', $t3 = 'a', $t4 = 'z'
add $t1, $zero, 0x41
add $t2, $zero, 0x5a
add $t3, $zero, 0x61
add $t4, $zero, 0x7a
# loop through the text until $t0 = 0
ProcessStringLoopBegin:
beq $s3, $zero, ProcessStringEnd # end processString when the nul character is found
# check if character is greater than 0x41
slt $t5, $s3, $t1
bne $t5, $zero, ProcessStringLoopBottom
# check if char is less than 5a. If less, go to addChar
slt $t5, $s3, $t2
bne $t5, $zero, ProcessStringAddChar
# if char is less than 0x61, loop to bottom
slt $t5, $s3, $t3
bne $t5, $zero, ProcessStringLoopBottom
# if character is greater than 0x7a, loop to bottom
slt $t5, $s3, $t4
bne $t5, $zero, ProcessStringLoopBottom
ProcessStringAddChar:
# add the byte to $s4
sb $s4, 0($s3)
add $s4, $s4, 4
ProcessStringLoopBottom:
# move to the next byte in $a0
addi $a0, 1
# set $t0 to the next byte
lb $t0, 0($a0)
j ProcessStringLoopBegin
ProcessStringEnd:
# print out $t6
la $a0, 0($t6)
li $v0, 4
syscall
la $a0, mainNewline
li $v0, 4
syscall
# load $t6 into $a0 for the countLower and countUpper subroutines
la $a0, countLowerText
li $v0, 4
syscall
lw $a0, 0($t6)
jal countLower
la $a0, 0($v0)
li $v0, 1
syscall
la $a0, mainNewline
li $v0, 4
syscall
# load $t6 into $a0 for countUpper
la $a0, countUpperText
li $v0, 4
syscall
lw $a0, 0($t6)
jal countUpper
la $a0, 0($v0)
li $v0, 1
syscall
la $a0, mainNewline
li $v0, 4
syscall
# epilogue
lw $a0, 8($sp) # restore $a0
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