strcpy.asm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include once <realloc.asm>
  2. ; String library
  3. __STRASSIGN: ; Performs a$ = b$ (HL = address of a$; DE = Address of b$)
  4. ;- PROC
  5. ;- LOCAL __STRREALLOC
  6. ;- LOCAL __STRCONTINUE
  7. ;- LOCAL __B_IS_NULL
  8. ;- LOCAL __NOTHING_TO_COPY
  9. lda z80_d ;- ld b,d
  10. sta z80_b
  11. lda z80_e ;- ld c,e
  12. sta z80_c
  13. lda z80_b ;- ld a,b
  14. sta z80_a
  15. ora z80_c ;- or c
  16. ;- jr z, __B_IS_NULL
  17. lda z80_e ;- ex de,hl
  18. ldx z80_l
  19. stx z80_e
  20. sta z80_l
  21. lda z80_d
  22. ldx z80_h
  23. stx z80_d
  24. sta z80_h
  25. ;- ld c,(hl)
  26. inc z80_l ;- inc hl
  27. bne *+4
  28. inc z80_h
  29. ;- ld b,(hl)
  30. ;- dec hl ; BC = LEN(b$)
  31. lda z80_e ;- ex de,hl ; DE = &b$
  32. ldx z80_l
  33. stx z80_e
  34. sta z80_l
  35. lda z80_d
  36. ldx z80_h
  37. stx z80_d
  38. sta z80_h
  39. __B_IS_NULL: ; Jumps here if B$ pointer is NULL
  40. ;- inc bc
  41. ;- inc bc ; BC = BC + 2 ; (LEN(b$) + 2 bytes for storing length)
  42. lda z80_e ;- push de
  43. pha
  44. lda z80_d
  45. pha
  46. lda z80_l ;- push hl
  47. pha
  48. lda z80_h
  49. pha
  50. lda z80_h ;- ld a,h
  51. sta z80_a
  52. ora z80_l ;- or l
  53. ;- jr z, __STRREALLOC
  54. ;- dec hl
  55. ldy #$00 ;- ld d,(hl)
  56. lda (z80_hl),y
  57. sta z80_d
  58. ;- dec hl
  59. ldy #$00 ;- ld e, (hl) ; DE = MEMBLOCKSIZE(a$)
  60. lda (z80_hl),y
  61. sta z80_e
  62. ;- dec de
  63. ;- dec de ; DE = DE - 2 ; (Membloksize takes 2 bytes for memblock length)
  64. lda z80_b ;- ld h,b
  65. sta z80_h
  66. lda z80_c ;- ld l, c ; HL = LEN(b$) + 2 => Minimum block size required
  67. sta z80_l
  68. lda z80_e ;- ex de, hl ; Now HL = BLOCKSIZE(a$), DE = LEN(b$) + 2
  69. ldx z80_l
  70. stx z80_e
  71. sta z80_l
  72. lda z80_d
  73. ldx z80_h
  74. stx z80_d
  75. sta z80_h
  76. ora z80_a ;- or a ; Prepare to subtract BLOCKSIZE(a$) - LEN(b$)
  77. ;- sbc hl, de ; Carry if len(b$) > Blocklen(a$)
  78. jcc __STRREALLOC ;- jr c, __STRREALLOC ; No need to realloc
  79. ; Need to reallocate at least to len(b$) + 2
  80. lda z80_e ;- ex de,hl ; DE = Remaining bytes in a$ mem block.
  81. ldx z80_l
  82. stx z80_e
  83. sta z80_l
  84. lda z80_d
  85. ldx z80_h
  86. stx z80_d
  87. sta z80_h
  88. lda #4 ;- ld hl, 4
  89. sta z80_l
  90. lda #0
  91. sta z80_h
  92. ;- sbc hl, de ; if remaining bytes < 4 we can continue
  93. jcs __STRCONTINUE ;- jr nc,__STRCONTINUE ; Otherwise, we realloc, to free some bytes
  94. __STRREALLOC:
  95. pla ;- pop hl
  96. sta z80_h
  97. pla
  98. sta z80_l
  99. jsr __REALLOC ;- call __REALLOC ; Returns in HL a new pointer with BC bytes allocated
  100. lda z80_l ;- push hl
  101. pha
  102. lda z80_h
  103. pha
  104. __STRCONTINUE: ; Pops hl and de SWAPPED
  105. pla ;- pop de ; DE = &a$
  106. sta z80_d
  107. pla
  108. sta z80_e
  109. pla ;- pop hl ; HL = &b$
  110. sta z80_h
  111. pla
  112. sta z80_l
  113. lda z80_d ;- ld a,d ; Return if not enough memory for new length
  114. sta z80_a
  115. ora z80_e ;- or e
  116. bne *+3 ;- ret z ; Return if DE == NULL (0)
  117. rts
  118. __STRCPY: ; Copies string pointed by HL into string pointed by DE
  119. ; Returns DE as HL (new pointer)
  120. lda z80_h ;- ld a,h
  121. sta z80_a
  122. ora z80_l ;- or l
  123. jeq __NOTHING_TO_COPY ;- jr z, __NOTHING_TO_COPY
  124. ;- ld c,(hl)
  125. inc z80_l ;- inc hl
  126. bne *+4
  127. inc z80_h
  128. ldy #$00 ;- ld b,(hl)
  129. lda (z80_hl),y
  130. sta z80_b
  131. ;- dec hl
  132. inc z80_c ;- inc bc
  133. bne *+4
  134. inc z80_b
  135. inc z80_c ;- inc bc
  136. bne *+4
  137. inc z80_b
  138. lda z80_e ;- push de
  139. pha
  140. lda z80_d
  141. pha
  142. ;- ldir
  143. pla ;- pop hl
  144. sta z80_h
  145. pla
  146. sta z80_l
  147. rts ;- ret
  148. __NOTHING_TO_COPY:
  149. lda z80_e ;- ex de,hl
  150. ldx z80_l
  151. stx z80_e
  152. sta z80_l
  153. lda z80_d
  154. ldx z80_h
  155. stx z80_d
  156. sta z80_h
  157. lda z80_e ;- ld (hl),e
  158. ldy #$00
  159. sta (z80_hl),y
  160. inc z80_l ;- inc hl
  161. bne *+4
  162. inc z80_h
  163. lda z80_d ;- ld (hl),d
  164. ldy #$00
  165. sta (z80_hl),y
  166. ;- dec hl
  167. rts ;- ret
  168. ;- ENDP