Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
values returned by comparations
#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


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)