Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
values returned by comparations
#1
I realized that some comparations with string lenghts return different values than in Sinclar Basic:

Code:
dim text as string
let text="A"
print len(text)=1 ' prints 0!
print len(text)=len(text) ' prints 255!
print 2=2 ' prints 1 as expected

Why?
Reply
#2
programandala.net Wrote:I realized that some comparations with string lenghts return different values than in Sinclar Basic:

Code:
10 dim text as string
20 let text="A"
30 print len(text)=1 ' prints 0!
40 print len(text)=len(text) ' prints 255!
50 print 2=2 ' prints 1 as expected

Why?
Note: Line 30 prints 255. In fact TRUE is "NON ZERO VALUE". False is 0. 255 stands for -1 (Byte type) actually. An it's much easier to manage -1 in asm than 1 :!: ZX BASIC uses A and H registers for 8 bits (Byte, Ubyte, Boolean) values.
In assembler, AND mnemonic is bitwise. This means that 128 AND 1 == 0 (and should return 1, thus TRUE). We have to convert numerical values to booleans. But we also want speed and memory saving, so ... look:

For example, the following ASM listing computes A and B provided both A and B are 8bit values, returning always 0 (FALSE) or 1 (TRUE)
Code:
ld l, a
    or a
    jr z, _NEXT
    ld l, 1
_NEXT: ; At this point l = 0 or 1
    ld a, h
    or a
    jr z, _NEXT2
    ld a, 1
_NEXT2:
    and h
    ret

LENGTH: 13 bytes, T-States: 54 / 58

This one uses "Any value" as TRUE:
Code:
dec a
    sbc a, a
    ld l, a
    ld a, h
    dec a
    sbc a, a
    and l
    cpl
    ret
This code is shorter and faster (7 bytes and 42 T-States).

Code:
or a
   ret z
   ld a, h
   ret
4 bytes and 15-28 T-States: Shortest and fastest!

The same applies to the other boolean operations. If you REALLY need 0 and 1, you can convert a boolean result using SGN:
Code:
10 dim text as string
20 let text="A"
30 print sgn(len(text)=1) ' prints 1!
40 print sgn(len(text)=len(text)) ' prints 1!
50 print 2=2 ' prints 1 as expected
Update: Maybe another --strict-boolean flag? So (x == y) always = [0, 1] (Slower but compatible)
Reply
#3
programandala.net Wrote:
Code:
print len(text)=1 ' prints 0!

It prints 255, not 0. That was my fault, sorry. The original test had a former line that modified the variable:

Code:
let text=text( to len(text)-2+(len(text)=1)) ' doesn't work because:
print len(text)=1 ' prints 0!
Reply
#4
boriel Wrote:The same applies to the other boolean operations. If you REALLY need 0 and 1, you can convert a boolean result using SGN:

Thank you for the detailed explanation (I love Z80 assembler). In the future I'll check the boolean operations without "Sinclair Basic assumptions" Smile

boriel Wrote:Maybe another --strict-boolean flag? So (x == y) always = [0, 1] (Slower but compatible)

I see there are many little but important differences with Sinclair Basic. Some of them are not evident. I think only very simple programs, like the provided examples, can be ported without modifications. Some compatibility options would be great, to let more complex programs to be compiled without rewriting. I am not interested in compiling Sinclar Basic code, but in writing ZX Basic, though I'm learning the language by porting a bit complex Sinclair Basic program of mine. That's a good and fun way to learn.
Reply
#5
Ok, I've just upgraded and uploaded a new version: ZXB 1.2.6-r1558 (Download 1.2.6 release - either .msi, zip or tar.gz file at <!-- m --><a class="postlink" href="http://www.boriel.com/files/zxb">http://www.boriel.com/files/zxb</a><!-- m --> as always). This version allows strict 0/1 boolean values if you compile with --strict-boolean flag.
Compile your test program both with and without --strict-boolean flag and see what happens.

NOTE: using --sinclair also enables --strict-boolean, since --sinclair implies "maximum compatibility"

It also enables out of memory checking with --debug-memory. Try this program:
Code:
10 LET A$ = ""
20 LET A$ = A$ + " "
30 GOTO 20
compile this program both with and without --debug-memory flag and see what happens.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)