Omnimaga
Calculator Community => TI Calculators => ASM => Topic started 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 :)
-
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.
-
Define "best way". If you want the smallest solution, it's probably something like:
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:
ld h,doubleLUT >> 8
ld l,a
ld e,(hl)
inc h
ld d,(hl)
-
I didn't think about a lookup table. That's actually probably the best unless a faster one comes around :)
-
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):
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.)
-
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:
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.)
-
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.