Searching \ for '[PIC]: Right shifting across diff bytes' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Right shifting across diff bytes'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Right shifting across diff bytes'
2001\05\02@173455 by Graham North

picon face
Hi,

Again thanks to everyone who has replied to any of my recent posts.

I am having a bit of trouble trying to right shift a multiple byte value.

I am trying to right shift a 24 bit value 8 times (to give a 16 bit answer),
but I can not figure out how to get the bit shifted to the carry, into the
top bit of the next byte down.

I am sure there is a simple answer. I have been lookin at this screen TOO
long!

Thanks

Graham
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\05\02@174322 by David VanHorn

flavicon
face
>
>I am trying to right shift a 24 bit value 8 times (to give a 16 bit answer),
>but I can not figure out how to get the bit shifted to the carry, into the
>top bit of the next byte down.
>
>I am sure there is a simple answer. I have been lookin at this screen TOO
>long!

If I understand you right, then all you need to do is RRF each register in
turn:

Clear carry first, then a loop of eight cycles:

rrf high_byte
rrf med_byte
rrf low_byte

The lowest bit goes into carry, and what was in carry before ends up in the
high bit.

Alternately,
movfw   med_byte
movwf low_byte
movfw high_byte
movwf med_byte

will do it all at one whack, without worrying about carry.

Of course you could just use the two high bytes and not worry about the low
byte at all.



--
Dave's Engineering Page: http://www.dvanhorn.org
Where's dave? http://www.findu.com/cgi-bin/find.cgi?kc6ete-9

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\05\02@174742 by Stephen B Webb

flavicon
face
> I am trying to right shift a 24 bit value 8 times (to give a 16 bit answer),
> but I can not figure out how to get the bit shifted to the carry, into the
> top bit of the next byte down.

ah,am,al  // 3 byte value  high byte, middle byte, low byte

movf  am,w
movwf al
movf  ah,w
movwf am
xorwf ah,f

Since you are shifting 8 bits (one full byte) you can just set the low
byte to the middle byte, the midlle byte to the high byte, and the high
byte to zero.  The xorwf instruction xors what is in W (ah) with a file
register (ah) and stores it in the file register (ah) a xor a == 0, so
you end up with zero in the ah, which is what you want.

-Steve

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\05\02@175126 by Wayne Hortensius

flavicon
face
At 09:33 PM 5/2/01 +0000, Graham North <spam_OUTgs_piclistTakeThisOuTspamHOTMAIL.COM> wrote:
>I am having a bit of trouble trying to right shift a multiple byte value.
>
>I am trying to right shift a 24 bit value 8 times (to give a 16 bit answer),
>but I can not figure out how to get the bit shifted to the carry, into the
>top bit of the next byte down.
>
>I am sure there is a simple answer. I have been lookin at this screen TOO
>long!

8 bit right shift? 24 bits in src_H (MSB), src_M, src_L (LSB).

   movf    src_M,w          ; bits 8-15
   movwf  src_L              ; into bits 0-7
   movf    src_H,w           ; bits 16-23
   movwf  src_M             ; into bits 8-15
   clrf       src_H             ; clear bits 16-23

or, the hard way

   movlw    8
   movwf    temp
loop:
   clrc
   rrf          src_H,f         ; b7 now zero, b0 into carry
   rrf          src_M,f         ; old b0 of src_H into b7, b0 into carry
   rrf          src_L,f          ; old b0 of src_M into b7
   decfsz    temp,f
   goto       loop

Wayne

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\05\02@175133 by Christian Dorner

flavicon
face
Hi!

> I am trying to right shift a 24 bit value 8 times (to give a 16 bit
answer),

Just use the two upper bytes without shifting around. You have already the
16 bit answer!
(In case you need the answer in the way you descriped above you can also
copy the MID-Byte to the LOW-Byte and the HIGH-Byte to the MID-Byte (and
clear the HIGH-Byte afterwards).)

cu, Doc ...

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\05\03@090556 by Olin Lathrop

face picon face
> I am trying to right shift a 24 bit value 8 times (to give a 16 bit
answer),
> but I can not figure out how to get the bit shifted to the carry, into the
> top bit of the next byte down.

Shifting a multiple of 8 bits requires no bit shifting at all.  Just grab
the appropriate bytes.

In general to right shift a multi-byte value, so something like this:

    bcf    status, c      ;set high bit to shift in
    rrf    <high byte>, f ;shift byte and put shifted-out byte into C
    rrf    <next lower byte>, f
    .
    .
    rrf    <lowest byte>, f


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, .....olinKILLspamspam@spam@embedinc.com, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...