arrayfree.asm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ; This routine is in charge of freeing an array of strings from memory
  2. ; HL = Pointer to start of array in memory
  3. ; Top of the stack = Number of elements of the array
  4. ;
  5. #include once <free.asm>
  6. ;
  7. __ARRAY_FREE:
  8. ;- PROC
  9. ;- LOCAL __ARRAY_LOOP
  10. lda z80_e ;- ex_de_hl
  11. ldx z80_l
  12. stx z80_e
  13. sta z80_l
  14. lda z80_d
  15. ldx z80_h
  16. stx z80_d
  17. sta z80_h
  18. pla ;- pop hl ; (ret address)
  19. sta z80_h
  20. pla
  21. sta z80_l
  22. tsx ;- ex (sp), hl ; Callee -> HL = Number of elements
  23. lda $0103,x
  24. ldy z80_h
  25. sta z80_h
  26. tya
  27. sta $0103,x
  28. lda $0104,x
  29. ldy z80_l
  30. sta z80_l
  31. tya
  32. sta $104,x
  33. lda z80_e ;- ex de,hl
  34. ldx z80_l
  35. stx z80_e
  36. sta z80_l
  37. lda z80_d
  38. ldx z80_h
  39. stx z80_d
  40. sta z80_h
  41. __ARRAY_FREE_FAST: ; Fastcall entry: DE = Number of elements
  42. lda z80_h ;- ld a,h
  43. sta z80_a
  44. ora z80_l ;- or l
  45. bne *+3 ;- ret z ; ret if NULL
  46. rts
  47. lda z80_d ;- ld b,d
  48. sta z80_b
  49. lda z80_e ;- ld c,e
  50. sta z80_c
  51. ldy #$00 ;- ld e,(hl)
  52. lda (z80_hl),y
  53. sta z80_e
  54. inc z80_l ;- inc hl
  55. bne *+4
  56. inc z80_h
  57. ldy #$00 ;- ld d,(hl)
  58. lda (z80_hl),y
  59. sta z80_d
  60. inc z80_l ;- inc hl ; DE = Number of dimensions
  61. bne *+4
  62. inc z80_h
  63. lda z80_e ;- ex_de_hl
  64. ldx z80_l
  65. stx z80_e
  66. sta z80_l
  67. lda z80_d
  68. ldx z80_h
  69. stx z80_d
  70. sta z80_h
  71. asl z80_l ;- add hl,hl ; HL = HL * 2
  72. rol z80_h
  73. clc ;- add hl,de
  74. lda z80_l
  75. adc z80_e
  76. sta z80_l
  77. lda z80_h
  78. adc z80_d
  79. sta z80_h
  80. inc z80_l ;- inc hl ; HL now points to the element start
  81. bne *+4
  82. inc z80_h
  83. __ARRAY_LOOP:
  84. ldy #$00 ;- ld e,(hl)
  85. lda (z80_hl),y
  86. sta z80_e
  87. inc z80_l ;- inc hl
  88. bne *+4
  89. inc z80_h
  90. ldy #$00 ;- ld d,(hl)
  91. lda (z80_hl),y
  92. sta z80_d
  93. inc z80_l ;- inc hl ; DE = (HL) = String Pointer
  94. bne *+4
  95. inc z80_h
  96. lda z80_l ;- push hl
  97. pha
  98. lda z80_h
  99. pha
  100. lda z80_c ;- push bc
  101. pha
  102. lda z80_b
  103. pha
  104. lda z80_e ;- ex de,hl
  105. ldx z80_l
  106. stx z80_e
  107. sta z80_l
  108. lda z80_d
  109. ldx z80_h
  110. stx z80_d
  111. sta z80_h
  112. jsr __MEM_FREE ;- call __MEM_FREE ; Frees it from memory
  113. pla ;- pop bc
  114. sta z80_b
  115. pla
  116. sta z80_c
  117. pla ;- pop hl
  118. sta z80_h
  119. pla
  120. sta z80_l
  121. ;- dec bc
  122. lda z80_b ;- ld a,b
  123. sta z80_a
  124. ora z80_c ;- or c
  125. jne __ARRAY_LOOP ;- jp nz, __ARRAY_LOOP
  126. rts ;- ret ; Frees it and return
  127. ;- ENDP
  128. __ARRAY_FREE_MEM: ; like the above, buf also frees the array itself
  129. lda z80_e ;- ex_de_hl
  130. ldx z80_l
  131. stx z80_e
  132. sta z80_l
  133. lda z80_d
  134. ldx z80_h
  135. stx z80_d
  136. sta z80_h
  137. pla ;- pop hl ; (ret address)
  138. sta z80_h
  139. pla
  140. sta z80_l
  141. tsx ;- ex (sp),hl ; Callee -> HL = Number of elements
  142. lda $0103,x
  143. ldy z80_h
  144. sta z80_h
  145. tya
  146. sta $0103,x
  147. lda $0104,x
  148. ldy z80_l
  149. sta z80_l
  150. tya
  151. sta $104,x
  152. lda z80_e ;- ex de,hl
  153. ldx z80_l
  154. stx z80_e
  155. sta z80_l
  156. lda z80_d
  157. ldx z80_h
  158. stx z80_d
  159. sta z80_h
  160. lda z80_l ;- push hl ; Saves array pointer for later
  161. pha
  162. lda z80_h
  163. pha
  164. jsr __ARRAY_FREE_FAST ;- call __ARRAY_FREE_FAST
  165. pla ;- pop hl ; recovers array block pointer
  166. sta z80_h
  167. pla
  168. sta z80_l
  169. jmp __MEM_FREE ;- jp __MEM_FREE ; Frees it and returns from __MEM_FREE