From ccslist@ewol.com Thu Mar 27 13:27:45 1997
Date: Sun, 16 Mar 1997 11:26:22 -0500
From: Customized Controller Solutions <ccslist@ewol.com>
To: 8051code@emily.keilsoftware.com
Subject: Re: Seeking ASM code for extract 5 bits from 16 bit word.

Thanks for your help.  Notes and comments follow.

At 12:03 AM 3/16/97 +0100, Joakim Rehn <joakim.rehn@mailbox.swipnet.se>
replied to Brent Pollock <ccs@ewol.com>:
>> > 
>> > Seeking ASM code for extract 5 bits from 16 bit word.
>> > 
>> > ##### Begin Example:
>###################################################
>> > 
>> > The 16 bit word:
>> > 
>> > _HiByte   _LoByte
>> > 0101 0111 0101 0101  <--- work on this 16 bit word
>> > 
>> > 0000 0111 1100 0000  <--- get these into the 5 lsb's of a resultant
>word.
>> >       ^^^ ^^
>> >       ||| ||
>> > Extract These BITS.
>> > 
>> > Result = 11101 == 1Dh == 29d
>> > ##### End Example:
>#####################################################
>> > 
>> > This piece of code will rotate the bits of _WORKBYTE to the right 
>> > by the number in __MYBITS and place the value in the least significant 
>> > 4 bits.  The result is then placed in __PRODUCT
>> > 
>> > Any criticism is welcome and appreiated.  I want to become a better 
>> > programmer.
>> > 
>> > #ASM
>> > ;
>> >    MOV     DPTR,#__MYBITS       ; Move Data Pointer to RAM
>> >                                     ; X-reg with # of bits to shift
>> >    MOVX    A, @DPTR                 ; Put val at DPTR into ACC
>> >    MOV _CCSTemp1,A              ; Store the value in _CCSTemp1
>> >    JNZ StartShift1              ; If __MYBITS <> 0 THEN Continue
>> >    SJMP    ExitShift1               ; Else Exit Code
>> > ;
>> > StartShift1:
>> >    MOV A,_WORKBYTE              ; Word to work on.
>> > ShiftLp1:
>> >    RR  A
>> >    DJNZ    _CCSTemp1,ShiftLp1       ; IF more shifts left then repeat
>> > ;
>> > ExitShift1:
>> >    MOV _WORKBYTE,A              ; Put the shifted bits in ACC
>> >    ANL A,#00001111B             ;
>> >    MOV     DPTR,#__PRODUCT      ; Move Data Pointer to RAM
>> >    MOVX    @DPTR,A              ; Put the __Product into RAM BYTE
>> > ;
>> > #ASM_END
>> > 
>> 
>> 
>> 
>> Hi there!
>> 
>> I have not tested this code, but i think it should be a 
>> solution to the above example.
>> 
>> (btw, why are you shifting out 5 bits, and then stripping it down to 4?)

This was my mistake.  I forgot to put a note at the top of the original 
post requesting help on this.

My intention was to note that the example was what I was seeking to acheive, 
and the code that is shown above was my first exercise to learn how to work 
with byte on a bit level.  That is the reason for the two (code and example) 
being different.  

I used the above code(not the above example) to show how I was attempting
to get 4 bits within a byte into a single nibble.  Your code (shown below)
is more along the lines of what I was looking to obtain, base on my 
above listed example(not my code).  My above listed code was to show
what I had been able to do with just one byte before getting to the more
complex extraction of 5 bits split between to bytes, then put into the
least significant 5 bits of a byte.

With my example,(listed above) the hope was to get 5 bits from two bytes,
3 bits from the upper and 2 bits from the lower.  There are cases where
I will need to get anywhere from 2 to 5 bits contained anywhere within 
an upper byte anywhere from and 2 to 5 bits contained anywhere within a
lower byte.

Your code (listed below) is a great starting point for what I need to
do.  The changes I will make will be posted to the various 8051 list
that this post has been sent to.  I will be more careful to note the
intention of each example and piece of code, in the future postings.

Thank you for your time and effort.  This makes things easier for me 
to understand.

>> 
>> 
>> #ASM
>>  Mov Dptr,#__MYBITS      ;Set pointer
>>  Movx    A,@Dptr         ;Get HIGHBYTE
>>  Anl A,#0Fh          ;Mask out bottom nibble
>>  Mov _TEMP,A         ;Store 
>>  Inc Dptr                ;Point at LOWBYTE
>>  Movx    A,@Dptr         ;Get LOW
>>  Anl A,#0F0h         ;Mask out top nibble
>>  Orl A,_TEMP         ;Add HIGHBYTE 0..3 to LOWBYTE 7..4
>>  RL  A           ;Rotate up 2 steps to adjust the result
>>  RL  A           ;Here we should have the result in ACC
>>  Mov Dptr,#__PRODUCT     ;Set return word pointer
>>  Movx    @Dptr,A         ;Place the result at __PRODUCT
>> #END_ASM
>> 
>> 
>> 
>>      Regards, Joakim
>> 
>>     ///////////////////////////////////////////////
>>    //  E:    joakim.rehn@mailbox.swipnet.se     //
>>   //  Fax:  +46-8-30 66 70                     //
>>  //  URL:  http://home1.swipnet.se/~w-10955   //
>> ///////////////////////////////////////////////
>> 
Thank you for your time and effort.  
Brent Pollock

Separate EMails to avoid losing non-list EMails directed to me.

ccs@ewol.com - business
bpollock@ewol.com - personal
ccslist@ewol.com - to receive from EMail list. (8051,PIC)


The 8051 Code mailing list is maintained by Keil Software.
Check http://www.keil.com/ for more information.
