Yeah, i try to avoid IF..THEN wherever I can, especially if it requires an 
extra line.

It does give some strange-looking code though, like line 40 in the HEX to 
decimal conversion in my dump program that conditionally converts the from/to 
address digits to upper case if necessary by subtracting 32 (a simple AND 95 
would probably have been simpler ;-) )

Convert up to 4 HEX digits in X$ to decimal equivalent in X:

20 X=0:X$=RIGHT$("0000"+X$,4)
30 FORI=1TO4:Y=ASC(MID$(X$,I,1))
40 Y$=CHR$(Y+(Y>95)*32)
50 L=INSTR(1,H,Y$)-1:X=X+L*(16^(4-I))

  ----- Original Message ----- 
  From: John R. Hogerhuis 
  To: [email protected] 
  Sent: Thursday, October 27, 2022 5:19 PM
  Subject: Re: [M100] Compute a 16-bit signed value in BASIC


  I was curious if loading a string pointer could be done without an explicit 
IF.


  Seems like what we're doing is J as LSB, K as MSB

  K>127: J + K*256-65536
  or
  K<=127: J + K*256 - 0


  Taking J + K * 256 - 65536


  factoring out 256 and putting both functions in a similar form:

  K > 127: J + 256 ( K - 256)
  K <= 127: J + 256 ( K - 0 )

  Knowing comparisons can be done without 'IF'... if an expression is true you 
get -1 and 0 if false, we can inline the conditional into the expression 

  So the computation without IF would be

  J + 256% * ( K + 256% * (K > 127%))

  So something like this:

  10 DEFINTP:DEFSTRH
  20 H=CHR$(201)
  30 P=VARPTR(H)+1
  1000 P=PEEK(P)+256%*(PEEK(P+1)+256%*(PEEK(P+1)>127))
  1010 PRINTP


  Not sure if this works, particularly with the integer constants. And there 
might be speed optimizations... certainly it is doing the MSB peek twice.


  -- John.

Reply via email to