Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: ralphdspam on August 25, 2011, 09:06:44 pm

Title: Constructive and Creative uses of RLD and RRD
Post by: ralphdspam on August 25, 2011, 09:06:44 pm
I saw the topic "Constructive and Creative uses of IX and IY," and I liked the idea of brainstorming creative uses for commands. 

Two commands that always perplex me are RLD and RRD.  I cannot think of many practical uses of them. 
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: calc84maniac on August 25, 2011, 11:11:00 pm
I use one of those in TI-Boy to emulate the SWAP (HL) instruction, which swaps the upper and lower nibbles of (HL).
It works like ld a,(hl) \ rrd.
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: DrDnar on August 26, 2011, 02:20:18 am
As far as I can tell, the instructions are intended for use with BCD. They appear to allow multiplying and dividing by ten. Or sixteen, if you're using binary.
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: thepenguin77 on August 26, 2011, 03:12:08 pm
Indeed, the only time I've ever used it was for BCD. It's useful if you need to recover a number from TI's floating points. For instance:

Code: [Select]
turnTheEntireOp1ToADecimalStringRegardlessOfSignificateFigures:
ld hl, op1+2
ld c, 7
outerLoop:
ld b, 2
innerLoop:
ld a, $30
rld
ld (de), a
inc de
djnz innerLoop
inc hl
dec c
jr nz, outerLoop
xor a
ld (de), a
ret
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: ralphdspam on August 26, 2011, 06:01:52 pm
I'm glad there are actually uses for that command.  I like the idea of manipulating nibbles, but I don't like the fact that it modifies the source data.  Can you use it on ROM code without any problems? 
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: calcdude84se on August 26, 2011, 09:01:12 pm
I'm glad there are actually uses for that command.  I like the idea of manipulating nibbles, but I don't like the fact that it modifies the source data.  Can you use it on ROM code without any problems? 
You probably shouldn't be manipulating code by nibble :P Anyway, A contains the correct result even if (HL) can't be changed, IIRC.
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: calc84maniac on August 26, 2011, 10:30:15 pm
/me uses OPTIMIZATION
Code: [Select]
turnTheEntireOp1ToADecimalStringRegardlessOfSignificateFigures:
ld hl, op1+2
ld b, 7
ld a, $30
loop:
rld
ld (de), a
inc de
rld
ld (de), a
inc de
inc hl
djnz loop
xor a
ld (de), a
ret

Edit:
Ooh, even better!
Code: [Select]
turnTheEntireOp1ToADecimalStringRegardlessOfSignificateFigures:
ld hl, op1+2
ld bc, 7
loop:
ld a, $33
rrd
ldi
ld (de), a
inc de
jp pe, loop
xor a
ld (de), a
ret
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: Xeda112358 on December 30, 2011, 11:40:38 pm
Ooh, rrd and rld are some of my favorite instructions! When I was describing what it does in my pocket asm tutorial was this:
Quote
rrd/rld the two nibbles from (hl) and the last nibble of 'a' make three nibbles. rrd                
           rotates them right and rld rotates them left.
As for a way to use them, I have a few codes that make excellent use of them. This one is from my program BSPRT. It uses hexadecimal sprite data, so instead of converting the hex string to a location in RAM and then copying it to the screen, I just did both at once (here is the sprite display part):
Code: (BSPRT) [Select]
;Inputs:
;     HL points to the buffer location to draw to
;     DE points to the sprite data

     ld bc,080Ch     ;010C08
Loop:
     call PutNibble  ;CD****
     call PutNibble  ;CD****
     ld a,b          ;78
     ld b,0          ;0600
     add hl,bc       ;09
     ld b,a          ;47
     djnz Loop       ;10F3
     ret             ;C9
PutNibble:
     ld a,(de)       ;1A
     add $C0         ;C6C0
     jr nc,$+4       ;3002
       sub 7         ;D607
     rld             ;ED6F
     inc de          ;13
     ret             ;C9
This is my favorite one, though. When I made a graph shifting routine, I made these to shift 4 pixels very quickly:
Code: [Select]
ShiftLeft4:
     ld hl,GraphBuf+767
     ld c,64
       xor a
       ld b,12
         rld
         dec hl
         djnz $-3
       dec c
       jr nz,$-9
       ret
Code: [Select]
ShiftRight4:
     ld hl,GraphBuf
     ld c,64
       xor a
       ld b,12
         rrd
         inc hl
         djnz $-3
       dec c
       jr nz,$-9
       ret
I feel pretty proud of these :) I think the first can be optimised more, but it is still nice, I think :)
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: chickendude on December 31, 2011, 05:32:36 am
Unless i'm missing something, the first just says "Array", maybe you pasted the wrong thing?
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: Hayleia on December 31, 2011, 06:49:29 am
It is because she wrote [code=something].
Here is what she meant :)

Code: [Select]
;Inputs:
;     HL points to the buffer location to draw to
;     DE points to the sprite data

     ld bc,080Ch     ;010C08
Loop:
     call PutNibble  ;CD****
     call PutNibble  ;CD****
     ld a,b          ;78
     ld b,0          ;0600
     add hl,bc       ;09
     ld b,a          ;47
     djnz Loop       ;10F3
     ret             ;C9
PutNibble:
     ld a,(de)       ;1A
     add $C0         ;C6C0
     jr nc,$+4       ;3002
       sub 7         ;D607
     rld             ;ED6F
     inc de          ;13
     ret             ;C9
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: Xeda112358 on December 31, 2011, 10:21:06 am
Unless i'm missing something, the first just says "Array", maybe you pasted the wrong thing?
I am not seeing that... On mine, Hayleia and I have the same thing...
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: Hayleia on December 31, 2011, 10:23:43 am
Unless i'm missing something, the first just says "Array", maybe you pasted the wrong thing?
I am not seeing that... On mine, Hayleia and I have the same thing...
??? I see "Array" too on yours. Maybe it depends on the browser.
But do you see "BSPRT" next to "code" ? (I don't see it, I had to quote you to see it should be here ;))
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: chickendude on December 31, 2011, 11:00:35 am
Well, it doesn't really matter now as i see what code you were talking about in Hayleia's post. Btw, what exactly do you mean by "hexidecimal sprites"? I'm trying to figure out what exactly your code does.
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: Xeda112358 on December 31, 2011, 02:10:01 pm
Ah, well with that code, will draw a sprite with hexadecimal data input. By that, I mean . db "3C4281818181423C" as opposed to .db 3Ch,42h,81h,81h,81h,81h,42h,3Ch

The first is what a TI-BASIC string would be like and the program that code is in is designed for TI-BASIC programmers to use.
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: Quigibo on January 02, 2012, 06:07:01 am
Axe uses RRD and RLD for nibble storing.  I've never used them anywhere else, they are definitely very uncommon.  :ninja:

Code: [Select]
;hl = Nth nibble after address $8000
;e = Nibble to store there
NibSto:
scf
rr h
rr l
jr nc,__NibStoHigh
rrd
ld a,e
rld
ret
__NibStoHigh:
rld
ld a,e
rrd
ret
__NibStoEnd:
Title: Re: Constructive and Creative uses of RLD and RRD
Post by: Xeda112358 on January 02, 2012, 12:12:33 pm
That is fairly similar to how I used it in BatLib for the nibble storing and retrieving.