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
ZX81 BASIC
- janbredenbeek
- Super Gold Card
- Posts: 632
- Joined: Wed Jan 21, 2015 4:54 pm
- Location: Hilversum, The Netherlands
Re: ZX81 BASIC
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: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?
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
Code: Select all
200 IF X$="N" THEN LET A=A-(1 AND A<>0)
Code: Select all
200 IF X$="N" THEN LET A=A-(A<>0)
Re: ZX81 BASIC
From the Sinclair ZX World forum:
So this could make sense, hat it was for optimization reasons not for some unique boolean logic design.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.
- NormanDunbar
- Forum Moderator
- Posts: 2275
- Joined: Tue Dec 14, 2010 9:04 am
- Location: Leeds, West Yorkshire, UK
- Contact:
Re: ZX81 BASIC
When I first saw the AND logical operator I couldn't get my head around code like:
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:
And, apparently, was quicker, although I have my doubts. Less typing definitely, always a bonus on the ZX-81 keyboard.
Cheers,
Norm.
Code: Select all
x = x - (x > 0)
Code: Select all
if x > 0 then x = x - 1
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.
Author of Arduino Software Internals
Author of Arduino Interrupts
No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
Re: ZX81 BASIC
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.
- NormanDunbar
- Forum Moderator
- Posts: 2275
- Joined: Tue Dec 14, 2010 9:04 am
- Location: Leeds, West Yorkshire, UK
- Contact:
Re: ZX81 BASIC
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.
HTH
Cheers,
Norm.
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.
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.
Author of Arduino Software Internals
Author of Arduino Interrupts
No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
Re: ZX81 BASIC
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