Bits and bytes

bbc micro/electron/atom/risc os coding queries and routines
Post Reply
fuzzel
Posts: 651
Joined: Sun Jan 02, 2005 1:16 pm
Location: Cullercoats, North Tyneside
Contact:

Bits and bytes

Post by fuzzel » Sun Aug 04, 2019 11:07 am

Following on from my previous topic, using my new compression technique with an extra 250 compressed "words" I've been able to free up some space in my adventure for a short location description which will be quoted before the main body of location text. The plan is to have a BRIEF command whereby only this is printed if you want to move around quickly without seeing the full text displayed each time (VERBOSE will be the opposite). I'll probably also only print the brief description once you've visited the room once. I'm also going to add an extra byte per location and with this I'd like to store a number of flags which can either be true or false:
room visited: Y/N
room naturally light / dark: Y/N
room large / small (can't drop things here): Y/N
dead room (die on visiting): Y/N
dry / wet room (need a boat to visit for wet rooms otherwise drown): Y/N
Directions viewable from here (for EXITS command): Y/N - the short location description will be used for this command.

Is it possible to do all this with one byte, i.e. can I test a bit for a 0 or 1 value ? Also, how do I set the bit to 0 or 1 ?

cmorley
Posts: 1293
Joined: Sat Jul 30, 2016 8:11 pm
Location: Oxford
Contact:

Re: Bits and bytes

Post by cmorley » Sun Aug 04, 2019 11:22 am

Sounds reasonable.

Research the ? BASIC byte memory operator. If you DIM some memory then you can access it with ?

Code: Select all

DIM mem 256
I%=25
mem?I%=&FF
P.mem?I%
For the bit flags you should look at making some functions (DEFFN) to check/set the bits with AND & OR operators.

fuzzel
Posts: 651
Joined: Sun Jan 02, 2005 1:16 pm
Location: Cullercoats, North Tyneside
Contact:

Re: Bits and bytes

Post by fuzzel » Sun Aug 04, 2019 12:48 pm

Thanks. My game's in assembly language, are there commands in that which would do the job ?

User avatar
lurkio
Posts: 2851
Joined: Wed Apr 10, 2013 12:30 am
Location: Doomawangara
Contact:

Re: Bits and bytes

Post by lurkio » Sun Aug 04, 2019 12:59 pm

Here's a not-particularly-great BASIC version, but it seems to work:

Code: Select all

 10 PRINT"  Get bit 1 of byte-value   2: ";FNgetBit(1,2)
 20 PRINT"  Get bit 0 of byte-value   2: ";FNgetBit(0,2)
 30 :
 40 PRINT"  Set bit 0 of byte-value   2: ";FNsetBit(0,2)
 50 PRINT"Clear bit 0 of byte-value   3: ";FNclrBit(0,3)
 60 :
 70 PRINT"  Set bit 7 of byte-value   0: ";FNsetBit(7,0)
 80 PRINT"Clear bit 3 of byte-value 255: ";FNclrBit(3,&FF)'
 90 :
100 DIM m% 0
110 ?m%=5
120 PRINT"?m%=5"
130 PRINT"  Get bit 1 of ?m%: ";FNgetBit(1,?m%)
140 PRINT"  Get bit 0 of ?m%: ";FNgetBit(0,?m%)
150 PRINT"  Set bit 1 of ?m%: ";:?m%=FNsetBit(1,?m%)
160 PRINT;?m%
170 PRINT"Clear bit 2 of ?m%: ";:?m%=FNclrBit(2,?m%)
180 PRINT;?m%'
190 :
200 END
210 :
220 DEFFNgetBit(b%,byte%)
230 IF (byte% AND 2^b%)>0 THEN =1 ELSE =0
240 :
250 DEFFNsetBit(b%,byte%)
260 =byte% OR 2^b% 
270 :
280 DEFFNclrBit(b%,byte%)
290 =byte% AND (&FF - 2^b%)
Result:

Code: Select all

  Get bit 1 of byte-value   2: 1
  Get bit 0 of byte-value   2: 0
  Set bit 0 of byte-value   2: 3
Clear bit 0 of byte-value   3: 2
  Set bit 7 of byte-value   0: 128
Clear bit 3 of byte-value 255: 247

?m%=5
  Get bit 1 of ?m%: 0
  Get bit 0 of ?m%: 1
  Set bit 1 of ?m%: 7
Clear bit 2 of ?m%: 3
Perhaps you can derive a machine-code version from that? Assembler has AND, OR and EOR operators functions, as I recall -- though I have a feeling you might need to use ROL as well...

:?:
Last edited by lurkio on Sun Aug 04, 2019 3:41 pm, edited 1 time in total.

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

Re: Bits and bytes

Post by sweh » Sun Aug 04, 2019 1:05 pm

In assembler you define bitmasks and then use the "OR" and "AND" logical functions.

eg

Code: Select all

room_visited = &1  \bit0 stores room visited flag
room_dark    = &2  \bit1 stored room naturally dark

	\ To set room_visited
	LDA #room_visited
	ORA status_byte
	STA status_byte
	
	\ To clear room_dark
	LDA #room_dark
	EOR #&FF
	AND status_byte
	STA status_byte
	
	\ To test room_dark
	LDA #room_dark
	AND status_byte
	BNE room_dark
	\ room is naturally light
Last edited by sweh on Sun Aug 04, 2019 1:10 pm, edited 2 times in total.
Rgds
Stephen

fuzzel
Posts: 651
Joined: Sun Jan 02, 2005 1:16 pm
Location: Cullercoats, North Tyneside
Contact:

Re: Bits and bytes

Post by fuzzel » Sun Aug 04, 2019 1:58 pm

That's excellent help thanks. I shall mess around with this and report back.
The game's coming along nicely btw, I will post an update in the "Software New Games - Land a multi-user dungeon (well almost)" topic shortly.

User avatar
MartinB
Posts: 5328
Joined: Mon Mar 31, 2008 10:04 pm
Location: Obscurity
Contact:

Re: Bits and bytes

Post by MartinB » Sun Aug 04, 2019 5:25 pm

Have a look at the BIT instruction too which is specifically designed for testing individual bits as you require. It’s advantage is that it is non-destructive, only setting status flags and so is excellent for iterative bit-testing of a sequence of bytes.....


EF86AF2F-8524-4618-BA4B-B7A3E8EF72AC.jpeg

Post Reply

Return to “programming”