Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: Hot_Dog on October 08, 2011, 03:08:16 pm

Title: Duplicating Bits in Register A
Post by: Hot_Dog on October 08, 2011, 03:08:16 pm
If I have a number stored in Register A, such as %01110010, what's the best way to turn it into %0011111100001100?  Meaning every bit in the register is doubled?  I don't care what the output register (or variable) is :)
Title: Re: Duplicating Bits in Register A
Post by: Deep Toaster on October 08, 2011, 04:27:16 pm
I once had to do that, but ended up just looping through it and copying each bit twice. (When I asked Kerm he couldn't think of any alternatives at the mement either.) Someone out of the people viewing this topic is probably gonna come up with some brilliant routine to do this, but I have no idea.
Title: Re: Duplicating Bits in Register A
Post by: calc84maniac on October 08, 2011, 04:28:13 pm
Define "best way". If you want the smallest solution, it's probably something like:
Code: [Select]
  ld b,8
doubleloop:
  rlca
  adc hl,hl
  rrca
  adc hl,hl
  rlca
  djnz doubleloop

The fastest solution would be to use a lookup table, like so:
Code: [Select]
  ld h,doubleLUT >> 8
  ld l,a
  ld e,(hl)
  inc h
  ld d,(hl)
Title: Re: Duplicating Bits in Register A
Post by: Hot_Dog on October 08, 2011, 05:17:26 pm
I didn't think about a lookup table.  That's actually probably the best unless a faster one comes around :)
Title: Re: Duplicating Bits in Register A
Post by: calcdude84se on October 08, 2011, 07:08:24 pm
calc84's is broken as far as I can tell because it neglects that when "duplicated" A is two bytes. (You also need to 256-byte align the table.)
A fixed version (which destroys A):
Code: [Select]
ld h,LUT>>8
add a,a
jr nc,$+3
inc h
ld e,(hl)
inc hl
ld d,(hl)
Edit: In general, except for the simplest of processes, look-up tables tend to be the fastest method. (They are also the most space-consuming.)
Title: Re: Duplicating Bits in Register A
Post by: thepenguin77 on October 08, 2011, 07:38:57 pm
calc84's is broken as far as I can tell because it neglects that when "duplicated" A is two bytes. (You also need to 256-byte align the table.)

I thought that same thing at first, however:

Code: [Select]
  inc h

He actually did it right. He just didn't order the LUT the way you expected him to ;D (Or for that matter, the way any other person would.)
Title: Re: Duplicating Bits in Register A
Post by: calcdude84se on October 09, 2011, 03:19:23 pm
Nice catch. And a more efficient way to order it, too. I shall have to remember that. Perhaps I thought I had seen "inc hl" instead of "inc h". * calcdude shrugs.