4

I was getting ?OUT OF MEMORY ERROR when I was trying to get directory listing of a disk after returned assembly routine.

I've prepared a simple routine just to understand what was happening, e.g:

*=$c000

 lda #$00
 sta $d020
 rts

And my build script is:

C:\...\acme0.95.6win\acme.exe --outfile build\ROUTFILE --format cbm RSOURCEFILE
C:\...\tools\WinVICE-2.4-x64\x64.exe build\ROUTFILE

When I run the build script emulator loads the PRG file. Tries to run it but nothing happens as I didn't include the BASIC loader. Even after that point if I writre LOAD "$",8 to get directory listing. I'm getting ?OUT OF MEMORY ERROR. So what is wrong in above scenario?


Hex dump of the PRG file is:

00 c0 a9 00 8d 20 d0 60 
wizofwor
  • 917
  • 8
  • 25
  • 1
    You seem to be trashing the memory pointers of the BASIC environment. At first glance the build script looks reasonable though and should emit and respect the `$C000` load address. Can you show us a HEX dump of the resulting binary? – doynax Apr 19 '17 at 15:25
  • 2
    You need to load with `,8,1` and run it using `sys 49152`. The default behaviour is probably tailored for basic programs. – Jester Apr 19 '17 at 17:19
  • @Jester the emulator automatically loads with `,8,1` and the problem appreas. I tried loading with `,8` and there was no problem. – wizofwor Apr 19 '17 at 19:21

2 Answers2

10

This appears to be just an artifact how the Commodore 64 loads programs. When you you use the LOAD command to load something in memory, the pointers to the start and end of various BASIC data areas are set to the address immediately after the loaded program. So for a 6 byte program loaded at $C000, these pointers are set to $C006. However the end of BASIC memory pointer remains at $A000. This creates an invalid situation, the as the pointers to BASIC's data areas are higher than the maximum address BASIC is allowed to use. It ends up looking like BASIC has run out of memory.

Usually this isn't a problem. You can only load assembly programs to $C000, and the so normally first thing and only thing you do after loading the program is execute the program by entering SYS 49152, like Jester mentioned. If you want load the program but not execute it right away you can fix the invalid pointer problem by using the NEW command as given in Peter Kofler's answer, this should reset the pointers to their valid default states, while leaving the code loaded at $C000 unmodified.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • But what if you want to return to basic to return commands? I'm using CDM Studio: i've got ASM byte code for character set loaded from *=$3800, and i have a routine at *=$C000 to point the VIC to that character set (with "RTS" at the end). My basic code has 10 SYS 49152, 20 PRINT CHR$(147) and i'm getting "out of memory" at line 20... I can't use "new" because that wipes the basic memory?? – Simon Sep 12 '21 at 06:12
8

Sometimes after loading data into higher areas of memory, in your case $c000, the memory pointer is broken (as doynax suggests). You need to do a

NEW

to fix that. (The Basic memory pointer is updated to the end of the loaded file. If your file ends after $A000 you are out of Basic memory, resulting in OUT OF MEMORY ERROR.) After that you will be able to load $. As you have no Basic code, that will not do any harm.

I do not know the tools you are using, but as mentioned by Jester, your code needs to be loaded as ,8,1 so the load address is respected. The code you shared is correct, it starts with 00 C0 as first two bytes which are the load address.

If load "$",8 succeeds and your file is on the disk, try loading it ,8,1 and then try sys 49152 (which is 0xC000).

Peter Kofler
  • 9,252
  • 8
  • 51
  • 79
  • `NEW` command works but I didn't understand why I'm having this problem. If I load the file manually by writing `load "file",8,1` the same problem appreas. When I write 'load "file",8 there is no problem. – wizofwor Apr 19 '17 at 19:18
  • 2
    @wizofwor When you write `LOAD"FILE",8` there _is_ a problem: The file isn't loaded at $C000 into memory but at the BASIC memory start at $0801. You need to start it there with `SYS2049` and that only works because your machine program doesn't use absolute addressing of code or data. – BlackJack Oct 26 '17 at 19:27