Basic VAL function

The place for codemasters or beginners to talk about programming any language for the Spectrum.
tsm
Berk
Posts: 7
Joined: Thu May 24, 2018 8:59 am

Basic VAL function

Post by tsm » Thu May 24, 2018 9:16 am

Hello!
If I'm not mistaken, the VAL function takes a string containing an expression and returns a number which represents the result of the expression.
I'm reading a Basic listing and I found some constructs that confuse me a little. Some examples:

LET d=VAL "256"
GO SUB VAL "900"
POKE VAL "23658",VAL "8"

Why not simply:

LET d=256
GO SUB 900
POKE 23658,8

:?: :?: :?:

Thank you!
0 x

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

Re: Basic VAL function

Post by djnzx48 » Thu May 24, 2018 9:48 am

In this case VAL's probably being used to save memory. Numbers on the Spectrum are stored with a 5-byte numeric representation accompanying the textual representation, which speeds up the interpreter as it doesn't have to parse each number every time it reads it. The downside is this ends up wasting memory. Although VAL looks like it's making the program larger, it actually saves bytes in most cases: you have three extra bytes for the VAL and the two quote marks, but you no longer have to store the 5-byte number and so you save two bytes overall.

There are also some other tricks you can do, such as BIN for zero, SGN PI for one, and INT PI for 3, but VAL is more general and works in most situations.

Hope this helps!
0 x

User avatar
RMartins
Manic Miner
Posts: 392
Joined: Thu Nov 16, 2017 3:26 pm

Re: Basic VAL function

Post by RMartins » Thu May 24, 2018 10:07 am

It's to save memory, since in BASIC representation, VAL "256", takes less bytes to represent the number 256 directly.

This saves memory, if you are really tight on it, but it's slower. So it's a compromise, that can be used on code that does not run often, or is just part of the loader for example.

The less bytes you consume in basic, the more bytes you can LOAD "" CODE, for assembly, for example.
0 x

tsm
Berk
Posts: 7
Joined: Thu May 24, 2018 8:59 am

Re: Basic VAL function

Post by tsm » Thu May 24, 2018 10:13 am

Got it, thank you!
0 x

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

Re: Basic VAL function

Post by R-Tape » Thu May 24, 2018 10:16 am

I think this is done more from habit than necessity. VAL and NOT PI are commonly used in short loaders for machine code games where the handful of bytes it saves doesn't make any difference, but then if you can save a few bytes then why not.

Actually how many bytes does NOT PI save?
0 x

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

Re: Basic VAL function

Post by Ast A. Moore » Thu May 24, 2018 10:48 am

R-Tape wrote:
Thu May 24, 2018 10:16 am
Actually how many bytes does NOT PI save?
Four.
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.

spectron
Berk
Posts: 22
Joined: Thu Mar 29, 2018 2:27 pm

Re: Basic VAL function

Post by spectron » 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.

It may have been that it became habit and as mentioned, if you can save bytes then why not?
0 x

User avatar
Einar Saukas
Manic Miner
Posts: 949
Joined: Wed Nov 15, 2017 2:48 pm

Re: Basic VAL function

Post by Einar Saukas » Thu May 24, 2018 1:39 pm

Ast A. Moore wrote:
Thu May 24, 2018 10:48 am
R-Tape wrote:
Thu May 24, 2018 10:16 am
Actually how many bytes does NOT PI save?
Four.
Actually five :)
0 x

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

Re: Basic VAL function

Post by Ast A. Moore » Thu May 24, 2018 2:07 pm

Einar Saukas wrote:
Thu May 24, 2018 1:39 pm
Ast A. Moore wrote:
Thu May 24, 2018 10:48 am
R-Tape wrote:
Thu May 24, 2018 10:16 am
Actually how many bytes does NOT PI save?
Four.
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.
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
Einar Saukas
Manic Miner
Posts: 949
Joined: Wed Nov 15, 2017 2:48 pm

Re: Basic VAL function

Post by Einar Saukas » 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
Ast A. Moore wrote:
Thu May 24, 2018 10:48 am


Four.
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.
0 x

Post Reply