Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
62
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. convert:            # Function prologue
  150.     addiu   $sp, $sp, -24
  151.     sw  $fp, 0($sp)
  152.     sw  $ra, 4($sp)
  153.     sw  $a0, 8($sp) # Save the parameter
  154.     addi    $fp, $sp, 20
  155.    
  156.     add $t0, $a0, $zero # $t0 = the memory address of the first character in the string.
  157.     addi    $t4, $zero, 0   # $t4 = 0 (counts the number of characters in the string).
  158.     lb  $t5, ($t0)  # $t1 = byte from memory address
  159.    
  160. nextChar:
  161.     lb  $t1, ($t0)  # $t1 = byte from memory address
  162.     beqz    $t1, strEnd     #
  163.     addi    $t3, $0, 0x61
  164.     sub     $t2, $t1, $t3
  165.     bgez    $t2, l1
  166.     add     $t1, $t1, 32
  167.     sb  $t1, ($t0)
  168.    
  169.     addi    $t4, $t4, 1 # $t4++
  170.    
  171.     add $a0, $t5, $zero
  172.     add $a1, $t1, $zero
  173.     jal printLine
  174. .data
  175.  
  176. mainNumPhrases:
  177.         .word  3
  178.  
  179. mainPhrases:
  180.         .word  mainPhrase1
  181.         .word  mainPhrase2
  182.         .word  mainPhrase3
  183.  
  184. mainPhrase1:
  185.         .asciiz "A man, a plan, a canal, Panama!"
  186.         .asciiz "bYe"
  187. mainPhrase2:
  188.         .asciiz "   CSc252 -- Computer Organization  "
  189. mainPhrase3:
  190.         .asciiz "            Three Tabs To StarT This Line    "
  191.  
  192. mainOriginalStr:
  193.         .asciiz "main: The original string:\n"
  194. mainFinalStr:
  195.         .asciiz "main: The final string:\n"
  196. mainTotalStr:
  197.         .asciiz "main: Total character count: "
  198.  
  199. mainNewline:
  200.         .asciiz "\n"
  201.  
  202. .text
  203. main:
  204.          # Function prologue -- even main has one
  205.          subu  $sp, $sp, 24       # allocate stack space
  206.          sw    $fp, 0($sp)        # save frame pointer of caller
  207.          sw    $ra, 4($sp)        # save return address
  208.          addiu $fp, $sp, 20       # setup frame pointer of main
  209.  
  210.          la    $t0, mainNumPhrases
  211.          lw    $s0, 0($t0)        # $s0 = mainNumPhrases
  212.  
  213.          # skip everything if there are no phrases
  214.          beq   $s0, $zero, mainDone
  215.  
  216.          # for i = 0 to mainNumPhrases - 1
  217.          #   print phrase
  218.          #   call convert
  219.          addi  $s1, $zero, 0      # $s1 = i
  220.          la    $s2, mainPhrases   # $s2 = mainPhrases[i]
  221.          lw    $s3, 0($s2)        # $s3 = address of current phrase
  222.          addi  $s4, $zero, 0      # $s4 = character count = 0
  223.  
  224. mainLoopBegin:
  225.          # if i < mainNumPhrases
  226.          slt   $t3, $s1, $s0      # $t3 = i < mainNumPhrases
  227.          beq   $t3, $zero, mainLoopEnd
  228.  
  229.          # print the current phrase
  230.          la    $a0, mainOriginalStr
  231.          addi  $v0, $zero, 4
  232.          syscall
  233.          addi  $a0, $s3, 0        # $a0 = address of current phrase
  234.          addi  $v0, $zero, 4
  235.          syscall
  236.          la    $a0, mainNewline
  237.          addi  $v0, $zero, 4
  238.          syscall
  239.        
  240.          # Startup Sequence:
  241.          addi  $a0, $s3, 0        # $a0 = address of current phrase
  242.          jal   convert
  243.          # Clean up: get count of characters
  244.          add   $s4, $s4, $v0      # $s4 += count
  245.        
  246.          # print the modified string
  247.          la    $a0, mainFinalStr
  248.          addi  $v0, $zero, 4
  249.          syscall
  250.          addi  $a0, $s3, 0
  251.          addi  $v0, $zero, 4
  252.          syscall
  253.        
  254.          # print a newline to terminate line and a blank line
  255.          la    $a0, mainNewline
  256.          addi  $v0, $zero, 4
  257.          syscall
  258.          syscall
  259.        
  260.          addi  $s1, $s1, 1        # i++
  261.          addi  $s2, $s2, 4        # $s2 = address of next phrase
  262.          lw    $s3, 0($s2)        # $s3 = next phrase
  263.          j     mainLoopBegin
  264.        
  265. mainLoopEnd:
  266.          # print the total character count
  267.          la    $a0, mainTotalStr
  268.          addi  $v0, $zero, 4
  269.          syscall
  270.          addi  $a0, $s4, 0
  271.          addi  $v0, $zero, 1
  272.          syscall
  273.          la    $a0, mainNewline
  274.          addi  $v0, $zero, 4
  275.          syscall
  276.  
  277. mainDone:# Epilogue for main -- restore stack & frame ptrs & return
  278.          lw    $ra, 4($sp)        # get return address from stack
  279.          lw    $fp, 0($sp)        # restore frame pointer of caller
  280.          addiu $sp, $sp, 24       # restore stack pointer of caller
  281.          jr    $ra                # return to caller
  282.  
  283. .data
  284. printLineNewline:
  285.          .asciiz "\n"
  286. .text
  287. printLine:
  288.          # Function prologue
  289.          subu  $sp, $sp, 24       # allocate stack space -- default of 24 here
  290.          sw    $ra, 4($sp)        # save return address
  291.          sw    $fp, 0($sp)        # save frame pointer of caller
  292.          addiu $fp, $sp, 20       # setup frame ptr for printLine
  293.  
  294.        
  295.          # for (i = $a0; i <= $a1; i++)
  296.          #    print character at location i
  297.          add   $t0, $zero, $a0    # i = $t0 = $a0
  298. printLineLoopBegin:
  299.          # loop stops when i > $a1
  300.          slt   $t2, $a1, $t0      # $t2 = $a1 < i
  301.          bne   $t2, $zero, printLineLoopEnd
  302.          lb    $a0, 0($t0)        # $a0 = character at array[i]
  303.          addi  $v0, $zero, 11
  304.          syscall
  305.          addi  $t0, $t0, 1        # i++
  306.          j     printLineLoopBegin
  307.  
  308. printLineLoopEnd:
  309.          la    $a0, printLineNewline
  310.          addi  $v0, $zero, 4
  311.          syscall
  312.  
  313. printLineDone:
  314.          # Epilogue for printLine -- restore stack & frame pointers
  315.          lw    $ra, 4($sp)        # get return address from stack
  316.          lw    $fp, 0($sp)        # restore frame pointer of caller
  317.          addiu $sp, $sp, 24       # restore stack pointer of caller
  318.          jr    $ra                # return to caller
  319.  
  320. # Your code goes below this line
  321.  
  322. convert:            # Function prologue
  323.     addiu   $sp, $sp, -24
  324.     sw  $fp, 0($sp)
  325.     sw  $ra, 4($sp)
  326.     sw  $a0, 8($sp) # Save the parameter
  327.     addi    $fp, $sp, 20
  328.    
  329.     add $t0, $a0, $zero # $t0 = the memory address of the first character in the string.
  330.     addi    $t4, $zero, 0   # $t4 = 0 (counts the number of characters in the string).
  331.     lb  $t5, ($t0)  # $t1 = byte from memory address
  332.    
  333. nextChar:
  334.     lb  $t1, ($t0)  # $t1 = byte from memory address
  335.     beqz    $t1, strEnd     #
  336.     addi    $t3, $0, 0x61
  337.     sub     $t2, $t1, $t3
  338.     bgez    $t2, l1
  339.     add     $t1, $t1, 32
  340.     sb  $t1, ($t0)
  341.    
  342.     addi    $t4, $t4, 1 # $t4++
  343.    
  344.     add $a0, $t5, $zero
  345.     add $a1, $t1, $zero
  346.     jal printLine
  347. l1:
  348.     addi    $t9, $0, 9
  349.     beq $t1, $t9, jumpIt
  350.     addi    $t9, $t9, 1
  351.     beq $t1, $t9, jumpIt
  352.     j   gotoNext
  353. jumpIt:
  354.     addi $t5, $t5, 1    nextChar
  355. gotoNext:
  356.     add     $t0, $t0, 1
  357.     j   nextChar
  358.  
  359. strEnd:
  360.     add $v0, $t4, $zero
  361.    
  362. # Function epilogue -- restore stack & frame pointers and return
  363. lw $ra, 4($sp) # get return address from stack
  364. lw $fp, 0($sp) # restore the caller's frame pointer
  365. addiu $sp, $sp, 20 # restore the caller's stack pointer
  366. jr $ra # return to caller's code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement