Re: [M100] Compute a 16-bit signed value in BASIC

2022-10-27 Thread MikeS
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$(""+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: m...@bitchin100.com 
  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.

Re: [M100] Compute a 16-bit signed value in BASIC

2022-10-27 Thread John R. Hogerhuis
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.


Re: [M100] Compute a 16-bit signed value in BASIC

2022-10-27 Thread Ken Pettit

On 10/26/22 11:50 PM, John R. Hogerhuis wrote:



On Wed, Oct 26, 2022 at 4:57 PM Ken Pettit > wrote:



You have to make it a negative number by subtracting it from 65536:

20 H$="0123456789ABCDEF":

D=VARPTR(H$)+1:IFPEEK(D+1)>127THENC=65536-256*PEEK(D+1)-PEEK(D)ELSEC=PEEK(D)+256*PEEK(D+1)



Thanks! Makes sense.

Pity about the conditional.



Yep.  And actually, that should have been C=256*PEEK(D+1)+PEEK(D) - 
65536, otherwise it is just a different positive value.


Ken


Re: [M100] Compute a 16-bit signed value in BASIC

2022-10-26 Thread John R. Hogerhuis
On Wed, Oct 26, 2022 at 4:57 PM Ken Pettit  wrote:

>
> You have to make it a negative number by subtracting it from 65536:
>
> 20 H$="0123456789ABCDEF":
>
> D=VARPTR(H$)+1:IFPEEK(D+1)>127THENC=65536-256*PEEK(D+1)-PEEK(D)ELSEC=PEEK(D)+256*PEEK(D+1)
>
>
>
Thanks! Makes sense.

Pity about the conditional.

-- John.


Re: [M100] Compute a 16-bit signed value in BASIC

2022-10-26 Thread Ken Pettit

On 10/26/22 4:33 PM, John R. Hogerhuis wrote:

Anyone know how to compute a 16-bit integer address without overflow?

Trying to get the address of a string H$ into a variable C that I can 
PEEK. The first byte of a string object is the length. It's followed 
by a pointer to the string


10 DEFINTA-F
20 H$="0123456789ABCDEF": D=VARPTR(H$)+1:C=PEEK(D)+256*PEEK(D+1)


You have to make it a negative number by subtracting it from 65536:

20 H$="0123456789ABCDEF": 
D=VARPTR(H$)+1:IFPEEK(D+1)>127THENC=65536-256*PEEK(D+1)-PEEK(D)ELSEC=PEEK(D)+256*PEEK(D+1)





No workey... overflow error.

What is happening is that PEEK(D+1) is 128. 256 * 128 = 32768 which is 
too big to be stored in C% (a 16-bit signed integer)


So given a number 0-65535 is there a good way in BASIC to get the 
16-bit signed value -32768..32767?


FWIW, AND and NOT only work on signed 16-bit values, not > 32767 
positive values.


-- John.




[M100] Compute a 16-bit signed value in BASIC

2022-10-26 Thread John R. Hogerhuis
Anyone know how to compute a 16-bit integer address without overflow?

Trying to get the address of a string H$ into a variable C that I can PEEK.
The first byte of a string object is the length. It's followed by a pointer
to the string

10 DEFINTA-F
20 H$="0123456789ABCDEF": D=VARPTR(H$)+1:C=PEEK(D)+256*PEEK(D+1)

No workey... overflow error.

What is happening is that PEEK(D+1) is 128. 256 * 128 = 32768 which is too
big to be stored in C% (a 16-bit signed integer)

So given a number 0-65535 is there a good way in BASIC to get the 16-bit
signed value -32768..32767?

FWIW, AND and NOT only work on signed 16-bit values, not > 32767 positive
values.

-- John.