Searching \ for '[PIC]: Tine challendge shortest X -> 2X + 1' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/microchip/devices.htm?key=pic
Search entire site for: 'Tine challendge shortest X -> 2X + 1'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Tine challendge shortest X -> 2X + 1'
2002\06\03@004521 by Dmitriy A. Kiryashov

picon face
Hi guys. Here is tiny challendge to warm up. ;)

We have X variable. What is shortest possible
code to obtain 2X + 1 result back into X cell ?

WBR Dmitry.

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\06\03@005356 by Nick Veys

flavicon
face
rlf reg, f
incf reg, f

Ignoring any possible overflow of course...

spam_OUTnickTakeThisOuTspamveys.com | http://www.veys.com/nick

{Original Message removed}

2002\06\03@011234 by Spehro Pefhany

picon face
At 12:38 AM 6/3/02 +0400, you wrote:
>Hi guys. Here is tiny challendge to warm up. ;)
>
>We have X variable. What is shortest possible
>code to obtain 2X + 1 result back into X cell ?

        SETC                    ; set carry (BSF 3,0)
        RLF     X,F             ; now (X*2) + 1 for 0 <= X <= 7F


>Best regards,

Spehro Pefhany --"it's the network..."            "The Journey is the reward"
.....speffKILLspamspam@spam@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
9/11 United we Stand

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\06\03@021929 by ISO-8859-1?Q?Ruben_J=F6nsson?=

flavicon
face
Not using w or any other register except X. Only works for
X<=0x7f unsigned.

       bsf             STATUS,CARRY
       rlf             x,f

Since 2X always gives an even result (bit 0=0), setting CY to 1
before rlf has the effect of adding 1 to the result.

Ruben

{Quote hidden}

==============================
Ruben Jvnsson
AB Liros Electronic
Box 9124, 200 39 Malmv, Sweden
TEL INT +46 40142078
FAX INT +46 40947388
rubenspamKILLspampp.sbbs.se
==============================

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\06\03@071422 by Olin Lathrop
face picon face
> rlf reg, f
> incf reg, f

This is no good since the bit shifted in is unknown.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\06\03@134151 by Herbert Graf

flavicon
face
Careful, by using that method you risk adding TWO since if C is a one before
the rlf it will shift the one in as well (causing a pretty tricky bug to
find!). We can of course take advantage of this effect:

bsf STATUS, C
rlf ref, F

This wll set C to one, and shift that in on the LSB. Of course this does not
deal with overflow, as before. TTYL

> {Original Message removed}

2002\06\04@100526 by Scott Dattalo

face
flavicon
face
On Mon, 3 Jun 2002, Dmitriy A. Kiryashov wrote:

> Hi guys. Here is tiny challendge to warm up. ;)
>
> We have X variable. What is shortest possible
> code to obtain 2X + 1 result back into X cell ?
>

How about:

sdcc -S -mpic14 c.c

...

;;      Unrolled 8 X 8 multiplication
;#CSRC  c.c 71
;  achar0 = achar0*2 + 1;
       MOVF    _achar0,W       ;key=000,flow seq=001
       MOVWF   r0x20   ;key=001,flow seq=001
       ADDWF   r0x20,F ;key=002,flow seq=001
       CLRF    r0x21   ;key=003,flow seq=001
       RLF     r0x21,F ;key=004,flow seq=001
       INCF    r0x20,W ;key=005,flow seq=001
       MOVWF   _achar0 ;key=006,flow seq=001

I think I hate C compilers. :)

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email .....listservKILLspamspam.....mitvma.mit.edu with SET PICList DIGEST in the body


2002\06\04@112846 by Drew Vassallo

picon face
>How about:
>
>sdcc -S -mpic14 c.c
.
.
.
>I think I hate C compilers. :)

Looks like you've got some work to do, Scott :)



_________________________________________________________________
Join the world s largest e-mail service with MSN Hotmail.
http://www.hotmail.com

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email EraseMElistservspam_OUTspamTakeThisOuTmitvma.mit.edu with SET PICList DIGEST in the body


2002\06\04@121159 by John Dammeyer

flavicon
face
From the Bytecraft C

08D9 1003    BCF    STATUS,C          i = i*2+1;

MPC "C" COMPILER V1.40  24-Jul-2000
PAGE 89


08DA 0D4D    RLF    4D,W
08DB 00FE    MOVWF  7E
08DC 0A7E    INCF   7E,W
08DD 00CD    MOVWF  4D

John Dammeyer



Wireless CAN with the CANRF module.
www.autoartisans.com/documents/canrf_prod_announcement.pdf
Automation Artisans Inc.
Ph. 1 250 544 4950


> {Original Message removed}

2002\06\04@130932 by Walter Banks

picon face
John Dammeyer wrote:
>
> >From the Bytecraft C
>
> 08D9 1003    BCF    STATUS,C          i = i*2+1;
>
> MPC "C" COMPILER V1.40  24-Jul-2000
> PAGE 89
>
> 08DA 0D4D    RLF    4D,W
> 08DB 00FE    MOVWF  7E
> 08DC 0A7E    INCF   7E,W
> 08DD 00CD    MOVWF  4D
>
>
MPC "C" COMPILER B1.96s  31-May-2002

001F 1403    BSF    STATUS,C          c = (2 * c) + 1;
0020 0DB5    RLF    35

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservspamspam_OUTmitvma.mit.edu with SET PICList DIGEST in the body


2002\06\04@131303 by Herbert Graf

flavicon
face
All in all, not too bad, considering it is a generic routine that creates
that code. TTYL

{Quote hidden}

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservEraseMEspam.....mitvma.mit.edu with SET PICList DIGEST in the body


2002\06\04@135152 by Scott Dattalo

face
flavicon
face
On Tue, 4 Jun 2002, Walter Banks wrote:

> John Dammeyer wrote:
> >
> > >From the Bytecraft C
> >
> > 08D9 1003    BCF    STATUS,C          i = i*2+1;
> >
> > MPC "C" COMPILER V1.40  24-Jul-2000
> > PAGE 89
> >
> > 08DA 0D4D    RLF    4D,W
> > 08DB 00FE    MOVWF  7E
> > 08DC 0A7E    INCF   7E,W
> > 08DD 00CD    MOVWF  4D
> >
> >
> MPC "C" COMPILER B1.96s  31-May-2002
>
> 001F 1403    BSF    STATUS,C          c = (2 * c) + 1;
> 0020 0DB5    RLF    35

And thus reaffirming what I've been claiming all along -- for professional
projects you really want professional products. I know what it takes to
coerce a C compiler to generate efficient code. Walter (and Clyde)
definitely have managed to tame their compilers. Superb job, Walter!

Scott

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email EraseMElistservspammitvma.mit.edu with SET PICList DIGEST in the body


2002\06\04@152822 by uter van ooijen & floortje hanneman

picon face
> sdcc -S -mpic14 c.c
> ;  achar0 = achar0*2 + 1;
>         MOVF    _achar0,W       ;key=000,flow seq=001
>         MOVWF   r0x20   ;key=001,flow seq=001
>         ADDWF   r0x20,F ;key=002,flow seq=001
>         CLRF    r0x21   ;key=003,flow seq=001
>         RLF     r0x21,F ;key=004,flow seq=001
>         INCF    r0x20,W ;key=005,flow seq=001
>         MOVWF   _achar0 ;key=006,flow seq=001

You could try Jal ;-)

;; 005 : a = 2 * a + 1
 clrc                                    ; 0008: 1003 ;
 rlf     H'0F',w                         ; 0009: 0D0F ; 0x0F a
 addlw   H'01'                           ; 000A: 3E01 ; 1
 movwf   H'0F'                           ; 000B: 008F ; 0x0F a

But that is still not very convincing against a good assembly programmer.
How do the other (C) compilers fare?

Wouter van Ooijen
--
Van Ooijen Technische Informatica: http://www.voti.nl
Jal compiler, Wisp programmer, WLoader bootloader, PICs kopen

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservEraseMEspamEraseMEmitvma.mit.edu with SET PICList DIGEST in the body


2002\06\04@155508 by Dale Botkin

flavicon
face
On Tue, 4 Jun 2002, wouter van ooijen & floortje hanneman wrote:

> How do the other (C) compilers fare?

Interesting.  CCS PCW 3.091 (pretty recent) does this:

0000                00122 .................... a = a * 2 + 1;
0007 1003       00123 BCF    03.0
0008 0D0E       00124 RLF    0E,W
0009 008C       00125 MOVWF  0C
               ^^^^^^^^^^^^^^^ Saves to a scratch register...
                               this should definitely be optimized out!
000A 3E01       00126 ADDLW  01
000B 008E       00127 MOVWF  0E

Not too horrible, but there's definitely room for optimization.  I tried
splitting the operation into two lines to see if the scratch register save
went away, and it did:

0000                00128 .................... a *= 2;
000C 1003       00129 BCF    03.0
000D 0D8E       00130 RLF    0E,F
0000                00131 .................... a++;
000E 0A8E       00132 INCF   0E,F

Now we've got three instructions, which isn't too bad.  I might have
noticed it while reviewing the assembly output, probably not unless I was
really scraping the barrel for space or speed (which almost never
happens).

Dale

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservspam_OUTspamKILLspammitvma.mit.edu with SET PICList DIGEST in the body


2002\06\04@163439 by Matt Pobursky

flavicon
face
Which just goes to show, you really need to learn the personality
of your C compiler (especially for resource limited MCU's like
the PIC) and how it makes code from your source. The listing file
is your best friend!

On a side note (shameless plug follows)...

Not many people have said much on the PIC list over the past few
years about ByteCraft's MPC compiler. It's been the PIC C
compiler of choice for my professional work for many years now.
It's always generated very good code and the support from Walter
and his crew is tremendous. If only most software companies had
the same kind of service and support, we'd all be a whole lot
happier.

(End of shameless plug -- no, I don't work for ByteCraft or have
any affiliations with them. I buy my C compilers at retail just
like everyone else.)

Matt Pobursky
Maximum Performance Systems

On Tue, 4 Jun 2002 13:05:47 -0400, Walter Banks wrote:
{Quote hidden}

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservTakeThisOuTspamspammitvma.mit.edu with SET PICList DIGEST in the body


2002\06\04@181041 by John Dammeyer

flavicon
face
I find the same thing with Bytecraft.  More often than not,  simple
lines of code translate to the smallest memory image.  So your sequence
of splitting the operation is a) more efficient, b) easier to source
code debug with the ICE since you can step on each C line.

John Dammeyer



Wireless CAN with the CANRF module.
www.autoartisans.com/documents/canrf_prod_announcement.pdf
Automation Artisans Inc.
Ph. 1 250 544 4950


> {Original Message removed}

2002\06\05@024903 by Peter L. Peres

picon face
On Tue, 4 Jun 2002, Scott Dattalo wrote:

{Quote hidden}

That's a 8x8 multiplication by a *constant*, no ? Did you not draw upon
Dmitry's excellent multiplier/divider code to generate that sort of code ?

Peter

--
http://www.piclist.com hint: To leave the PICList
EraseMEpiclist-unsubscribe-requestspamspamspamBeGonemitvma.mit.edu


2002\06\05@034911 by Mircea Chiriciuc

flavicon
face
I hate C too, so here is my code:

; Xvar=Xvar*2+1
movf      Xvar,w        ;put X in W
addwf    Xvar,f          ; add W to X (X*2)
incf        Xvar,f          ; add 1 (x*2+1)

Note that the result is modulo 256 so it's working only if  X<127. Thus you
can check the C flag for overfolw and use it also for X>127

Mircea Chiriciuc
EMCO INVEST

--
http://www.piclist.com hint: To leave the PICList
RemoveMEpiclist-unsubscribe-requestKILLspamspammitvma.mit.edu


2002\06\05@072748 by Olin Lathrop

face picon face
> I hate C too, so here is my code:
>
> ; Xvar=Xvar*2+1
> movf      Xvar,w        ;put X in W
> addwf    Xvar,f          ; add W to X (X*2)
> incf        Xvar,f          ; add 1 (x*2+1)
>
> Note that the result is modulo 256 so it's working only if  X<127. Thus
you
> can check the C flag for overfolw and use it also for X>127

The other methods do this too.  There is no need for a third instruction:

    bsf   status, c     ;set value of bit to shift in
    rlf   x             ;shift left (X*2), shift in 1 (X*2 + 1)

Or:

    rlf   x             ;shift left (X*2), garbage in low bit
    bsf   x, 0          ;explicitly set low bit for the added 1

Either way the C flag is set if an overflow occurred, since a 1 bit would
have been shifted out by the RLF instruction.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestSTOPspamspamspam_OUTmitvma.mit.edu


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