Finding LSB in a register

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Kweepa
Manic Miner
Posts: 215
Joined: Sat Feb 03, 2018 6:14 pm
Location: Austin, Texas

Re: Finding LSB in a register

Post by Kweepa » Thu Jan 17, 2019 9:47 pm

Mine is 13 bytes (checked with www.asm80.com, which is awesome!).
Metalbrain's is 12 bytes, if you remove the ret c and replace ld hl,0 with xor h/xor l.
0 x

User avatar
djnzx48
Manic Miner
Posts: 536
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Finding LSB in a register

Post by djnzx48 » Thu Jan 17, 2019 9:54 pm

Could you replace XOR 255 with CPL and get it down to 11 bytes?
1 x

Kweepa
Manic Miner
Posts: 215
Joined: Sat Feb 03, 2018 6:14 pm
Location: Austin, Texas

Re: Finding LSB in a register

Post by Kweepa » Thu Jan 17, 2019 9:58 pm

Yep, that works :)
Didn't know about that instruction!
0 x

User avatar
djnzx48
Manic Miner
Posts: 536
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Finding LSB in a register

Post by djnzx48 » Fri Jan 18, 2019 12:32 am

If you're doing regular AND-then-OR masking, you can just use OR B instead of CPL: AND B (and the same for C) as it doesn't matter what the mask is when the pixel value is set. That would get it down to 9 bytes.

(EDIT: Actually in that case all you need to create the mask is a single DEC instruction. So this method only requires one byte.)

Maybe other types of masking would work, for instance OR for the mask and then XOR for the sprite. Are you doing the same thing to create a mask on the left using the MSB?
1 x

User avatar
Metalbrain
Dizzy
Posts: 80
Joined: Thu Feb 15, 2018 2:14 pm

Re: Finding LSB in a register

Post by Metalbrain » Fri Jan 18, 2019 7:58 am

Kweepa wrote:
Thu Jan 17, 2019 9:47 pm
and replace ld hl,0 with xor h/xor l.
xor h/xor l wouldn't set hl to 0, they would just make A = A xor H xor L
0 x

User avatar
arkannoyed
Manic Miner
Posts: 365
Joined: Mon Feb 05, 2018 9:56 am

Re: Finding LSB in a register

Post by arkannoyed » Fri Jan 18, 2019 9:16 am

Wow, some great ideas, and it certainly looks as if the 8 bit logic methods will win. I'll have a go with some ideas later and see what works best.

Lots of re-designing to do to make the printing byte-wise work as opposed to bit-wise, which is how the Chess pieces are done currently. The speed improvement will be huge though, and hopefully might lose a few bytes as well as a bonus!
0 x

User avatar
lister_of_smeg
Berk
Posts: 37
Joined: Thu Nov 16, 2017 1:44 pm

Re: Finding LSB in a register

Post by lister_of_smeg » Fri Jan 18, 2019 9:42 am

Kweepa wrote:
Thu Jan 17, 2019 3:49 pm
No loops:
ld bc,hl
dec bc
ld a,h
xor 255
and b
ld b,a
ld a,l
xor 255
and c
ld c,a
Just uses HL & BC
ld bc,hl isn't a valid z80 instruction
Last edited by lister_of_smeg on Fri Jan 18, 2019 9:48 am, edited 2 times in total.
0 x

User avatar
arkannoyed
Manic Miner
Posts: 365
Joined: Mon Feb 05, 2018 9:56 am

Re: Finding LSB in a register

Post by arkannoyed » Fri Jan 18, 2019 9:46 am

yes, but ld B,H and ld C,L are, which does the same thing.
0 x

User avatar
arkannoyed
Manic Miner
Posts: 365
Joined: Mon Feb 05, 2018 9:56 am

Re: Finding LSB in a register

Post by arkannoyed » Fri Jan 18, 2019 2:01 pm

Seems to almost do what it needs to. The only thing left that I should've mentioned before is that it needs to RESet the LSB in HL (the data register).
So whereas maybe HL=05c0h, after creating the mask, bit 6 of L needs to be cleared, giving the result HL=0580h.

Does that make sense?
0 x

Kweepa
Manic Miner
Posts: 215
Joined: Sat Feb 03, 2018 6:14 pm
Location: Austin, Texas

Re: Finding LSB in a register

Post by Kweepa » Fri Jan 18, 2019 3:19 pm

arkannoyed wrote:
Fri Jan 18, 2019 9:46 am
yes, but ld B,H and ld C,L are, which does the same thing.
Wow, apparently asm80.com is too clever for its own good - it automatically converted ld bc,hl to ld b,h/ld c,l!
0 x

Post Reply