Page 1 of 1

what is this code doing exactly?

Posted: Tue Mar 28, 2023 11:17 pm
by Crayon21
I just typed in the 1k version of pumpkin for the zx81 and have a question:
I noticed that this version is using a lot of VAL statements but where is the information coming from
Logic would dictate that the CPU would be getting the info from data statements. Obviously sinclair basic lacks this (until the speccy anyway) so how exactly does this code function? What's happening here. can someone break this down step by step?

https://archive.org/details/syncmagazin ... ew=theater
page 11

Re: what is this code doing exactly?

Posted: Wed Mar 29, 2023 4:26 am
by SafePit
VAL interprets the string as a number or expression of a number (i.e. with functions and other calculations). Most of the use was to save memory (i.e. VAL "5" is the same as 5 but, but the first is 4 bytes and the second is 7 bytes). The most complicated is line 15, but you could remove the VAL and quotes and would run just fine, just may not fit in 1K.

Re: what is this code doing exactly?

Posted: Wed Mar 29, 2023 7:55 am
by 1024MAK
SafePit wrote: Wed Mar 29, 2023 4:26 amMost of the use was to save memory (i.e. VAL "5" is the same as 5 but, but the first is 4 bytes and the second is 7 bytes).
This may sound strange, but it is due to the way that numbers are stored.

A numerical constant in the program, both the visible “text” is stored and also an internal floating point representation of it (which is hidden during listing) which takes up six bytes. Hence the number 5 takes up a total of seven bytes in memory. See also the chapter 27 in the manual here. The link to the full manual is here.

However numbers in between double quotes like “5” are treated as plain text. So VAL “5” uses four bytes (including one byte for the keyword ’token’ used for ‘VAL’.

The advantage as SafePit says, is by using VAL you save memory compared to not doing so. Very useful for a machine that only has 1K bytes of RAM. The downside is that using VAL in this way is slower.

Mark