PEEK with AND or OR

Started by brasspen, June 23, 2009, 01:41 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

brasspen

Hi All,

I'm reading the code for the game RATRUN. Spoke to Chris Nadovich, who wrote it the other day. It's the 3D maze game from CURSOR. He got the idea from visiting an IBM lab, saw the game running on a 3270 terminal on a mainframe, written in APL. Anyway...

Reading the code in Pet Fun And Games, I have two statements that confuse me:

POKE AV,PEEK(AV) OR 1

IF X<H AND (PEEK(AV) AND 2)

The first is odd, it seems to say put in there whatever is in there. If there is nothing there, a zero is there, then make it a 1. Or add a 1. Or something.

The second statement ... I have no idea how it's augmenting the PEEK'd value. I run tests and even if the value in the PEEK'd location is a number other than 0 or 32, I get a zero.

Any help would be appreciated. It's not clearly documented in my copy of PET BASIC: training your PET computer.

BigDumbDinosaur

Quote from: brasspen on June 23, 2009, 01:41 PM
Hi All,

I'm reading the code for the game RATRUN. Spoke to Chris Nadovich, who wrote it the other day. It's the 3D maze game from CURSOR. He got the idea from visiting an IBM lab, saw the game running on a 3270 terminal on a mainframe, written in APL. Anyway...

Reading the code in Pet Fun And Games, I have two statements that confuse me:

POKE AV,PEEK(AV) OR 1

IF X<H AND (PEEK(AV) AND 2)

The first is odd, it seems to say put in there whatever is in there. If there is nothing there, a zero is there, then make it a 1. Or add a 1. Or something.

The second statement ... I have no idea how it's augmenting the PEEK'd value. I run tests and even if the value in the PEEK'd location is a number other than 0 or 32, I get a zero.

Any help would be appreciated. It's not clearly documented in my copy of PET BASIC: training your PET computer.
POKE AV,PEEK(AV) OR 1 is setting bit zero of whatever happens to be in location AV.  It's a simple Boolean operation.  in machine code it would be written as:

lda AV
ora #%00000001
sta AV



IF X<H AND (PEEK(AV) AND 2) might be easier to understand if rewritten thusly:

IF (X<H) AND (PEEK(AV) AND 2)

First the X<H expression is evaluated.  It will produce 1 if X is less than H or 0 (zero) otherwise.  PEEK(AV) AND 2 will evaluate to 0 if bit 1 of the value stored in AV is clear.  Otherwise, the expression will evaluate to 1.  Therefore, read it as follows: (if X is less than H) AND (bit 1 of the value in AV is set) then the result of the expression (X<H) AND (PEEK(AV) AND 2) will be true (1) and the IF will succeed.

What is being done isn't to get the actual value in location AV but to determine if a particular bit (bit 1 in this case) is set or clear.  In such a case, the Boolean result is wanted, which will be true (1) or false (0).
x86?  We ain't got no x86.  We don't need no stinking x86!

brasspen

Thanks, I think I'm getting the idea. I wouldn't have got it without your post. I was looking at BASIC, and it's working at a lower, more assembler level. It looks like it's time to pull Butterfield's "Machine Language" down off the shelf.

BigDumbDinosaur

Quote from: brasspen on June 24, 2009, 10:39 AM
Thanks, I think I'm getting the idea. I wouldn't have got it without your post. I was looking at BASIC, and it's working at a lower, more assembler level. It looks like it's time to pull Butterfield's "Machine Language" down off the shelf.
You can't go wrong with one of the master's books.  :)
x86?  We ain't got no x86.  We don't need no stinking x86!

Hydrophilic

BDD gave a pretty good explaination, but I feel obliged to point out that the comparison X<H (or any comparison evaluated as a number) returns NEGATIVE 1 when true. 

Why negative one?  Because it equals binary 1111111111111111, the exact opposite of zero (false).
I'm kupo for kupo nuts!