|
| 1 | + org 32768 |
| 2 | +__START_PROGRAM: |
| 3 | + di |
| 4 | + push ix |
| 5 | + push iy |
| 6 | + exx |
| 7 | + push hl |
| 8 | + exx |
| 9 | + ld hl, 0 |
| 10 | + add hl, sp |
| 11 | + ld (__CALL_BACK__), hl |
| 12 | + ei |
| 13 | + ld hl, 0 |
| 14 | + ld (_i), hl |
| 15 | + jp __LABEL0 |
| 16 | +__LABEL3: |
| 17 | + ld hl, (_i) |
| 18 | + push hl |
| 19 | + ld hl, _a |
| 20 | + push hl |
| 21 | + call _func2 |
| 22 | + ld (_c), hl |
| 23 | +__LABEL4: |
| 24 | + ld hl, (_i) |
| 25 | + inc hl |
| 26 | + ld (_i), hl |
| 27 | +__LABEL0: |
| 28 | + ld hl, 3 |
| 29 | + ld de, (_i) |
| 30 | + or a |
| 31 | + sbc hl, de |
| 32 | + jp nc, __LABEL3 |
| 33 | +__LABEL2: |
| 34 | + ld hl, 0 |
| 35 | + ld b, h |
| 36 | + ld c, l |
| 37 | +__END_PROGRAM: |
| 38 | + di |
| 39 | + ld hl, (__CALL_BACK__) |
| 40 | + ld sp, hl |
| 41 | + exx |
| 42 | + pop hl |
| 43 | + exx |
| 44 | + pop iy |
| 45 | + pop ix |
| 46 | + ei |
| 47 | + ret |
| 48 | +__CALL_BACK__: |
| 49 | + DEFW 0 |
| 50 | +_func1: |
| 51 | + push ix |
| 52 | + ld ix, 0 |
| 53 | + add ix, sp |
| 54 | + ld l, (ix+6) |
| 55 | + ld h, (ix+7) |
| 56 | + push hl |
| 57 | + push ix |
| 58 | + pop hl |
| 59 | + ld de, 4 |
| 60 | + add hl, de |
| 61 | + call __ARRAY_PTR |
| 62 | + ld e, (hl) |
| 63 | + inc hl |
| 64 | + ld d, (hl) |
| 65 | + ex de, hl |
| 66 | +_func1__leave: |
| 67 | + ld sp, ix |
| 68 | + pop ix |
| 69 | + exx |
| 70 | + pop hl |
| 71 | + pop bc |
| 72 | + ex (sp), hl |
| 73 | + exx |
| 74 | + ret |
| 75 | +_func2: |
| 76 | + push ix |
| 77 | + ld ix, 0 |
| 78 | + add ix, sp |
| 79 | + ld l, (ix+6) |
| 80 | + ld h, (ix+7) |
| 81 | + push hl |
| 82 | + ld l, (ix+4) |
| 83 | + ld h, (ix+5) |
| 84 | + push hl |
| 85 | + call _func1 |
| 86 | +_func2__leave: |
| 87 | + ld sp, ix |
| 88 | + pop ix |
| 89 | + exx |
| 90 | + pop hl |
| 91 | + pop bc |
| 92 | + ex (sp), hl |
| 93 | + exx |
| 94 | + ret |
| 95 | +#line 1 "array.asm" |
| 96 | +; vim: ts=4:et:sw=4: |
| 97 | + ; Copyleft (K) by Jose M. Rodriguez de la Rosa |
| 98 | + ; (a.k.a. Boriel) |
| 99 | +; http://www.boriel.com |
| 100 | + ; ------------------------------------------------------------------- |
| 101 | + ; Simple array Index routine |
| 102 | + ; Number of total indexes dimensions - 1 at beginning of memory |
| 103 | + ; HL = Start of array memory (First two bytes contains N-1 dimensions) |
| 104 | + ; Dimension values on the stack, (top of the stack, highest dimension) |
| 105 | + ; E.g. A(2, 4) -> PUSH <4>; PUSH <2> |
| 106 | + ; For any array of N dimension A(aN-1, ..., a1, a0) |
| 107 | + ; and dimensions D[bN-1, ..., b1, b0], the offset is calculated as |
| 108 | + ; O = [a0 + b0 * (a1 + b1 * (a2 + ... bN-2(aN-1)))] |
| 109 | +; What I will do here is to calculate the following sequence: |
| 110 | + ; ((aN-1 * bN-2) + aN-2) * bN-3 + ... |
| 111 | +#line 1 "mul16.asm" |
| 112 | +__MUL16: ; Mutiplies HL with the last value stored into de stack |
| 113 | + ; Works for both signed and unsigned |
| 114 | + PROC |
| 115 | + LOCAL __MUL16LOOP |
| 116 | + LOCAL __MUL16NOADD |
| 117 | + ex de, hl |
| 118 | + pop hl ; Return address |
| 119 | + ex (sp), hl ; CALLEE caller convention |
| 120 | +__MUL16_FAST: |
| 121 | + ld b, 16 |
| 122 | + ld a, h |
| 123 | + ld c, l |
| 124 | + ld hl, 0 |
| 125 | +__MUL16LOOP: |
| 126 | + add hl, hl ; hl << 1 |
| 127 | + sla c |
| 128 | + rla ; a,c << 1 |
| 129 | + jp nc, __MUL16NOADD |
| 130 | + add hl, de |
| 131 | +__MUL16NOADD: |
| 132 | + djnz __MUL16LOOP |
| 133 | + ret ; Result in hl (16 lower bits) |
| 134 | + ENDP |
| 135 | +#line 20 "array.asm" |
| 136 | +#line 24 "/zxbasic/library-asm/array.asm" |
| 137 | +__ARRAY_PTR: ;; computes an array offset from a pointer |
| 138 | + ld c, (hl) |
| 139 | + inc hl |
| 140 | + ld h, (hl) |
| 141 | + ld l, c |
| 142 | +__ARRAY: |
| 143 | + PROC |
| 144 | + LOCAL LOOP |
| 145 | + LOCAL ARRAY_END |
| 146 | + LOCAL RET_ADDRESS ; Stores return address |
| 147 | + LOCAL TMP_ARR_PTR ; Stores pointer temporarily |
| 148 | + ld e, (hl) |
| 149 | + inc hl |
| 150 | + ld d, (hl) |
| 151 | + inc hl |
| 152 | + ld (TMP_ARR_PTR), hl |
| 153 | + ex de, hl |
| 154 | + ex (sp), hl ; Return address in HL, array address in the stack |
| 155 | + ld (RET_ADDRESS + 1), hl ; Stores it for later |
| 156 | + exx |
| 157 | + pop hl ; Will use H'L' as the pointer |
| 158 | + ld c, (hl) ; Loads Number of dimensions from (hl) |
| 159 | + inc hl |
| 160 | + ld b, (hl) |
| 161 | + inc hl ; Ready |
| 162 | + exx |
| 163 | + ld hl, 0 ; HL = Offset "accumulator" |
| 164 | +LOOP: |
| 165 | +#line 62 "/zxbasic/library-asm/array.asm" |
| 166 | + pop bc ; Get next index (Ai) from the stack |
| 167 | +#line 72 "/zxbasic/library-asm/array.asm" |
| 168 | + add hl, bc ; Adds current index |
| 169 | + exx ; Checks if B'C' = 0 |
| 170 | + ld a, b ; Which means we must exit (last element is not multiplied by anything) |
| 171 | + or c |
| 172 | + jr z, ARRAY_END ; if B'Ci == 0 we are done |
| 173 | + ld e, (hl) ; Loads next dimension into D'E' |
| 174 | + inc hl |
| 175 | + ld d, (hl) |
| 176 | + inc hl |
| 177 | + push de |
| 178 | + dec bc ; Decrements loop counter |
| 179 | + exx |
| 180 | + pop de ; DE = Max bound Number (i-th dimension) |
| 181 | + call __FNMUL |
| 182 | + jp LOOP |
| 183 | +ARRAY_END: |
| 184 | + ld a, (hl) |
| 185 | + exx |
| 186 | +#line 101 "/zxbasic/library-asm/array.asm" |
| 187 | + LOCAL ARRAY_SIZE_LOOP |
| 188 | + ex de, hl |
| 189 | + ld hl, 0 |
| 190 | + ld b, a |
| 191 | +ARRAY_SIZE_LOOP: |
| 192 | + add hl, de |
| 193 | + djnz ARRAY_SIZE_LOOP |
| 194 | +#line 111 "/zxbasic/library-asm/array.asm" |
| 195 | + ex de, hl |
| 196 | + ld hl, (TMP_ARR_PTR) |
| 197 | + ld a, (hl) |
| 198 | + inc hl |
| 199 | + ld h, (hl) |
| 200 | + ld l, a |
| 201 | + add hl, de ; Adds element start |
| 202 | +RET_ADDRESS: |
| 203 | + jp 0 |
| 204 | + ;; Performs a faster multiply for little 16bit numbs |
| 205 | + LOCAL __FNMUL, __FNMUL2 |
| 206 | +__FNMUL: |
| 207 | + xor a |
| 208 | + or h |
| 209 | + jp nz, __MUL16_FAST |
| 210 | + or l |
| 211 | + ret z |
| 212 | + cp 33 |
| 213 | + jp nc, __MUL16_FAST |
| 214 | + ld b, l |
| 215 | + ld l, h ; HL = 0 |
| 216 | +__FNMUL2: |
| 217 | + add hl, de |
| 218 | + djnz __FNMUL2 |
| 219 | + ret |
| 220 | +TMP_ARR_PTR: |
| 221 | + DW 0 ; temporary storage for pointer to tables |
| 222 | + ENDP |
| 223 | +#line 84 "pararray10.bas" |
| 224 | +ZXBASIC_USER_DATA: |
| 225 | +_i: |
| 226 | + DEFB 00, 00 |
| 227 | +_c: |
| 228 | + DEFB 00, 00 |
| 229 | +_a: |
| 230 | + DEFW __LABEL5 |
| 231 | +_a.__DATA__.__PTR__: |
| 232 | + DEFW _a.__DATA__ |
| 233 | +_a.__DATA__: |
| 234 | + DEFB 0Ah |
| 235 | + DEFB 00h |
| 236 | + DEFB 0Bh |
| 237 | + DEFB 00h |
| 238 | + DEFB 0Ch |
| 239 | + DEFB 00h |
| 240 | + DEFB 0Dh |
| 241 | + DEFB 00h |
| 242 | +__LABEL5: |
| 243 | + DEFW 0000h |
| 244 | + DEFB 02h |
| 245 | +; Defines DATA END --> HEAP size is 0 |
| 246 | +ZXBASIC_USER_DATA_END: |
| 247 | + ; Defines USER DATA Length in bytes |
| 248 | +ZXBASIC_USER_DATA_LEN EQU ZXBASIC_USER_DATA_END - ZXBASIC_USER_DATA |
| 249 | + END |
0 commit comments