AppleIIAsm-Collection/disks/disk3_arrays/T.ADIM81.SUB
nathanriggs ad58479739 Disk 3: ARRAYS 0.3.0 updates
- major bugfixes, especially in 2D arrays
- separated each subroutine into own file
- reconfigured arrays to use 1-based indexing
- Arrays.demo uses macros now
- added overflow error handling to get and put routines
2019-01-20 19:14:17 -05:00

198 lines
4.6 KiB
Plaintext

*
*``````````````````````````````*
* ADIM81D : INIT 1D ARRAY *
*- -*
* CREATES AN ARRAY OF LENGTH *
* [:ASIZE] WITH ELEMENTS OF *
* LENGTH [:ESIZE] AT MEMORY *
* LOCATION POINTED TO BY *
* [:ALOC]. RETURNS ARRAY START *
* LOCATION IN .A *
*- -*
* FIRST BYTE OF MEMORY USED *
* HOLDS LENGTH OF ARRAY; *
* SECOND BYTE HOLDS LENGTH OF *
* ELEMENTS. *
*- -*
* CLOBBERS: *
* *
* FLAGS: ????---- REG: AXYM *
*- -*
* CYCLES: ??? *
* SIZE: *
*- -*
* USAGE: *
* *
* LDA #>$300 ; ARRAYLOC *
* PHA *
* LDA #<$300 *
* PHA *
* LDA #10 ; ARRAY SIZE *
* PHA *
* LDA #2 ; ELEMENT SIZE *
* PHA *
* LDA #$AA ; FILL VALUE *
* PHA *
* JSR DIM81 *
*- -*
* ENTRY *
* *
* TOP OF STACK *
* *
* LOW BYTE OF RETURN ADDRESS *
* HI BYTE OF RETURN ADDRESS *
* FILL VALUE *
* ELEMENT SIZE *
* ARRAY SIZE *
* LOW BYTE OF ARRAY ADDRESS *
* HIGH BYTE OF ARRAY ADDRESS *
*- -*
* EXIT *
* *
* TOP OF STACK *
* *
* LOW BYTE OF RETURN ADDRESS *
* HI BYTE OF RETURN ADDRESS *
* *
* .Y = LO BYTE OF ARRAY ADDR *
* .X = HI BYTE OF ARRAY ADDR *
* .A = ARRAY SIZE *
* *
* [RETURN] = TOTAL SIZE OF *
* ARRAY IN BYTES (WORD) *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
ADIM81
*
** SAVE RETURN ADDRESS
*
PLA
STA RETADR
PLA
STA RETADR+1
*
** GET PARAMETERS
*
PLA
STA :FILL ; FILL VALUE
PLA
STA :ESIZE ; ELEMENT SIZE
STA :ESZBAK
PLA
STA :ASIZE ; ARRAY SIZE
STA :ASZBAK
PLA
STA ADDR1 ; ARRAY ADDRESS
PLA
STA ADDR1+1
LDY #0
LDA #0
STY SCRATCH
BEQ :ENTLP
*
** MULTIPLY [:ASIZE] AND [:ESIZE] TO GET
** TOTAL SIZE OF MEMORY TO CLEAR AND USE
*
:DOADD
CLC
ADC :ASIZE
TAX
*
TYA
ADC SCRATCH
TAY
TXA
*
:LP
ASL :ASIZE
ROL SCRATCH
:ENTLP
LSR :ESIZE
BCS :DOADD
BNE :LP
*
CLC
INX
INX ; INCREASE BY TWO
; TO MAKE ROOM FOR SIZES
BCC :NOINY ; IF .X ROLLS OVER, INC .Y
INY
:NOINY
STX :MSIZE ; LOW BYTE
STY :MSIZE+1 ; HIGH BYTE
*
** NOW CLEAR MEMORY BLOCKS
*
** FILL WHOLE PAGES FIRST
*
LDA :FILL ; FILL VALUE
LDX :MSIZE+1
BEQ :PART ; IF NO WHOLE PAGES, JUST PART
LDY #0
:FULL
STA (ADDR1),Y
INY ; NEXT BYTE
BNE :FULL ; LOOP UNTIL PAGE DONE
INC ADDR1+1 ; GO TO NEXT PAGE
DEX
BNE :FULL ; LOOP IF PAGES LEFT
*
** NOW DO REMAINING PARTIAL PAGES
*
:PART
LDX :MSIZE ; PARTIAL PAGE BYTES
BEQ :MFEXIT ; EXIT IF = 0
LDY #0
:PARTLP
STA (ADDR1),Y ; STORE VAL
INY ; INC INDEX
DEX ; DEC COUNTER
BNE :PARTLP ; LOOP UNTIL DONE
:MFEXIT
*
** MEMORY FILL IS DONE
*
** THEN, SET SIZES
*
LDY #0
LDA :ASZBAK
STA (ADDR1),Y
INY
LDA :ESZBAK
STA (ADDR1),Y
*
** RESTORE RETURN ADDRESS
*
LDA RETADR+1
PHA
LDA RETADR
PHA
*
** RETURN POINTER TO STARTING ADDR OF ARRAY
*
LDY ADDR1 ; LOW
LDX ADDR1+1 ; HIGH
LDA :ASZBAK
*
** STORE TOTAL ARRAY SIZE IN BYTES
** IN MEMORY ALLOCATED FOR RETURN VALUES
*
LDA :MSIZE
STA RETURN
LDA :MSIZE+1
STA RETURN+1
LDA #2
STA RETLEN ; 2 BYTE LENGTH
*
RTS
*
** DATA
*
:MSIZE DS 2
:ASIZE DS 1
:ESIZE DS 1
:ASZBAK DS 1
:ESZBAK DS 1
:FILL DS 1
*