Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: Xeda112358 on January 15, 2012, 09:51:37 pm

Title: Comparing bits in a register
Post by: Xeda112358 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.
Title: Re: Comparing bits in a register
Post by: Hot_Dog 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

Title: Re: Comparing bits in a register
Post by: Xeda112358 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.
Title: Re: Comparing bits in a register
Post by: Hot_Dog on January 15, 2012, 10:05:17 pm
So you want to know if bits 5, 4, 1 and 0 are all alike?
Title: Re: Comparing bits in a register
Post by: Xeda112358 on January 15, 2012, 10:06:47 pm
I just want to know if bit 5 == bit 1 and bit 4 == bit 0.
Title: Re: Comparing bits in a register
Post by: calc84maniac 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
Title: Re: Comparing bits in a register
Post by: Xeda112358 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!
Title: Re: Comparing bits in a register
Post by: calc84maniac 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.
Title: Re: Comparing bits in a register
Post by: Xeda112358 on January 15, 2012, 10:19:56 pm
oh, right XD Thanks much, this is very nice!
Title: Re: Comparing bits in a register
Post by: calc84maniac 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
Title: Re: Comparing bits in a register
Post by: Xeda112358 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!