Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- mainNumPhrases:
- .word 3
- mainPhrases:
- .word mainPhrase1
- .word mainPhrase2
- .word mainPhrase3
- mainPhrase1:
- .asciiz "A man, a plan, a canal, Panama!"
- .asciiz "bYe"
- mainPhrase2:
- .asciiz " CSc252 -- Computer Organization "
- mainPhrase3:
- .asciiz " Three Tabs To StarT This Line "
- mainOriginalStr:
- .asciiz "main: The original string:\n"
- mainFinalStr:
- .asciiz "main: The final string:\n"
- mainTotalStr:
- .asciiz "main: Total character count: "
- mainNewline:
- .asciiz "\n"
- .text
- main:
- # Function prologue -- even main has one
- subu $sp, $sp, 24 # allocate stack space
- sw $fp, 0($sp) # save frame pointer of caller
- sw $ra, 4($sp) # save return address
- addiu $fp, $sp, 20 # setup frame pointer of main
- la $t0, mainNumPhrases
- lw $s0, 0($t0) # $s0 = mainNumPhrases
- # skip everything if there are no phrases
- beq $s0, $zero, mainDone
- # for i = 0 to mainNumPhrases - 1
- # print phrase
- # call convert
- addi $s1, $zero, 0 # $s1 = i
- la $s2, mainPhrases # $s2 = mainPhrases[i]
- lw $s3, 0($s2) # $s3 = address of current phrase
- addi $s4, $zero, 0 # $s4 = character count = 0
- mainLoopBegin:
- # if i < mainNumPhrases
- slt $t3, $s1, $s0 # $t3 = i < mainNumPhrases
- beq $t3, $zero, mainLoopEnd
- # print the current phrase
- la $a0, mainOriginalStr
- addi $v0, $zero, 4
- syscall
- addi $a0, $s3, 0 # $a0 = address of current phrase
- addi $v0, $zero, 4
- syscall
- la $a0, mainNewline
- addi $v0, $zero, 4
- syscall
- # Startup Sequence:
- addi $a0, $s3, 0 # $a0 = address of current phrase
- jal convert
- # Clean up: get count of characters
- add $s4, $s4, $v0 # $s4 += count
- # print the modified string
- la $a0, mainFinalStr
- addi $v0, $zero, 4
- syscall
- addi $a0, $s3, 0
- addi $v0, $zero, 4
- syscall
- # print a newline to terminate line and a blank line
- la $a0, mainNewline
- addi $v0, $zero, 4
- syscall
- syscall
- addi $s1, $s1, 1 # i++
- addi $s2, $s2, 4 # $s2 = address of next phrase
- lw $s3, 0($s2) # $s3 = next phrase
- j mainLoopBegin
- mainLoopEnd:
- # print the total character count
- la $a0, mainTotalStr
- addi $v0, $zero, 4
- syscall
- addi $a0, $s4, 0
- addi $v0, $zero, 1
- syscall
- la $a0, mainNewline
- addi $v0, $zero, 4
- syscall
- mainDone:# Epilogue for main -- restore stack & frame ptrs & return
- lw $ra, 4($sp) # get return address from stack
- lw $fp, 0($sp) # restore frame pointer of caller
- addiu $sp, $sp, 24 # restore stack pointer of caller
- jr $ra # return to caller
- .data
- printLineNewline:
- .asciiz "\n"
- .text
- printLine:
- # Function prologue
- subu $sp, $sp, 24 # allocate stack space -- default of 24 here
- sw $ra, 4($sp) # save return address
- sw $fp, 0($sp) # save frame pointer of caller
- addiu $fp, $sp, 20 # setup frame ptr for printLine
- # for (i = $a0; i <= $a1; i++)
- # print character at location i
- add $t0, $zero, $a0 # i = $t0 = $a0
- printLineLoopBegin:
- # loop stops when i > $a1
- slt $t2, $a1, $t0 # $t2 = $a1 < i
- bne $t2, $zero, printLineLoopEnd
- lb $a0, 0($t0) # $a0 = character at array[i]
- addi $v0, $zero, 11
- syscall
- addi $t0, $t0, 1 # i++
- j printLineLoopBegin
- printLineLoopEnd:
- la $a0, printLineNewline
- addi $v0, $zero, 4
- syscall
- printLineDone:
- # Epilogue for printLine -- restore stack & frame pointers
- lw $ra, 4($sp) # get return address from stack
- lw $fp, 0($sp) # restore frame pointer of caller
- addiu $sp, $sp, 24 # restore stack pointer of caller
- jr $ra # return to caller
- # Your code goes below this line
- convert: # Function prologue
- addiu $sp, $sp, -24
- sw $fp, 0($sp)
- sw $ra, 4($sp)
- sw $a0, 8($sp) # Save the parameter
- addi $fp, $sp, 20
- add $t0, $a0, $zero # $t0 = the memory address of the first character in the string.
- addi $t4, $zero, 0 # $t4 = 0 (counts the number of characters in the string).
- lb $t5, ($t0) # $t1 = byte from memory address
- nextChar:
- lb $t1, ($t0) # $t1 = byte from memory address
- beqz $t1, strEnd #
- addi $t3, $0, 0x61
- sub $t2, $t1, $t3
- bgez $t2, l1
- add $t1, $t1, 32
- sb $t1, ($t0)
- addi $t4, $t4, 1 # $t4++
- add $a0, $t5, $zero
- add $a1, $t1, $zero
- jal printLine
- .data
- mainNumPhrases:
- .word 3
- mainPhrases:
- .word mainPhrase1
- .word mainPhrase2
- .word mainPhrase3
- mainPhrase1:
- .asciiz "A man, a plan, a canal, Panama!"
- .asciiz "bYe"
- mainPhrase2:
- .asciiz " CSc252 -- Computer Organization "
- mainPhrase3:
- .asciiz " Three Tabs To StarT This Line "
- mainOriginalStr:
- .asciiz "main: The original string:\n"
- mainFinalStr:
- .asciiz "main: The final string:\n"
- mainTotalStr:
- .asciiz "main: Total character count: "
- mainNewline:
- .asciiz "\n"
- .text
- main:
- # Function prologue -- even main has one
- subu $sp, $sp, 24 # allocate stack space
- sw $fp, 0($sp) # save frame pointer of caller
- sw $ra, 4($sp) # save return address
- addiu $fp, $sp, 20 # setup frame pointer of main
- la $t0, mainNumPhrases
- lw $s0, 0($t0) # $s0 = mainNumPhrases
- # skip everything if there are no phrases
- beq $s0, $zero, mainDone
- # for i = 0 to mainNumPhrases - 1
- # print phrase
- # call convert
- addi $s1, $zero, 0 # $s1 = i
- la $s2, mainPhrases # $s2 = mainPhrases[i]
- lw $s3, 0($s2) # $s3 = address of current phrase
- addi $s4, $zero, 0 # $s4 = character count = 0
- mainLoopBegin:
- # if i < mainNumPhrases
- slt $t3, $s1, $s0 # $t3 = i < mainNumPhrases
- beq $t3, $zero, mainLoopEnd
- # print the current phrase
- la $a0, mainOriginalStr
- addi $v0, $zero, 4
- syscall
- addi $a0, $s3, 0 # $a0 = address of current phrase
- addi $v0, $zero, 4
- syscall
- la $a0, mainNewline
- addi $v0, $zero, 4
- syscall
- # Startup Sequence:
- addi $a0, $s3, 0 # $a0 = address of current phrase
- jal convert
- # Clean up: get count of characters
- add $s4, $s4, $v0 # $s4 += count
- # print the modified string
- la $a0, mainFinalStr
- addi $v0, $zero, 4
- syscall
- addi $a0, $s3, 0
- addi $v0, $zero, 4
- syscall
- # print a newline to terminate line and a blank line
- la $a0, mainNewline
- addi $v0, $zero, 4
- syscall
- syscall
- addi $s1, $s1, 1 # i++
- addi $s2, $s2, 4 # $s2 = address of next phrase
- lw $s3, 0($s2) # $s3 = next phrase
- j mainLoopBegin
- mainLoopEnd:
- # print the total character count
- la $a0, mainTotalStr
- addi $v0, $zero, 4
- syscall
- addi $a0, $s4, 0
- addi $v0, $zero, 1
- syscall
- la $a0, mainNewline
- addi $v0, $zero, 4
- syscall
- mainDone:# Epilogue for main -- restore stack & frame ptrs & return
- lw $ra, 4($sp) # get return address from stack
- lw $fp, 0($sp) # restore frame pointer of caller
- addiu $sp, $sp, 24 # restore stack pointer of caller
- jr $ra # return to caller
- .data
- printLineNewline:
- .asciiz "\n"
- .text
- printLine:
- # Function prologue
- subu $sp, $sp, 24 # allocate stack space -- default of 24 here
- sw $ra, 4($sp) # save return address
- sw $fp, 0($sp) # save frame pointer of caller
- addiu $fp, $sp, 20 # setup frame ptr for printLine
- # for (i = $a0; i <= $a1; i++)
- # print character at location i
- add $t0, $zero, $a0 # i = $t0 = $a0
- printLineLoopBegin:
- # loop stops when i > $a1
- slt $t2, $a1, $t0 # $t2 = $a1 < i
- bne $t2, $zero, printLineLoopEnd
- lb $a0, 0($t0) # $a0 = character at array[i]
- addi $v0, $zero, 11
- syscall
- addi $t0, $t0, 1 # i++
- j printLineLoopBegin
- printLineLoopEnd:
- la $a0, printLineNewline
- addi $v0, $zero, 4
- syscall
- printLineDone:
- # Epilogue for printLine -- restore stack & frame pointers
- lw $ra, 4($sp) # get return address from stack
- lw $fp, 0($sp) # restore frame pointer of caller
- addiu $sp, $sp, 24 # restore stack pointer of caller
- jr $ra # return to caller
- # Your code goes below this line
- convert: # Function prologue
- addiu $sp, $sp, -24
- sw $fp, 0($sp)
- sw $ra, 4($sp)
- sw $a0, 8($sp) # Save the parameter
- addi $fp, $sp, 20
- add $t0, $a0, $zero # $t0 = the memory address of the first character in the string.
- addi $t4, $zero, 0 # $t4 = 0 (counts the number of characters in the string).
- lb $t5, ($t0) # $t1 = byte from memory address
- nextChar:
- lb $t1, ($t0) # $t1 = byte from memory address
- beqz $t1, strEnd #
- addi $t3, $0, 0x61
- sub $t2, $t1, $t3
- bgez $t2, l1
- add $t1, $t1, 32
- sb $t1, ($t0)
- addi $t4, $t4, 1 # $t4++
- add $a0, $t5, $zero
- add $a1, $t1, $zero
- jal printLine
- l1:
- addi $t9, $0, 9
- beq $t1, $t9, jumpIt
- addi $t9, $t9, 1
- beq $t1, $t9, jumpIt
- j gotoNext
- jumpIt:
- addi $t5, $t5, 1 nextChar
- gotoNext:
- add $t0, $t0, 1
- j nextChar
- strEnd:
- add $v0, $t4, $zero
- # Function epilogue -- 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, 20 # restore the caller's stack pointer
- jr $ra # return to caller's code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement