Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .data
  2.  
  3. mainNumPhrases:
  4.         .word  3
  5.  
  6. mainPhrases:
  7.         .word  mainPhrase1
  8.         .word  mainPhrase2
  9.         .word  mainPhrase3
  10.  
  11. mainPhrase1:
  12.         .asciiz "A man, a plan, a canal, Panama!"
  13.         .asciiz "bYe"
  14. mainPhrase2:
  15.         .asciiz "   CSc252 -- Computer Organization  "
  16. mainPhrase3:
  17.         .asciiz "            Three Tabs To StarT This Line    "
  18.  
  19. mainOriginalStr:
  20.         .asciiz "main: The original string:\n"
  21. mainFinalStr:
  22.         .asciiz "main: The final string:\n"
  23. mainTotalStr:
  24.         .asciiz "main: Total character count: "
  25.  
  26. mainNewline:
  27.         .asciiz "\n"
  28.  
  29. .text
  30. main:
  31.          # Function prologue -- even main has one
  32.          subu  $sp, $sp, 24       # allocate stack space
  33.          sw    $fp, 0($sp)        # save frame pointer of caller
  34.          sw    $ra, 4($sp)        # save return address
  35.          addiu $fp, $sp, 20       # setup frame pointer of main
  36.  
  37.          la    $t0, mainNumPhrases
  38.          lw    $s0, 0($t0)        # $s0 = mainNumPhrases
  39.  
  40.          # skip everything if there are no phrases
  41.          beq   $s0, $zero, mainDone
  42.  
  43.          # for i = 0 to mainNumPhrases - 1
  44.          #   print phrase
  45.          #   call convert
  46.          addi  $s1, $zero, 0      # $s1 = i
  47.          la    $s2, mainPhrases   # $s2 = mainPhrases[i]
  48.          lw    $s3, 0($s2)        # $s3 = address of current phrase
  49.          addi  $s4, $zero, 0      # $s4 = character count = 0
  50.  
  51. mainLoopBegin:
  52.          # if i < mainNumPhrases
  53.          slt   $t3, $s1, $s0      # $t3 = i < mainNumPhrases
  54.          beq   $t3, $zero, mainLoopEnd
  55.  
  56.          # print the current phrase
  57.          la    $a0, mainOriginalStr
  58.          addi  $v0, $zero, 4
  59.          syscall
  60.          addi  $a0, $s3, 0        # $a0 = address of current phrase
  61.          addi  $v0, $zero, 4
  62.          syscall
  63.          la    $a0, mainNewline
  64.          addi  $v0, $zero, 4
  65.          syscall
  66.        
  67.          # Startup Sequence:
  68.          addi  $a0, $s3, 0        # $a0 = address of current phrase
  69.          jal   convert
  70.          # Clean up: get count of characters
  71.          add   $s4, $s4, $v0      # $s4 += count
  72.        
  73.          # print the modified string
  74.          la    $a0, mainFinalStr
  75.          addi  $v0, $zero, 4
  76.          syscall
  77.          addi  $a0, $s3, 0
  78.          addi  $v0, $zero, 4
  79.          syscall
  80.        
  81.          # print a newline to terminate line and a blank line
  82.          la    $a0, mainNewline
  83.          addi  $v0, $zero, 4
  84.          syscall
  85.          syscall
  86.        
  87.          addi  $s1, $s1, 1        # i++
  88.          addi  $s2, $s2, 4        # $s2 = address of next phrase
  89.          lw    $s3, 0($s2)        # $s3 = next phrase
  90.          j     mainLoopBegin
  91.        
  92. mainLoopEnd:
  93.          # print the total character count
  94.          la    $a0, mainTotalStr
  95.          addi  $v0, $zero, 4
  96.          syscall
  97.          addi  $a0, $s4, 0
  98.          addi  $v0, $zero, 1
  99.          syscall
  100.          la    $a0, mainNewline
  101.          addi  $v0, $zero, 4
  102.          syscall
  103.  
  104. mainDone:# Epilogue for main -- restore stack & frame ptrs & return
  105.          lw    $ra, 4($sp)        # get return address from stack
  106.          lw    $fp, 0($sp)        # restore frame pointer of caller
  107.          addiu $sp, $sp, 24       # restore stack pointer of caller
  108.          jr    $ra                # return to caller
  109.  
  110. .data
  111. printLineNewline:
  112.          .asciiz "\n"
  113. .text
  114. printLine:
  115.          # Function prologue
  116.          subu  $sp, $sp, 24       # allocate stack space -- default of 24 here
  117.          sw    $ra, 4($sp)        # save return address
  118.          sw    $fp, 0($sp)        # save frame pointer of caller
  119.          addiu $fp, $sp, 20       # setup frame ptr for printLine
  120.  
  121.        
  122.          # for (i = $a0; i <= $a1; i++)
  123.          #    print character at location i
  124.          add   $t0, $zero, $a0    # i = $t0 = $a0
  125. printLineLoopBegin:
  126.          # loop stops when i > $a1
  127.          slt   $t2, $a1, $t0      # $t2 = $a1 < i
  128.          bne   $t2, $zero, printLineLoopEnd
  129.          lb    $a0, 0($t0)        # $a0 = character at array[i]
  130.          addi  $v0, $zero, 11
  131.          syscall
  132.          addi  $t0, $t0, 1        # i++
  133.          j     printLineLoopBegin
  134.  
  135. printLineLoopEnd:
  136.          la    $a0, printLineNewline
  137.          addi  $v0, $zero, 4
  138.          syscall
  139.  
  140. printLineDone:
  141.          # Epilogue for printLine -- restore stack & frame pointers
  142.          lw    $ra, 4($sp)        # get return address from stack
  143.          lw    $fp, 0($sp)        # restore frame pointer of caller
  144.          addiu $sp, $sp, 24       # restore stack pointer of caller
  145.          jr    $ra                # return to caller
  146.  
  147. # Your code goes below this line
  148.  
  149. .data
  150.  
  151. .text
  152. convert:            # Function prologue
  153.     addiu   $sp, $sp, -24
  154.     sw  $fp, 0($sp)
  155.     sw  $ra, 4($sp)
  156.     sw  $a0, 8($sp) # Save the parameter
  157.     addi    $fp, $sp, 20
  158.    
  159.     add $t0, $a0, $zero # $t0 = the memory address of the first character in the string.
  160.     addi    $t4, $zero, 0   # $t4 = 0 (counts the number of characters in the string).
  161.     lb  $t5, ($t0)  # $t5 = byte from memory address
  162.    
  163. nextChar:
  164.     lb  $t1, ($t0)  # $t1 = byte from memory address
  165.     beqz    $t1, strEnd     #
  166.     addi    $t3, $0, 0x61
  167.     sub     $t2, $t1, $t3
  168.     bgez    $t2, l1
  169.     add     $t1, $t1, 32
  170.     sb  $t1, ($t0)
  171.    
  172.     addi    $t4, $t4, 1 # $t4++
  173.    
  174.     # Startup Sequence
  175.     add $a0, $t5, $zero
  176.     add $a1, $t1, $zero
  177.     jal printLine
  178. l1:
  179.     addi    $t9, $0, 9
  180.     beq $t1, $t9, jumpIt
  181.     addi    $t9, $t9, 1
  182.     beq $t1, $t9, jumpIt
  183.     j   gotoNext
  184. jumpIt:
  185.     addi $t5, $t5, 1
  186. gotoNext:
  187.     add     $t0, $t0, 1
  188.     j   nextChar
  189.  
  190. strEnd:
  191.     add $v0, $t4, $zero
  192.    
  193. # Function epilogue -- restore stack & frame pointers and return
  194. lw $a0, 8($sp) # restore original value of $a0 for caller
  195. lw $ra, 4($sp) # get return address from stack
  196. lw $fp, 0($sp) # restore the caller's frame pointer
  197. addiu $sp, $sp, 24 # restore the caller's stack pointer
  198. jr $ra # return to caller's code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement