2019-06-23 21:44:55 +00:00
|
|
|
// Experiments with malloc() - a word array
|
|
|
|
.pc = $801 "Basic"
|
2020-06-27 20:36:52 +00:00
|
|
|
:BasicUpstart(__start)
|
2019-06-23 21:44:55 +00:00
|
|
|
.pc = $80d "Program"
|
|
|
|
.const SIZEOF_WORD = 2
|
2019-07-09 09:40:56 +00:00
|
|
|
// Top of the heap used by malloc()
|
|
|
|
.label HEAP_TOP = $a000
|
2019-06-24 21:22:20 +00:00
|
|
|
.label WORDS = malloc.return
|
2020-06-27 20:36:52 +00:00
|
|
|
__start: {
|
2020-06-27 18:32:09 +00:00
|
|
|
// malloc(0x200)
|
|
|
|
jsr malloc
|
|
|
|
jsr main
|
|
|
|
rts
|
|
|
|
}
|
2019-06-23 21:44:55 +00:00
|
|
|
main: {
|
|
|
|
.label w = 2
|
2019-06-24 21:22:20 +00:00
|
|
|
lda #<WORDS
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z w
|
2019-06-24 21:22:20 +00:00
|
|
|
lda #>WORDS
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z w+1
|
2019-06-23 21:44:55 +00:00
|
|
|
ldx #0
|
2019-09-29 21:13:37 +00:00
|
|
|
__b1:
|
2020-02-23 08:44:36 +00:00
|
|
|
// *w++ = i
|
2019-06-23 21:44:55 +00:00
|
|
|
txa
|
|
|
|
ldy #0
|
|
|
|
sta (w),y
|
|
|
|
tya
|
|
|
|
iny
|
|
|
|
sta (w),y
|
2020-02-23 08:44:36 +00:00
|
|
|
// *w++ = i;
|
2019-06-23 21:44:55 +00:00
|
|
|
lda #SIZEOF_WORD
|
|
|
|
clc
|
2019-08-07 19:00:19 +00:00
|
|
|
adc.z w
|
|
|
|
sta.z w
|
2019-06-23 21:44:55 +00:00
|
|
|
bcc !+
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z w+1
|
2019-06-23 21:44:55 +00:00
|
|
|
!:
|
2020-02-23 08:44:36 +00:00
|
|
|
// for( byte i: 0..255)
|
2019-06-23 21:44:55 +00:00
|
|
|
inx
|
|
|
|
cpx #0
|
2019-09-29 21:13:37 +00:00
|
|
|
bne __b1
|
2020-02-23 08:44:36 +00:00
|
|
|
// }
|
2019-06-23 21:44:55 +00:00
|
|
|
rts
|
|
|
|
}
|
2020-04-13 18:00:13 +00:00
|
|
|
// Allocates a block of size chars of memory, returning a pointer to the beginning of the block.
|
2019-06-23 21:44:55 +00:00
|
|
|
// The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.
|
|
|
|
malloc: {
|
2019-07-09 09:40:56 +00:00
|
|
|
.const size = $200
|
|
|
|
.label mem = HEAP_TOP-size
|
|
|
|
.label return = mem
|
2019-06-23 21:44:55 +00:00
|
|
|
rts
|
|
|
|
}
|