Post by wendling on Mar 18, 2021 17:22:38 GMT
Okay, stiggity: buffer to string.
The task:
When the string variable exists, make room in the stringpool, put there the contents of the buffer, change the link in the existing string variable to the new space in the stringpool with the buffer contents. Leave the old contents in memory. They will be marked as garbage when memory is full and the system performs a garbage collect. (marked as garbage: linkbyte low=lenght string, linkbyte high = $ff in the stringpool at end of bank1)
(I performed a garbage collect with $92ea (mapping the c128) and looked in the stringpool what changed, that's how I know that the linkbytes changed)
When the string variable is undefined, make room in the variable space by shifting array space 7 bytes, add 7 bytes to array start(which is also variable end) and array end.
bc$="hello" after sys4864 then bc$="stiggity"
no bc$ declared:after sys4864 then bc$ exists and bc$="stiggity"
after sys4868,asc("g"),asc("j") then gj$="stiggity"
Here is the code:
length def $1500 8
buffer def $1501 "stiggity"
$1300:
lda #$42 ; varname "b" (after sys4864 $bc="stiggity")
ldx #$43 ; varname "c"
sta $47 ; sys dec("1304"),asc(stringvarchar1),asc(stringvarchar2)
txa
ora #$80 ; add #80 to second stringchar to indicate it is a strinvar
sta $48
lda $2f ; get start address of variable space
sta $bb
lda $30
sta $bc
cmp $31
bne checkvar
lda $bc
cmp $32
beq createspace ; yes then var undefined and create new space
checkvar:
ldy #$00 ; check for varname
jsr fetchbb ; get first byte varname
cmp $47 ; first char
bne nextvar
iny
jsr fetchbb ; get second byte varname
cmp $48 ; second char
beq varfound
lda $bb
adc #$07
sta $bb
lda $bc
adc #$00
sta $bc
jmp varloop
createspace:
jsr newvar ; create space for new stringvar
varfound:
ldy $1500 ; buffer length
iny
iny ; add two for pointer
sty $fb
lda $35 ; create space in stringpool
sec
sbc $fb
sta $35
lda $36
sbc #$00
sta $36
lda $bb ; pointer points to right after stringvarname
clc
adc #$02
sta $fb
lda $bc
adc #$00
sta $fc
dey
lda $fc
jsr stash35 ; store pointer high in stringpool
dey
lda $fb
jsr stash35 ; store pointer low in stringpool
jsr stash35 ; store buffer text in stringpool
dey
bpl bufstoreloop
ldy #$00
lda $47
jsr stashbb ; store varname byte 1 in variable space
iny
lda $48
jsr stashbb ; store varname byte 2 in variable space
iny
lda length
jsr stashbb ; store length string in variable space
iny
lda $35
jsr stashbb ; store pointer low to stringpool in variable space
iny
lda $36
jmp stashbb ; store pointer high to stringpool in variable space
newvar:
lda $31 ; add 7 to start array space to make room for new stringvar
clc
adc #$07
sta $31
lda $32
adc #$00
sta $32
lda $33 ; add 7 to end array space
sta $fb ; save old end for copy
clc
adc #$07
sta $33 ; lowbyte end arrayspace
sta $fd ; save lowbyte new end for copy
lda $34
sta $fc
adc #$00
sta $34 ; highbyte end arrayspace
sta $fe ; save highbyte new end for copy
cmp $31
bne movearrayspace
lda $fe
cmp $32
beq copydone
movearrayspace:
lda $fb ; previous byte old end pointer
bne decreasefb
dec $fc
lda $fd ; previous byte new end pointer
bne decreasefd
dec $fe
ldy #$00
jsr fetchfb ; copy array space from old end array space
jsr stashfd ; to new end
jmp arraymoveloop
fetchbb:
lda #$bb
bne fetch
stash35:
bne stash
lda #$bb
bne stash
lda #$fd
pla
ldx #$7f
jmp $02af
Demo:
100 bu$="herman and hermonia":gosub130
110 bu$="stiggity":gosub130
120 end
130 bu=dec("1500")
140 pokebu,len(bu$)
150 fort=1 to len(bu$)
160 pokebu+t,asc(mid$(bu$,t,1))
170 next
180 ac$="who is walking in the park?
190 a$(5)=" with a dog"
200 print
210 printac$
220 print:print"it is ";
230 sys4864
240 printbc$;
250 printa$(5)
260 print
270 bc$="what a beautiful day, "
280 printbc$;
290 sys4864
300 printbc$
310 return
The task:
When the string variable exists, make room in the stringpool, put there the contents of the buffer, change the link in the existing string variable to the new space in the stringpool with the buffer contents. Leave the old contents in memory. They will be marked as garbage when memory is full and the system performs a garbage collect. (marked as garbage: linkbyte low=lenght string, linkbyte high = $ff in the stringpool at end of bank1)
(I performed a garbage collect with $92ea (mapping the c128) and looked in the stringpool what changed, that's how I know that the linkbytes changed)
When the string variable is undefined, make room in the variable space by shifting array space 7 bytes, add 7 bytes to array start(which is also variable end) and array end.
This is what happens when you use the code:(buffer="stiggity", length=8)
no bc$ declared:after sys4864 then bc$ exists and bc$="stiggity"
after sys4868,asc("g"),asc("j") then gj$="stiggity"
Here is the code:
length def $1500 8
buffer def $1501 "stiggity"
$1300:
lda #$42 ; varname "b" (after sys4864 $bc="stiggity")
ldx #$43 ; varname "c"
sta $47 ; sys dec("1304"),asc(stringvarchar1),asc(stringvarchar2)
txa
ora #$80 ; add #80 to second stringchar to indicate it is a strinvar
sta $48
lda $2f ; get start address of variable space
sta $bb
lda $30
sta $bc
varloop:
lda $bb ; check if end of var space is reachedcmp $31
bne checkvar
lda $bc
cmp $32
beq createspace ; yes then var undefined and create new space
checkvar:
ldy #$00 ; check for varname
jsr fetchbb ; get first byte varname
cmp $47 ; first char
bne nextvar
iny
jsr fetchbb ; get second byte varname
cmp $48 ; second char
beq varfound
nextvar:
clc ; next variable: add 7 byteslda $bb
adc #$07
sta $bb
lda $bc
adc #$00
sta $bc
jmp varloop
createspace:
jsr newvar ; create space for new stringvar
varfound:
ldy $1500 ; buffer length
iny
iny ; add two for pointer
sty $fb
lda $35 ; create space in stringpool
sec
sbc $fb
sta $35
lda $36
sbc #$00
sta $36
lda $bb ; pointer points to right after stringvarname
clc
adc #$02
sta $fb
lda $bc
adc #$00
sta $fc
dey
lda $fc
jsr stash35 ; store pointer high in stringpool
dey
lda $fb
jsr stash35 ; store pointer low in stringpool
dey
bufstoreloop:
lda buffer,y; buffer textjsr stash35 ; store buffer text in stringpool
dey
bpl bufstoreloop
ldy #$00
lda $47
jsr stashbb ; store varname byte 1 in variable space
iny
lda $48
jsr stashbb ; store varname byte 2 in variable space
iny
lda length
jsr stashbb ; store length string in variable space
iny
lda $35
jsr stashbb ; store pointer low to stringpool in variable space
iny
lda $36
jmp stashbb ; store pointer high to stringpool in variable space
newvar:
lda $31 ; add 7 to start array space to make room for new stringvar
clc
adc #$07
sta $31
lda $32
adc #$00
sta $32
lda $33 ; add 7 to end array space
sta $fb ; save old end for copy
clc
adc #$07
sta $33 ; lowbyte end arrayspace
sta $fd ; save lowbyte new end for copy
lda $34
sta $fc
adc #$00
sta $34 ; highbyte end arrayspace
sta $fe ; save highbyte new end for copy
arraymoveloop:
lda $fd ; copy reached new start array space then donecmp $31
bne movearrayspace
lda $fe
cmp $32
beq copydone
movearrayspace:
lda $fb ; previous byte old end pointer
bne decreasefb
dec $fc
decreasefb:
dec $fblda $fd ; previous byte new end pointer
bne decreasefd
dec $fe
decreasefd:
dec $fdldy #$00
jsr fetchfb ; copy array space from old end array space
jsr stashfd ; to new end
jmp arraymoveloop
copydone:
rts
fetchbb:
lda #$bb
bne fetch
fetchfb:
lda #$fbfetch:
ldx #$01
jmp $ff74stash35:
pha
lda #$35bne stash
stashbb:
phalda #$bb
bne stash
stashfd:
phalda #$fd
stash:
sta $02b9pla
ldx #$7f
jmp $02af
Demo:
100 bu$="herman and hermonia":gosub130
110 bu$="stiggity":gosub130
120 end
130 bu=dec("1500")
140 pokebu,len(bu$)
150 fort=1 to len(bu$)
160 pokebu+t,asc(mid$(bu$,t,1))
170 next
180 ac$="who is walking in the park?
190 a$(5)=" with a dog"
200 print
210 printac$
220 print:print"it is ";
230 sys4864
240 printbc$;
250 printa$(5)
260 print
270 bc$="what a beautiful day, "
280 printbc$;
290 sys4864
300 printbc$
310 return