Memory allocation: Difference between revisions

Added ZX81 BASIC
(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
(Added ZX81 BASIC)
Line 1,247:
=={{header|Scala}}==
The same as Java applies to Scala, because the VM will take of memory allocation by means of the Memory Manager. In Scala it's not a programmer concern.
 
=={{header|Sinclair ZX81 BASIC}}==
Ordinary variables spring into existence when they are first assigned to; arrays need to be <code>DIM</code>ensioned first. There is no easy way to remove a <i>particular</i> named variable, but a <code>CLEAR</code> statement removes <i>all</i> user-defined variables. This can be useful under two sets of circumstances: (1) if the program has, say, a setting-up section whose variables will not be needed again, so that their storage space can be reclaimed, or (2) if you are editing a larger program—variables persist even after the program has finished running, so <code>CLEAR</code>ing them frees up some memory and may make viewing and editing your program more comfortable.
 
If you want to allocate an arbitrary block of bytes that the interpreter will not interfere with, there are two ways to do it. The first is by altering the system variable <tt>RAMTOP</tt>: this is a 16-bit value stored in little-endian format at addresses 16388 and 16389, and tells BASIC the highest byte it can use. On a 1k ZX81, <tt>RAMTOP</tt> equals 17408 (until you change it); to find it on your system, use
<lang basic>PRINT PEEK 16388+256*16389</lang>
You can then use <code>POKE</code> statements to reset <tt>RAMTOP</tt> to a lower value, thus reserving the space above it.
 
The second approach, suitable especially if you want to reserve a few bytes for a small machine code subroutine, is to hide the storage space you want inside a comment. When you enter a <code>REM</code> statement, the interpreter sets aside sufficient bytes to store the text of your comment <i>and then doesn't care what you put in them</i>: if you know the address where the comment is stored, therefore, you can <code>POKE</code> whatever values you like into that space. If the comment is the first line in the program, the <code>REM</code> itself will be at address 16513 and the comment text will begin at 16514 and take up one byte per character. An example, with a trivial machine code routine (it adds 3 and 4):
<lang basic>10 REM ABCDEFGHI
20 LET P$="3E030E04814F0600C9"
30 LET ADDR=16514
40 POKE ADDR,CODE P$*16+CODE P$(2)-476
50 LET P$=P$(3 TO )
60 LET ADDR=ADDR+1
70 IF P$<>"" THEN GOTO 40
80 CLEAR
90 PRINT USR 16514</lang>
The <tt>ABCDEFGHI</tt> is arbitrary: any other nine characters would work just as well. The string in line <tt>20</tt> is the hex representation of the Z80 code, which could be disassembled as:
<lang z80asm>3e 03 ld a, 3
0e 04 ld c, 4
81 add a, c
4f ld c, a
06 00 ld b, 0
c9 ret</lang>
Line <tt>40</tt> reads a two-digit hex number and pokes its value into memory. <code>USR</code> ("user sub routine"), in line <tt>90</tt>, is a function that takes the address of a machine language routine, calls it, and returns the contents of the <tt>BC</tt> register pair when the routine terminates. Under normal circumstances, once you were satisfied the machine code program was working correctly you would remove lines <tt>20</tt> to <tt>80</tt>, leaving just the machine code subroutine and the call to it. Note that if you list the program once you have run it, the first line will look something like this:
<lang basic>10 REM Y▀:▖▟?▞ TAN</lang>
Unfortunately, you cannot type that in directly: not all 256 possible values are accessible from the keyboard, so there is no point trying to learn to enter machine code in that form.
 
=={{header|SNOBOL4}}==
519

edits