|
| 1 | +# | Register | Usage | Type | Description | |
| 2 | +# | -------- | ------------ | ------- | ---------------------------------------------------------- | |
| 3 | +# | `$a0` | input | address | null-terminated input string with newline after each input | |
| 4 | +# | `$a1` | input/output | address | null-terminated output string | |
| 5 | +# | `$a2` | temporary | address | null-terminated or newline-terminated source string | |
| 6 | +# | `$t0` | temporary | byte | character for output | |
| 7 | +# | `$t6` | temporary | address | current string | |
| 8 | +# | `$t7` | temporary | address | first string | |
| 9 | +# | `$t8` | temporary | byte | '\n' newline | |
| 10 | +# | `$t9` | temporary | address | return address | |
| 11 | + |
| 12 | +.globl recite |
| 13 | + |
| 14 | +.data |
| 15 | + |
| 16 | +for_want: .asciiz "For want of a " |
| 17 | +the: .asciiz " the " |
| 18 | +was_lost: .asciiz " was lost.\n" |
| 19 | +and_all: .asciiz "And all for the want of a " |
| 20 | +stop: .asciiz ".\n" |
| 21 | + |
| 22 | + |
| 23 | +.text |
| 24 | + |
| 25 | +recite: |
| 26 | + move $t7, $a0 # first string |
| 27 | + li $t8, '\n' |
| 28 | + move $t9, $ra # Save return address |
| 29 | + j next |
| 30 | + |
| 31 | +write_line: |
| 32 | + la $a2, for_want |
| 33 | + jal copy_string |
| 34 | + |
| 35 | + move $a2, $t6 |
| 36 | + jal copy_line |
| 37 | + |
| 38 | + la $a2, the |
| 39 | + jal copy_string |
| 40 | + |
| 41 | + move $a2, $a0 |
| 42 | + jal copy_line |
| 43 | + |
| 44 | + la $a2, was_lost |
| 45 | + jal copy_string |
| 46 | + |
| 47 | +next: |
| 48 | + move $t6, $a0 # current string |
| 49 | + |
| 50 | +scan: |
| 51 | + lb $t0, 0($a0) # read input byte |
| 52 | + addi $a0, $a0, 1 # increment input pointer |
| 53 | + bne $t0, $t8, scan # loop until newline |
| 54 | + |
| 55 | + lb $t0, 0($a0) # first byte after newline |
| 56 | + bnez $t0, write_line |
| 57 | + |
| 58 | +write_final_line: |
| 59 | + la $a2, and_all |
| 60 | + jal copy_string |
| 61 | + |
| 62 | + move $a2, $t7 # first string |
| 63 | + jal copy_line |
| 64 | + |
| 65 | + la $a2, stop |
| 66 | + jal copy_string |
| 67 | + |
| 68 | + jr $t9 |
| 69 | + |
| 70 | + |
| 71 | +copy_string: |
| 72 | + # copy string from source $a2 to destination $a1 |
| 73 | + lb $t0, 0($a2) # load source byte |
| 74 | + sb $t0, 0($a1) # write byte to destination |
| 75 | + addi $a2, $a2, 1 # increment souce pointer |
| 76 | + addi $a1, $a1, 1 # increment destination pointer |
| 77 | + bnez $t0, copy_string # repeat until we have reached null terminator |
| 78 | + |
| 79 | + subi $a1, $a1, 1 # decrement destination pointer, |
| 80 | + # ready to append other strings |
| 81 | + jr $ra |
| 82 | + |
| 83 | + |
| 84 | +copy_line: |
| 85 | + # copy line from source $a2 to destination $a1 |
| 86 | + # assume $t8 is '\n' |
| 87 | + lb $t0, 0($a2) # load source byte |
| 88 | + sb $t0, 0($a1) # write byte to destination |
| 89 | + addi $a2, $a2, 1 # increment souce pointer |
| 90 | + addi $a1, $a1, 1 # increment destination pointer |
| 91 | + bne $t0, $t8, copy_line # repeat until we have reached '\n' line terminator |
| 92 | + |
| 93 | + subi $a1, $a1, 1 # decrement destination pointer, |
| 94 | + # ready to append other strings |
| 95 | + jr $ra |
0 commit comments