Author Topic: Comparing bits in a register  (Read 3112 times)

0 Members and 1 Guest are viewing this topic.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Comparing bits in a register
« on: January 15, 2012, 09:51:37 pm »
I want to compare bit 5 to bit 1 and bit 4 to bit 0 in the A register (the other bits are always 0). in other words, I want to xor the two nibbles of A or something similar, but as fast as possible. To start, here is one idea:
Code: [Select]
     ld c,a
     and $F0
     sla c
     sla c
     sla c
     sla c
     xor c
That is 47 cycles, 12 bytes-- too big/slow/inelegant for me to accept as the best method.

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Comparing bits in a register
« Reply #1 on: January 15, 2012, 10:00:46 pm »
I don't quite understand how XORing will help you compare, but here's one (8 bytes, 31 cycles)

Code: [Select]

  ld c, a
  rrca
  rrca
  rrca
  rrca
  xor c
  and $F0


Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Comparing bits in a register
« Reply #2 on: January 15, 2012, 10:02:47 pm »
Thanks! I still wonder if there is some crazy method available, but that is a lot better!

I just want to check if they are the same or not. If not, the XOR (or in your case, the AND) will return nz whereas if they are the same, it will return z.

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Comparing bits in a register
« Reply #3 on: January 15, 2012, 10:05:17 pm »
So you want to know if bits 5, 4, 1 and 0 are all alike?

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Comparing bits in a register
« Reply #4 on: January 15, 2012, 10:06:47 pm »
I just want to know if bit 5 == bit 1 and bit 4 == bit 0.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Comparing bits in a register
« Reply #5 on: January 15, 2012, 10:14:33 pm »
and $00110011
jp po,bits_different
and $00010001
jp po,bits_different


If the other bits are already 0, you can go with this:
or a
jp po,bits_different
and $00010001
jp po,bits_different
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Comparing bits in a register
« Reply #6 on: January 15, 2012, 10:16:04 pm »
For the second one, the OR A would not recognise if a was %00110000, right? But the first one looks a lot more beautiful, thanks!

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Comparing bits in a register
« Reply #7 on: January 15, 2012, 10:17:52 pm »
For the second one, the OR A would not recognise if a was %00110000, right? But the first one looks a lot more beautiful, thanks!
The parity flag would be the same either way because it is based on the final result in A. That would indeed return PE, and go through to the second case where it returns PO.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Comparing bits in a register
« Reply #8 on: January 15, 2012, 10:19:56 pm »
oh, right XD Thanks much, this is very nice!

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Comparing bits in a register
« Reply #9 on: January 15, 2012, 10:24:29 pm »
Hmm, and I guess Hot_Dog's method could be slightly improved by doing this (mainly a size optimization over mine because mine has an "early out")

  ld c, a
  rrca
  rrca
  rrca
  rrca
  xor c
  jr nz,bits_different
« Last Edit: January 15, 2012, 10:24:42 pm by calc84maniac »
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Comparing bits in a register
« Reply #10 on: January 15, 2012, 10:26:23 pm »
So that is slightly slower, but I see how that works! These are great, thanks much, guys!