Basic VAL function

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
Einar Saukas
Manic Miner
Posts: 953
Joined: Wed Nov 15, 2017 2:48 pm

Re: Basic VAL function

Post by Einar Saukas » Thu May 24, 2018 2:16 pm

spectron wrote:
Thu May 24, 2018 1:23 pm
The above is all relevant and correct but it was especially useful in Sinclair Basic on the unexpanded ZX81.

With only 1K for screen and program, every byte was even more important than on the Spectrum.
Text adventures in Sinclair BASIC usually need this trick to fit in 16K.

This is also useful for small tape loaders. Making them smaller allows setting RAMTOP to a lower address, so you can have more available space to load a code block.
0 x

User avatar
R-Tape
Site Admin
Posts: 2665
Joined: Thu Nov 09, 2017 11:46 am

Re: Basic VAL function

Post by R-Tape » Thu May 24, 2018 3:19 pm

Good job black works well with every other colour. I wonder if any games chose a Magenta colour scheme purely because they used BORDER PI on loading...
0 x

Kweepa
Manic Miner
Posts: 219
Joined: Sat Feb 03, 2018 6:14 pm
Location: Austin, Texas

Re: Basic VAL function

Post by Kweepa » Thu May 24, 2018 3:41 pm

It's really a bizarre choice for a 1k machine's basic.
I guess the ZX80 BASIC, being integer only, didn't do this?
Microsoft BASIC leaves this kind of optimization in your hands - put the constant in a variable if it's in a tight loop.
0 x

User avatar
Ast A. Moore
Dynamite Dan
Posts: 1319
Joined: Mon Nov 13, 2017 3:16 pm

Re: Basic VAL function

Post by Ast A. Moore » Thu May 24, 2018 6:13 pm

Einar Saukas wrote:
Thu May 24, 2018 2:15 pm
Ast A. Moore wrote:
Thu May 24, 2018 2:07 pm
Einar Saukas wrote:
Thu May 24, 2018 1:39 pm


Actually five :)
Mmm . . . A zero is one byte, plus 5 bytes of the BASIC’s number representation. That’s six bytes. NOT PI is two bytes. Six minus two is four.
Plus one byte for control code 14 that prefixes the number representation IIRC.
D’oh. Right you are!
0 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

User avatar
djnzx48
Manic Miner
Posts: 585
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Basic VAL function

Post by djnzx48 » Fri May 25, 2018 7:46 am

Hmm, it looks like I was mistaken about BIN using fewer bytes. I just checked the memory of a BASIC program and it turns out that even if you have no digits after the BIN, it still uses 5 extra bytes to store the value and so uses exactly the same amount of memory as the number 0.
0 x

User avatar
Ast A. Moore
Dynamite Dan
Posts: 1319
Joined: Mon Nov 13, 2017 3:16 pm

Re: Basic VAL function

Post by Ast A. Moore » Fri May 25, 2018 10:16 am

djnzx48 wrote:
Fri May 25, 2018 7:46 am
Hmm, it looks like I was mistaken about BIN using fewer bytes. I just checked the memory of a BASIC program and it turns out that even if you have no digits after the BIN, it still uses 5 extra bytes to store the value and so uses exactly the same amount of memory as the number 0.
Yes, NOT PI is the way to go. Honestly, when it comes to loaders, I prefer using machine code inside BASIC.
0 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

User avatar
djnzx48
Manic Miner
Posts: 585
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Basic VAL function

Post by djnzx48 » Sun May 27, 2018 3:04 am

You'd have to make sure that the loader is relocatable though, as I'm pretty sure peripherals like the Interface 1 can move BASIC up in memory to make room for their own data.

Another alternative is to find some empty space on the loading screen and stick the loader there. If you go this route, you also get the advantage that it's a lot easier to load into the entire memory without having to worry about what happens when the loader gets overwritten.
0 x

User avatar
Ast A. Moore
Dynamite Dan
Posts: 1319
Joined: Mon Nov 13, 2017 3:16 pm

Re: Basic VAL function

Post by Ast A. Moore » Sun May 27, 2018 8:51 am

djnzx48 wrote:
Sun May 27, 2018 3:04 am
You'd have to make sure that the loader is relocatable though.
Sure. That’s trivial, though, particularly if you’re using a ROM-based loader. A custom loader would have to be relocated to non-contended memory anyway, so if you’re using calls within it, the addresses must be pre-calculated in relation to the execution location. Incidentally, I use both a ROM-based and a custom turbo loader in Yankee. Enable, say, Interface 1 in your emulator, initialize it by typing CAT 1 in BASIC, and then load each version of the game. They’ll both work. ;)
0 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

Pobulous
Berk
Posts: 21
Joined: Wed Nov 15, 2017 12:51 pm

Re: Basic VAL function

Post by Pobulous » Sat Jun 02, 2018 2:53 pm

I liked the trick of putting all the numbers in as zero and poking the correct values into the 5 bytes so you'd end up with loader programs like:

10 CLEAR 0: POKE 0,0: LOAD "" CODE: RANDOMIZE USR 0

You would still save some memory on the larger numbers, plus it would add some simple obfuscation. Also if the line was edited it would now no longer work as the zeros would really be zeros.
2 x

Post Reply