|
| 1 | +.section .rodata |
| 2 | + |
| 3 | +i_know: .string "I know an old lady who swallowed a " |
| 4 | +stop: .string ".\n" |
| 5 | +she_swallowed: |
| 6 | + .string "She swallowed the " |
| 7 | +to_catch: |
| 8 | + .string " to catch the " |
| 9 | +that_wriggled: |
| 10 | + .string " that wriggled and jiggled and tickled inside her" |
| 11 | + |
| 12 | +animals: |
| 13 | + .string "flyspiderbirdcatdoggoatcowhorse" |
| 14 | + |
| 15 | +animals_table: |
| 16 | + .hword 0, 0, 3, 9, 13, 16, 19, 23, 26, 31, 31 |
| 17 | + |
| 18 | +exclamations: |
| 19 | + .string "I don't know why she swallowed the fly. Perhaps she'll die.\nIt wriggled and jiggled and tickled inside her.\nHow absurd to swallow a bird!\nImagine that, to swallow a cat!\nWhat a hog, to swallow a dog!\nJust opened her throat and swallowed a goat!\nI don't know how she swallowed a cow!\nShe's dead, of course!\n" |
| 20 | + |
| 21 | +exclamations_table: |
| 22 | + .hword 0, 0, 60, 108, 138, 170, 200, 245, 283, 306 |
| 23 | + |
| 24 | + |
| 25 | +.text |
| 26 | +.globl recite |
| 27 | + |
| 28 | + |
| 29 | +/* char *copy(char *dest, void *, void *, const char *source); */ |
| 30 | +copy: |
| 31 | + lb t0, 0(a3) |
| 32 | + addi a3, a3, 1 |
| 33 | + sb t0, 0(a0) |
| 34 | + addi a0, a0, 1 |
| 35 | + bnez t0, copy |
| 36 | + |
| 37 | + addi a0, a0, -1 |
| 38 | + jalr zero, 0(t6) /* return */ |
| 39 | + |
| 40 | + |
| 41 | +/* char *copy_n(char *dest, void *, void *, const char *source, size_t length); */ |
| 42 | +copy_n: |
| 43 | + beqz a4, .copy_n_return |
| 44 | + |
| 45 | + lb t0, 0(a3) |
| 46 | + addi a3, a3, 1 |
| 47 | + sb t0, 0(a0) |
| 48 | + addi a0, a0, 1 |
| 49 | + addi a4, a4, -1 |
| 50 | + j copy_n |
| 51 | + |
| 52 | +.copy_n_return: |
| 53 | + jalr zero, 0(t6) /* return */ |
| 54 | + |
| 55 | + |
| 56 | +/* extern void recite(char *buffer, int start_verse, int end_verse); */ |
| 57 | +recite: |
| 58 | + slli a1, a1, 1 /* multiply start_verse by 2 */ |
| 59 | + slli a2, a2, 1 /* multiply end_verse by 2 */ |
| 60 | + li a7, 2 /* fly */ |
| 61 | + li a6, 4 /* spider */ |
| 62 | + li a5, '\n' |
| 63 | + |
| 64 | +.stanza: |
| 65 | + la a3, i_know |
| 66 | + jal t6, copy /* subroutine call */ |
| 67 | + |
| 68 | + la a3, animals |
| 69 | + la t0, animals_table |
| 70 | + add t0, t0, a1 |
| 71 | + lhu t1, 0(t0) |
| 72 | + add a3, a3, t1 /* start of animal string */ |
| 73 | + addi t0, t0, 2 |
| 74 | + lhu t2, 0(t0) |
| 75 | + sub a4, t2, t1 /* length of animal string */ |
| 76 | + jal t6, copy_n /* subroutine call */ |
| 77 | + |
| 78 | + la a3, stop |
| 79 | + jal t6, copy /* subroutine call */ |
| 80 | + |
| 81 | + la a3, exclamations |
| 82 | + la t0, exclamations_table |
| 83 | + add t0, t0, a1 |
| 84 | + lhu t1, 0(t0) |
| 85 | + add a3, a3, t1 /* start of exclamation string */ |
| 86 | + addi t0, t0, 2 |
| 87 | + lhu t2, 0(t0) |
| 88 | + sub a4, t2, t1 /* length of exclamation string */ |
| 89 | + jal t6, copy_n /* subroutine call */ |
| 90 | + |
| 91 | + li t0, 2 |
| 92 | + beq a1, t0, .next /* first verse? */ |
| 93 | + |
| 94 | + li t0, 16 |
| 95 | + beq a1, t0, .next /* last verse? */ |
| 96 | + |
| 97 | + move t3, a1 /* current animal */ |
| 98 | + |
| 99 | +.explain: |
| 100 | + la a3, she_swallowed |
| 101 | + jal t6, copy /* subroutine call */ |
| 102 | + |
| 103 | + la a3, animals |
| 104 | + la t0, animals_table |
| 105 | + add t0, t0, t3 |
| 106 | + lhu t1, 0(t0) |
| 107 | + add a3, a3, t1 /* start of animal string */ |
| 108 | + addi t0, t0, 2 |
| 109 | + lhu t2, 0(t0) |
| 110 | + sub a4, t2, t1 /* length of animal string */ |
| 111 | + jal t6, copy_n /* subroutine call */ |
| 112 | + |
| 113 | + addi t3, t3, -2 /* previous animal */ |
| 114 | + |
| 115 | + la a3, to_catch |
| 116 | + jal t6, copy /* subroutine call */ |
| 117 | + |
| 118 | + la a3, animals |
| 119 | + la t0, animals_table |
| 120 | + add t0, t0, t3 |
| 121 | + lhu t1, 0(t0) |
| 122 | + add a3, a3, t1 /* start of animal string */ |
| 123 | + addi t0, t0, 2 |
| 124 | + lhu t2, 0(t0) |
| 125 | + sub a4, t2, t1 /* length of animal string */ |
| 126 | + jal t6, copy_n /* subroutine call */ |
| 127 | + |
| 128 | + bne t3, a6, .stop /* only spider wriggles */ |
| 129 | + |
| 130 | + la a3, that_wriggled |
| 131 | + jal t6, copy /* subroutine call */ |
| 132 | + |
| 133 | +.stop: |
| 134 | + la a3, stop |
| 135 | + jal t6, copy /* subroutine call */ |
| 136 | + |
| 137 | + bne t3, a7, .explain /* loop until fly */ |
| 138 | + |
| 139 | + la a3, exclamations |
| 140 | + la t0, exclamations_table |
| 141 | + add t0, t0, t3 |
| 142 | + lhu t1, 0(t0) |
| 143 | + add a3, a3, t1 /* start of exclamation string */ |
| 144 | + addi t0, t0, 2 |
| 145 | + lhu t2, 0(t0) |
| 146 | + sub a4, t2, t1 /* length of exclamation string */ |
| 147 | + jal t6, copy_n /* subroutine call */ |
| 148 | + |
| 149 | +.next: |
| 150 | + sb a5, 0(a0) /* newline */ |
| 151 | + addi a0, a0, 1 |
| 152 | + addi a1, a1, 2 |
| 153 | + ble a1, a2, .stanza |
| 154 | + |
| 155 | + sb zero, -1(a0) |
| 156 | + ret |
0 commit comments