ZX81 BASIC

Anything QL Software or Programming Related.
User avatar
janbredenbeek
Super Gold Card
Posts: 632
Joined: Wed Jan 21, 2015 4:54 pm
Location: Hilversum, The Netherlands

Re: ZX81 BASIC

Post by janbredenbeek »

In ZX Basic, the AND operator just means 'if second operand is nonzero then take first operand as result, else 0'. This even goes for strings, a command like 'PRINT "hello" AND X' prints "hello" if X is nonzero and the empty string if X is zero. This cannot be reproduced on the QL, since the coercion mechanism will try to convert the string to a number which fails if its contents are non-numeric.

Jan


User avatar
bwinkel67
QL Wafer Drive
Posts: 1202
Joined: Thu Oct 03, 2019 2:09 am

Re: ZX81 BASIC

Post by bwinkel67 »

tofro wrote:
bwinkel67 wrote:
tofro wrote: That one I don't get. I can't think of a case atm where parenthesis could save an improperly written program that relies on the specific value of a boolean expression. Can you gve an example?
Well, parenthesis can force the meaning that you expect. If this code below used them then regardless of how AND was implemented (i.e. ZX81 BASIC vs SuperBASIC) would work:

Original code that broke in ZXSimulator which does not implement AND the way ZX81 BASIC does but instead the way SuperBASIC does. The meaning is to subtract 1 from A unless A is 0 but using the ZX81 understanding of AND it evaluates the A-1 and then the A<>0 and returns the first if the second is not 0 (in SuperBASIC the entire AND equates to 1 or 0):

Code: Select all

200 IF X$="N" THEN LET A=A-1 AND A<>0
But works with parenthesis using both ways:

Code: Select all

200 IF X$="N" THEN LET A=A-(1 AND A<>0)
... and could actually be cleaned up to do this:

Code: Select all

200 IF X$="N" THEN LET A=A-(A<>0)


User avatar
bwinkel67
QL Wafer Drive
Posts: 1202
Joined: Thu Oct 03, 2019 2:09 am

Re: ZX81 BASIC

Post by bwinkel67 »

From the Sinclair ZX World forum:
As the ZX81 uses a special calculator stack for most math operations, it is likely that the non zero integer was already on the stack, and to save time and space, it's easier just to return what is already there instead of replacing it with 1, to return 1.
So this could make sense, hat it was for optimization reasons not for some unique boolean logic design.


User avatar
NormanDunbar
Forum Moderator
Posts: 2275
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: ZX81 BASIC

Post by NormanDunbar »

When I first saw the AND logical operator I couldn't get my head around code like:

Code: Select all

x = x - (x > 0)
For example, when checking the x position of a character to ensure it cannot go off the left edge of the screen. So I sat down and worked it out (hopefully I've remembered the syntax correctly). It replaced this:

Code: Select all

if x > 0 then x = x - 1
And, apparently, was quicker, although I have my doubts. Less typing definitely, always a bonus on the ZX-81 keyboard.


Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
bwinkel67
QL Wafer Drive
Posts: 1202
Joined: Thu Oct 03, 2019 2:09 am

Re: ZX81 BASIC

Post by bwinkel67 »

Yes, (x > 0) equates to either 0 or 1 and so you add 0 if it's not greater than 0 and thus don't go out of bounds. It's common in most and the ZX81 does support it. It just has that weird behavior with AND and OR that's not very standard.


User avatar
NormanDunbar
Forum Moderator
Posts: 2275
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: ZX81 BASIC

Post by NormanDunbar »

Here's the ZX-81 OR and AND logical operations, if full glorious Technicolour, sorry, Assembly. It's taken from the calculator stack handling routines. There will be two floating point numbers on the calculator stack, addressed by (HL) and (DE) with (HL) pointing to the first value on the stack, (DE) to the second. Extracted from https://www.tablix.org/~avian/spectrum/ ... .htm#L199D.

Code: Select all

; -----------
; Zero or one
; -----------
; This routine places an integer value zero or one at the addressed location
; of calculator stack or MEM area. The value one is written if carry is set on
; entry else zero.

;; FP-0/1
L1AE0:  PUSH    HL              ; save pointer to the first byte
        LD      B,$05           ; five bytes to do.

;; FP-loop
L1AE3:  LD      (HL),$00        ; insert a zero.
        INC     HL              ;
        DJNZ    L1AE3           ; repeat.

        POP     HL              ;
        RET     NC              ;

        LD      (HL),$81        ; make value 1
        RET                     ; return.


; -----------------------
; Handle OR operator (07)
; -----------------------
; The Boolean OR operator. eg. X OR Y
; The result is zero if both values are zero else a non-zero value.
;
; e.g.    0 OR 0  returns 0.
;        -3 OR 0  returns -3.
;         0 OR -3 returns 1.
;        -3 OR 2  returns 1.
;
; A binary operation.
; On entry HL points to first operand (X) and DE to second operand (Y).

;; or
L1AED:  LD      A,(DE)          ; fetch exponent of second number
        AND     A               ; test it.
        RET     Z               ; return if zero.

        SCF                     ; set carry flag
        JR      L1AE0           ; back to FP-0/1 to overwrite the first operand
                                ; with the value 1.


; -----------------------------
; Handle number AND number (08)
; -----------------------------
; The Boolean AND operator.
;
; e.g.    -3 AND 2  returns -3.
;         -3 AND 0  returns 0.
;          0 and -2 returns 0.
;          0 and 0  returns 0.
;
; Compare with OR routine above.

;; no-&-no
L1AF3:  LD      A,(DE)          ; fetch exponent of second number.
        AND     A               ; test it.
        RET     NZ              ; return if not zero.

        JR      L1AE0           ; back to FP-0/1 to overwrite the first operand
                                ; with zero for return value.
HTH

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
bwinkel67
QL Wafer Drive
Posts: 1202
Joined: Thu Oct 03, 2019 2:09 am

Re: ZX81 BASIC

Post by bwinkel67 »

Thank you! I think someone gave me the same thing on the sinclairzxworld forum. Nice to be able to just look at the ROM. Been a while since I've done Z80 Assembly so that's why I just don't look at it myself :-)


Post Reply