FAQ  •  Register  •  Login

values returned by comparations

<<

programandala.net

Posts: 86

Joined: Fri Apr 09, 2010 12:50 am

Location: España/Hispanujo/Spain

Post Mon Apr 19, 2010 7:52 pm

values returned by comparations

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?
<<

boriel

Site Admin

Posts: 1144

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Mon Apr 19, 2010 8:15 pm

Re: values returned by comparations

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)
<<

programandala.net

Posts: 86

Joined: Fri Apr 09, 2010 12:50 am

Location: España/Hispanujo/Spain

Post Mon Apr 19, 2010 9:22 pm

Re: values returned by comparations

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!
<<

programandala.net

Posts: 86

Joined: Fri Apr 09, 2010 12:50 am

Location: España/Hispanujo/Spain

Post Mon Apr 19, 2010 9:40 pm

Re: values returned by comparations

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" :)

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.
<<

boriel

Site Admin

Posts: 1144

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Thu Apr 29, 2010 10:35 am

Re: values returned by comparations

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 http://www.boriel.com/files/zxb 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.

Return to Bug Reports

Who is online

Users browsing this forum: No registered users and 0 guests

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums/DivisionCore.

phpBB SEO