All pastes #262042 Raw Edit

Something

public text v1 · immutable
#262042 ·published 2006-11-30 02:50 UTC
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 "
ProcessNewLine:        .asciiz  "\n"

.text

processString:
        
        # Prologue
        subu  $sp, $sp, 184
        sw    $fp, 0($sp)
        sw    $ra, 4($sp)
        sw    $a0, 8($sp)
        addiu $fp, $sp, 24

        # print out the original string
        li    $v0, 4
        syscall

        # load $a0 into $t6 and $t7
        la    $t6, 0($sp)        # write text
        la    $t7, 0($a0)        # read text

        # $t0 = current character in $a0
        lb    $t0, 0($t7)

        # $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   $t0, $zero, ProcessStringEnd      # end processString when the nul character is found

        # check if character is greater than 0x41
        slt   $t5, $t0, $t1
        bne   $t5, $zero, ProcessStringLoopBottom

        # if character is less than 0x5a, then it is upper case
        slt   $t5, $t2, $t0
        beq   $t5, $zero, ProcessStringAddLetter

        # if character is less than 0x61, go to the bottom of the loop
        slt   $t5, $t0, $t3
        bne   $t5, $zero, ProcessStringLoopBottom

        # if character is less than 0x7a, then it is lower case
        slt   $t5, $t4, $t0
        beq   $t5, $zero, ProcessStringAddLetter

        # any other cases
        j ProcessStringLoopBottom

        # add a letter to $t6
ProcessStringAddLetter:
        sb    $t0, 0($t6)
        add   $t6, $t6, 1
        
ProcessStringLoopBottom:
        add   $t7, $t7, 1
        lb    $t0, 0($t7)

        j ProcessStringLoopBegin

ProcessStringEnd:

        add   $t0, $zero, $zero
        sb    $t0, 0($t6)

        # print new line
        la    $a0, ProcessNewLine
        li    $v0, 4
        syscall

        # print out the new string
        la    $a0, 0($sp)
        li    $v0, 4
        syscall

        la    $a0, ProcessNewLine
        li    $v0, 4
        syscall

        # print out countLowerText
        la    $a0, countLowerText
        li    $v0, 4
        syscall

        # call countLower
        add   $a0, $zero, $sp
        jal   countLower

        # print number from countLower
        add   $a0, $zero, $v0
        li    $v0, 1
        syscall

        la    $a0, ProcessNewLine
        li    $v0, 4
        syscall

        la    $a0, countUpperText
        li    $v0, 4
        syscall

        # call countUpper
        add   $a0, $zero, $sp
        jal   countUpper

        # print number from countUpper
        add   $a0, $zero, $v0
        li    $v0, 1
        syscall

        la    $a0, ProcessNewLine
        li    $v0, 4
        syscall

        la    $a0, ProcessNewLine
        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, 184      # restore the caller's stack pointer
        jr    $ra                # return to caller