IF INKEY-xy

bbc micro/electron/atom/risc os coding queries and routines
Post Reply
AJW
Posts: 906
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

IF INKEY-xy

Post by AJW » Sat Jul 04, 2020 9:07 pm

What is the machine code equivalent of IF INKEY-68 etc? I'm using:

Code: Select all

LDA#&81:LDX#1:LDY#0:JSR&FFF4 
but as I understand it this is roughly the same as A=INKEY(1) . Isn't INKEY-xy instant i.e. not 1 m/s delay?

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: IF INKEY-xy

Post by Richard Russell » Sat Jul 04, 2020 9:38 pm

AJW wrote:
Sat Jul 04, 2020 9:07 pm
What is the machine code equivalent of IF INKEY-68 etc? I'm using:

Code: Select all

LDA#&81:LDX#1:LDY#0:JSR&FFF4 
-68 is &FFBC so you should be loading Y with &FF and X with &BC.

AJW
Posts: 906
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: IF INKEY-xy

Post by AJW » Sat Jul 04, 2020 11:38 pm

I thought X and Y hold the delay time?

User avatar
jgharston
Posts: 4118
Joined: Thu Sep 24, 2009 12:22 pm
Location: Whitby/Sheffield
Contact:

Re: IF INKEY-xy

Post by jgharston » Sun Jul 05, 2020 12:17 am

AJW wrote:
Sat Jul 04, 2020 11:38 pm
I thought X and Y hold the delay time?
Yes, and if the delay time is negative.....

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.32
(C) Copyright J.G.Harston 1989,2005-2020
>_

AJW
Posts: 906
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: IF INKEY-xy

Post by AJW » Sun Jul 05, 2020 8:21 am

By -68 I mean a key number e.g.-68 for F, -69 for E.

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: IF INKEY-xy

Post by Richard Russell » Sun Jul 05, 2020 10:47 am

AJW wrote:
Sun Jul 05, 2020 8:21 am
By -68 I mean a key number e.g.-68 for F, -69 for E.
Yes. It's the parameter for INKEY which if positive is the maximum wait time and if negative is the key to be tested. Exactly the same is true in assembly language: the parameter goes into YX (as a 16-bit signed number) in that case, so again if it's positive it will set the wait time and if negative the key number. You are perhaps overthinking this: try doing what I suggested (set Y to &FF and X to &BC) and see what happens!

User avatar
1024MAK
Posts: 10287
Joined: Mon Apr 18, 2011 5:46 pm
Location: Looking forward to summer in Somerset, UK...
Contact:

Re: IF INKEY-xy

Post by 1024MAK » Sun Jul 05, 2020 11:31 am

As said many times before, BBC BASIC does not do any I/O itself, it just passes parameters to the OS. The OS does the I/O and then returns values to BASIC...

So calling the relevant OS routine via machine code is the same as doing the same operation with the relevant BASIC I/O command/function.

Mark

AJW
Posts: 906
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: IF INKEY-xy

Post by AJW » Sun Jul 05, 2020 1:28 pm

Richard Russell wrote:
Sun Jul 05, 2020 10:47 am
AJW wrote:
Sun Jul 05, 2020 8:21 am
By -68 I mean a key number e.g.-68 for F, -69 for E.
Yes. It's the parameter for INKEY which if positive is the maximum wait time and if negative is the key to be tested. Exactly the same is true in assembly language: the parameter goes into YX (as a 16-bit signed number) in that case, so again if it's positive it will set the wait time and if negative the key number. You are perhaps overthinking this: try doing what I suggested (set Y to &FF and X to &BC) and see what happens!
So now i have to convert all my keys into 2s complement 16bit hex .

e.g.

Code: Select all

2810 LDA#&81:LDX#1:LDY#0:JSR&FFF4 \inkey(1)
 2820 CPX#77:BNEek2:LDA#3:STA&8A:JSR updinput:LDA#1:STA&8B:RTS \m
 2830 .ek2 CPX#75:BNEek:LDA#4:STA&8A:JSR updinput:LDA#1:STA&8B:RTS \k
I assume EOR with 255 then add 1 to lower byte.

What is returned in A?

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: IF INKEY-xy

Post by Richard Russell » Sun Jul 05, 2020 1:47 pm

AJW wrote:
Sun Jul 05, 2020 1:28 pm
So now i have to convert all my keys into 2s complement 16bit hex .
No, you don't: the assembler will do that for you:

Code: Select all

      LDA#&81
      LDX#(-68 AND &FF)
      LDY#((-68 AND &FF00) / 256)
      JSR&FFF4 
It's one of the powerful features of BBC BASIC's assembler that the entire range of BASIC functions is available to you when computing the values of (for example) immediate constants, so you can often make your life easier and reduce the opportunity for errors by making use of this.
What is returned in A?
OSBYTE doesn't return the result in A, it returns it in X. See the documentation.

AJW
Posts: 906
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: IF INKEY-xy

Post by AJW » Sun Jul 05, 2020 2:01 pm

So X as I read it will hold the internal key number?

Clever use of the BASIC assembler, thanks.

User avatar
Richard Russell
Posts: 1668
Joined: Sun Feb 27, 2011 10:35 am
Location: Downham Market, Norfolk
Contact:

Re: IF INKEY-xy

Post by Richard Russell » Sun Jul 05, 2020 3:29 pm

AJW wrote:
Sun Jul 05, 2020 2:01 pm
So X as I read it will hold the internal key number?
As 1024MAK commented, the assembly language code does exactly the same thing as the BASIC INKEY() function. That returns zero (FALSE) if the key is not currently pressed and -1 (TRUE) if it is, so that's what X contains on return when you do it in assembler.

You can always experiment using BASIC code to find out what happens:

Code: Select all

      A% = 129
      X% = -68 AND &FF
      Y% = (-68 AND &FF00) / 256
      REPEAT
        PRINT ~USR(&FFF4)
      UNTIL FALSE
USR returns a 32-bit integer containing PYXA so you can see at a glance what is in each register.

AJW
Posts: 906
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: IF INKEY-xy

Post by AJW » Sun Jul 05, 2020 6:28 pm

Strange how I can do:

Code: Select all

LDX#(-102 AND&FF)
but not:

Code: Select all

CPX#(-1)
It has to be 255

User avatar
sweh
Posts: 2260
Joined: Sat Mar 10, 2012 12:05 pm
Location: New York, New York
Contact:

Re: IF INKEY-xy

Post by sweh » Sun Jul 05, 2020 7:06 pm

AJW wrote:
Sun Jul 05, 2020 6:28 pm
Strange how I can do:

Code: Select all

LDX#(-102 AND&FF)
but not:

Code: Select all

CPX#(-1)
It has to be 255
In both case the value has to be between 0 and 255 (inclusive) so the equivalent would be

Code: Select all

CPX#(-1 AND &FF)
Rgds
Stephen

Naomasa298
Posts: 391
Joined: Sat Feb 16, 2013 12:49 pm
Contact:

Re: IF INKEY-xy

Post by Naomasa298 » Sun Jul 05, 2020 7:09 pm

AJW wrote:
Sun Jul 05, 2020 6:28 pm
Strange how I can do:

Code: Select all

LDX#(-102 AND&FF)
but not:

Code: Select all

CPX#(-1)
It has to be 255
I assume this is because (-1) will be interpreted as a 32 bit number (&FFFFFFFF), which you cannot compare against. Convert it to an 8 bit number.

CPX#(-1 AND &FF), which is the same as CPX#255 (since &FFFFFFFF AND &FF = 255).

AJW
Posts: 906
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: IF INKEY-xy

Post by AJW » Sun Jul 26, 2020 7:30 pm

Apparently theres a way of peeking at &EC and ED that's hundres of cycles faster than Osbyte &81. Surprised noone mentioned it.

Post Reply

Return to “programming”