Author Topic: Duplicating Bits in Register A  (Read 3793 times)

0 Members and 1 Guest are viewing this topic.

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Duplicating Bits in Register A
« 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 :)
« Last Edit: October 08, 2011, 03:21:39 pm by Hot_Dog »

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Duplicating Bits in Register A
« Reply #1 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.
« Last Edit: October 08, 2011, 05:19:48 pm by Deep Thought »




Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Duplicating Bits in Register A
« Reply #2 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)
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: Duplicating Bits in Register A
« Reply #3 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 :)

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Duplicating Bits in Register A
« Reply #4 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.)
« Last Edit: October 08, 2011, 07:09:17 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Duplicating Bits in Register A
« Reply #5 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.)
« Last Edit: October 08, 2011, 07:40:21 pm by thepenguin77 »
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Duplicating Bits in Register A
« Reply #6 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.
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.