Variable size/Get: Difference between revisions
Content added Content deleted
No edit summary |
(Applesoft BASIC) |
||
Line 147: | Line 147: | ||
Note that when used with a string, <code>LEN</code> reports the length of the string, not its size in memory. |
Note that when used with a string, <code>LEN</code> reports the length of the string, not its size in memory. |
||
BASIC typically stores information about the string separately from the string itself, usually immediately before the string itself in memory (but some implementations may store such information elsewhere). |
BASIC typically stores information about the string separately from the string itself, usually immediately before the string itself in memory (but some implementations may store such information elsewhere). |
||
==={{header|Applesoft BASIC}}=== |
|||
Simple (non-array) real, integer, or string variables use 7 bytes including the 2 bytes for the variable name. Real variables use all 5 bytes for the value: a 1 byte exponent, and a 4 byte mantissa. Integer variables use 2 bytes for the value, and have 0's in the remaining 3 bytes. String variables use 3 bytes: 1 byte for the length of the string, 2 bytes for the pointer to the string in memory, and have 0's in the remaining 2 bytes. |
|||
Functions use 2 bytes for the pointer to the function definition in the program, 2 bytes for the pointer to the argument, a 1 byte copy of the first character of the function definition, and 7 bytes to store the argument variable. |
|||
To get the size of the variable, determine the type of the last variable used. The sizes reported do not include the 2 bytes to store the variable name. The sizes of array values are only for the specific value within an array. |
|||
<syntaxhighlight lang="gwbasic"> 100 PRINT "SIZE OF INTEGER I% IS ";:I% = I%: GOSUB 240 |
|||
110 PRINT "SIZE OF FLOAT I IS ";:I = I: GOSUB 240 |
|||
120 PRINT "SIZE OF STRING I$ IS ";:I$ = I$: GOSUB 240 |
|||
130 PRINT " LEN OF STRING I$ IS " LEN (I$) |
|||
140 PRINT "SIZE OF FLOAT ARG N IS ";: DEF FN I(N) = 0: GOSUB 240 |
|||
150 PRINT "SIZE OF FUNCTION FN I IS ";: PRINT MID$ ( STR$ ( FN I(0)),1,0);: GOSUB 240 |
|||
160 PRINT "ARRAYS:" |
|||
170 PRINT "SIZE OF FLOAT I(1) IS ";:I(1) = I(1): GOSUB 240 |
|||
180 PRINT "SIZE OF INTEGER I%(2) IS ";:I%(2) = I%(2): GOSUB 240 |
|||
190 PRINT "SIZE OF STRING I$(3) IS ";:I$(3) = I$(3): GOSUB 240 |
|||
200 PRINT " LEN OF STRING I$(3) IS " LEN (I$(3)) |
|||
210 PRINT "SIZE OF STRING I$(4) IS ";:I$(4) = "HELLO, WORLD!": GOSUB 240 |
|||
220 PRINT " LEN OF STRING I$(4) IS " LEN (I$(4)) |
|||
230 END |
|||
240 GOSUB 250: PRINT PEEK (236) + PEEK (237) * 256: RETURN |
|||
250 POKE 236,12: POKE 237,0: IF PEEK (129) > 127 AND PEEK (130) < 128 THEN RETURN |
|||
260 POKE 236,5 |
|||
270 IF PEEK (129) < 128 AND PEEK (130) > 127 GOTO 310STR |
|||
280 IF PEEK (131) + PEEK (132) * 256 < PEEK (107) + PEEK (108) * 256 THEN RETURN |
|||
290 IF PEEK (129) < 128 AND PEEK (130) < 128 THEN RETURN |
|||
300 POKE 236,2: IF PEEK (129) > 127 AND PEEK (130) > 127 THEN RETURN |
|||
310 IF PEEK (131) + PEEK (132) * 256 > = PEEK (107) + PEEK (108) * 256 THEN POKE 236,3 |
|||
320 IF PEEK ( PEEK (131) + PEEK (132) * 256) + PEEK (236) > 255 THEN POKE 237,1: POKE 236, PEEK ( PEEK (131) + PEEK (132) * 256) + PEEK (236) - 256: RETURN |
|||
330 POKE 236, PEEK ( PEEK (131) + PEEK (132) * 256) + PEEK (236) |
|||
340 RETURN</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
SIZE OF INTEGER I% IS 5 |
|||
SIZE OF FLOAT I IS 5 |
|||
SIZE OF STRING I$ IS 5 |
|||
LEN OF STRING I$ IS 0 |
|||
SIZE OF FLOAT ARG N IS 5 |
|||
SIZE OF FUNCTION FN I IS 12 |
|||
ARRAYS: |
|||
SIZE OF FLOAT I(1) IS 5 |
|||
SIZE OF INTEGER I%(2) IS 2 |
|||
SIZE OF STRING I$(3) IS 3 |
|||
LEN OF STRING I$(3) IS 0 |
|||
SIZE OF STRING I$(4) IS 16 |
|||
LEN OF STRING I$(4) IS 13 |
|||
</pre> |
|||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |