Skip to content

Commit f529bd2

Browse files
committed
Avoid using heap in load routine
This is needed to make LOAD "" DATA safe.
1 parent 21bafda commit f529bd2

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

arch/zx48k/library-asm/load.asm

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include once <alloc.asm>
21
#include once <free.asm>
32

43
#ifndef HIDE_LOAD_MSG
@@ -26,12 +25,14 @@ LOAD_CODE:
2625
LOCAL LOAD_END
2726
LOCAL VR_CONTROL, VR_CONT_1, VR_CONT_2
2827
LOCAL MEM0
28+
LOCAL TMP_SP
2929

3030
MEM0 EQU 5C92h ; Temporary memory buffer
3131
HEAD1 EQU MEM0 + 8 ; Uses CALC Mem for temporary storage
3232
; Must skip first 8 bytes used by
3333
; PRINT routine
3434
TMP_HEADER EQU HEAD1 + 17 ; Temporary HEADER2 pointer storage
35+
TMP_SP EQU TMP_HEADER + 2 ; Temporary SP storage
3536

3637
#ifdef __ENABLE_BREAK__
3738
LD_BYTES EQU 0556h ; ROM Routine LD-BYTES
@@ -59,6 +60,8 @@ __LOAD_CODE: ; INLINE version
5960
ld (ix + 13), e
6061
ld (ix + 14), d ; Store address in bytes
6162

63+
push hl ; String ptr to be freed later
64+
6265
ld a, h
6366
or l
6467
ld b, h
@@ -99,15 +102,15 @@ LOAD_CONT:
99102
ldir ; Copy String block NAME in header
100103

101104
LOAD_CONT2:
102-
ld bc, 17; 2nd Header
103-
call __MEM_ALLOC
105+
pop hl ; String ptr
106+
call MEM_FREE
104107

105-
ld a, h
106-
or l
107-
jr nz, LOAD_CONT3; there's memory
108-
109-
ld a, ERROR_OutOfMemory
110-
jp __ERROR
108+
ld hl, 0
109+
add hl, sp
110+
ld (TMP_SP), hl
111+
ld bc, -18
112+
add hl, sp
113+
ld sp, hl
111114

112115
LOAD_CONT3:
113116
ld (TMP_HEADER), hl
@@ -239,9 +242,10 @@ LOAD_ERROR:
239242
ld (ERR_NR), a
240243

241244
LOAD_END:
245+
ld hl, (TMP_SP)
246+
ld sp, hl ; Recovers stack
242247
pop ix ; Recovers stack frame pointer
243-
ld hl, (TMP_HEADER) ; Recovers tmp_header pointer
244-
jp MEM_FREE ; Returns via FREE_MEM, freeing tmp header
248+
ret
245249

246250
#ifndef __ENABLE_BREAK__
247251
LOCAL LD_BYTES_RET

0 commit comments

Comments
 (0)