Searching \ for 'Increment/Decrement "w"' 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/index.htm?key=incrementdecrement
Search entire site for: 'Increment/Decrement "w"'.

Truncated match.
PICList Thread
'Increment/Decrement "w"'
1999\06\22@083109 by Myke Predko

flavicon
face
Hi folks,

Yesterday, I needed snippets of code which which will increment/decrement
the "w" register without affecting any of the other registers in a 12C508.

For Increment, I came up with:

 xorlw 0x0FF                ;  Get 1s Complement of Number
 addwf Reg, w             ;  w = Reg + (w^0x0FF)
 subwf Reg, w             ;  w = Reg + ((Reg + (w^0x0FF))^0x0FF) + 1
                                     ;  w = w + 1

and for Decrement:

 subwf Reg, w             ;  w = Reg + (2^0x0FF) + 1
 xorlw 0x0FF               ;  Get 1s Complement of Result
 addwf Reg, w             ;  w = w - 1

"Reg" can be any Register in the PICMicro which cannot change during the
course of the three instructions (in the low-end PICMicros, this is any port
other than the timer and I/O ports).

I've added these to my list of PICMicro "snippets" at:

http://www.myke.com/PICMicro/snippet.htm

and I will see if I can find a general case that can be used for "addlw" and
"sublw" in the low-end devices.

myke

1999\06\22@124700 by Dmitry Kiryashov

flavicon
face
Hi Myke. Nice to see ya again ;-)

Do remember some discussion about tricks with ZERO_REG ? (with permanent
zero value. John, Scott and other guys were involved...) It is some
physically unimplemented location in memory and any operation read zero
from it. This will allow you to reduce you code down to 2 instruction
for every operation (I still not tested it yet but it looks workable)

WBR Dmitry.

Myke Predko wrote:
{Quote hidden}

       subwf   zero_reg,W      ; -W
       xorlw   0x0FF           ;-(-W)-1 = W-1

> and for Decrement:
>
>   subwf Reg, w             ;  w = Reg + (2^0x0FF) + 1
>   xorlw 0x0FF               ;  Get 1s Complement of Result
>   addwf Reg, w             ;  w = w - 1

       xorlw   0x0FF           ;-W -1
       subwf   zero_reg,W      ;-(-W -1) = W+1

{Quote hidden}

1999\06\22@203541 by Myke Predko

flavicon
face
Hey Dmitry,

Good to see you again as well!

No, I don't remember this one - Do you have any address (per device for
this)?

It would make the operations a lot simpler.

Thanx,

myke

{Original Message removed}

1999\06\22@205455 by Tony Nixon

flavicon
picon face
This is probably old news but I thought it was a good code snippet for
decrementing 16 bits.

decf Lo
incfsz Lo,w
decf Hi

--
Best regards

Tony

'The Engine' - Design your own programmer.

http://www.picnpoke.com
Email spam_OUTpicnpokeTakeThisOuTspamcdi.com.au

1999\06\22@223133 by Jim Robertson
flavicon
face
At 10:53 23/06/99 +1000, you wrote:
>This is probably old news but I thought it was a good code snippet for
>decrementing 16 bits.
>
>decf Lo
>incfsz Lo,w
>decf Hi


Is it just me or is the above incorrect?

Jim


________________________________________
Email: .....newfoundKILLspamspam@spam@pipeline.com.au
http://www.new-elect.com
WARP-13 SALE now on. $48USD with world delivery.
MPLAB compatible PIC programmers and firmware
upgrades for many programmers.
________________________________________

1999\06\22@235606 by Ben Stragnell

flavicon
face
> At 10:53 23/06/99 +1000, you wrote:
> >This is probably old news but I thought it was a good code snippet for
> >decrementing 16 bits.
> >
> >decf Lo
> >incfsz Lo,w
> >decf Hi
>
> Is it just me or is the above incorrect?

Yeah... incfsz really needs to be incfsnz (if only there were such an
instruction).
Better to use:

       movlw           1
       subwf           Lo
       btfsc           STATUS,C
       decf            Hi

Unless anyone has a better idea?

Cheers,
Ben

1999\06\23@003112 by Sean Breheny

face picon face
At 08:59 PM 6/22/99 -0700, you wrote:
>Yeah... incfsz really needs to be incfsnz (if only there were such an
>instruction).
>Better to use:
>
>        movlw           1
>        subwf           Lo
>        btfsc           STATUS,C
>        decf            Hi
>
>Unless anyone has a better idea?

Its not better, but an alternative:

decf Lo
decf Hi
incfsz Lo,w
incf Hi



>
>Cheers,
>Ben
>


Sean

|
| Sean Breheny
| Amateur Radio Callsign: KA3YXM
| Electrical Engineering Student
\--------------=----------------
Save lives, please look at http://www.all.org
Personal page: http://www.people.cornell.edu/pages/shb7
shb7spamKILLspamcornell.edu ICQ #: 3329174
________________________________________________________
NetZero - We believe in a FREE Internet.  Shouldn't you?
Get your FREE Internet Access and Email at
http://www.netzero.net/download/index.html

1999\06\23@005230 by Regulus Berdin

picon face
Hi,

Ben Stragnell wrote:
>         movlw           1
>         subwf           Lo
>         btfsc           STATUS,C
>         decf            Hi
>
> Unless anyone has a better idea?

dec16:
       movf    lo,f
       skpnz
        decf   hi,f
       decf    lo,f

Same number of cycles but does not touch W.

regards,
Reggie

--
e-mail: .....rberdinKILLspamspam.....bigfoot.com
ICQ#:   31651436
URL:    http://www.bigfoot.com/~rberdin

1999\06\23@190708 by Tony Nixon

flavicon
picon face
Was it April fools day yesterday?

Sorry about my little snippet, it obviously doesn't work.

Here's some more to make up.

Rotate a value without using carry.

rlf RAM,w
rlf RAM


Test if a value is in a certain range

W = value

addlw 255 - Hi
addlw (Hi - Lo) + 1

Carry is set if W is in range Lo - Hi




--
Best regards

Tony

'The Engine' - Design your own programmer.

http://www.picnpoke.com
Email EraseMEpicnpokespam_OUTspamTakeThisOuTcdi.com.au

1999\06\23@223732 by Sean Breheny

face picon face
At 09:06 AM 6/24/99 +1000, you wrote:
>Was it April fools day yesterday?
>
>Sorry about my little snippet, it obviously doesn't work.
>
>Here's some more to make up.
>
>Rotate a value without using carry.
>
>rlf RAM,w
>rlf RAM

I'm afraid I don't get this one. Why isn't carry affected? In fact, if it
weren't affected,the first line would do nothing to affect the second.

Sean

|
| Sean Breheny
| Amateur Radio Callsign: KA3YXM
| Electrical Engineering Student
\--------------=----------------
Save lives, please look at http://www.all.org
Personal page: http://www.people.cornell.edu/pages/shb7
shb7spamspam_OUTcornell.edu ICQ #: 3329174
________________________________________________________
NetZero - We believe in a FREE Internet.  Shouldn't you?
Get your FREE Internet Access and Email at
http://www.netzero.net/download/index.html

1999\06\23@232611 by Tony Nixon

flavicon
picon face
Sean Breheny wrote:

>Rotate a value without using carry.
>
> >rlf RAM,w
> >rlf RAM
>
> I'm afraid I don't get this one. Why isn't carry affected? In fact, if it
> weren't affected,the first line would do nothing to affect the second.

It does 'affect' the carry, but it doesn't 'use' it.
In other words rotate the byte left once.

--
Best regards

Tony

'The Engine' - Design your own programmer.

http://www.picnpoke.com
Email @spam@picnpokeKILLspamspamcdi.com.au

1999\06\24@030849 by Sean Breheny

face picon face
Ok, I get it, I was a bit slow on the uptake.<G> The first line places the
MSBit of RAM into Carry and then the second actually performs the rotate.

Thanks,

Sean

At 01:24 PM 6/24/99 +1000, you wrote:
{Quote hidden}

| Sean Breheny
| Amateur Radio Callsign: KA3YXM
| Electrical Engineering Student
\--------------=----------------
Save lives, please look at http://www.all.org
Personal page: http://www.people.cornell.edu/pages/shb7
RemoveMEshb7TakeThisOuTspamcornell.edu ICQ #: 3329174
________________________________________________________
NetZero - We believe in a FREE Internet.  Shouldn't you?
Get your FREE Internet Access and Email at
http://www.netzero.net/download/index.html

1999\06\24@063506 by Caisson

flavicon
face
> Van: Sean Breheny <spamBeGoneshb7spamBeGonespamCORNELL.EDU>
> Aan: TakeThisOuTPICLISTEraseMEspamspam_OUTMITVMA.MIT.EDU
> Onderwerp: Re: Increment/Decrement "w"
> Datum: donderdag 24 juni 1999 4:34
>
> At 09:06 AM 6/24/99 +1000, you wrote:
> >Was it April fools day yesterday?
> >
> >Sorry about my little snippet, it obviously doesn't work.
> >
> >Here's some more to make up.
> >
> >Rotate a value without using carry.
> >
> >rlf RAM,w
> >rlf RAM
>
> I'm afraid I don't get this one. Why isn't carry affected? In fact, if it
> weren't affected,the first line would do nothing to affect the second.

Sean, the normal Rotate-instructions are 9-bit (Carry into lowest bit,
highest bit into Carry).  This modification creates a 8-bit Rotate.
(Highest bit into Lowest bit).

Greetz,
 Rudy Wieser

1999\06\24@112808 by Dmitry Kiryashov

flavicon
face
Tony Nixon wrote:
>
> Sean Breheny wrote:
>
> >Rotate a value without using carry.
> >
> > >rlf RAM,w
> > >rlf RAM
> >
> > I'm afraid I don't get this one. Why isn't carry affected? In fact, if it
> > weren't affected,the first line would do nothing to affect the second.
>
> It does 'affect' the carry, but it doesn't 'use' it.
> In other words rotate the byte left once.


If you would like to rotate without affection the carry you can do it like that:

;right cycle shift

rrf     RAM,W
rrf     RAM,F
addlw   0x80    ;restore carry

;left cycle shift

rlf     RAM,W
rlf     RAM,F
movwf   temp_cy
rrf     temp_cy,f ;restore carry

Any ideas how to make second one shorter ? ;-)

WBR Dmitry.

1999\06\24@190625 by Regulus Berdin

picon face
Hi Dmitry,

Dmitry Kiryashov wrote:
> ;left cycle shift
>
> rlf     RAM,W
> rlf     RAM,F
> movwf   temp_cy
> rrf     temp_cy,f ;restore carry
>
> Any ideas how to make second one shorter ? ;-)

       rlf     RAM,w
       rlf     RAM,f
       movwf   STATUS          ; ;-) very BAD!!!

This is _NOT_ recommended. This is just to give a solution to the
problem to restore the carry using 3 cycles.  This will affect other
flags and bank settings depending on RAM contents.

regards,
Reggie

--
e-mail: RemoveMErberdinspamTakeThisOuTbigfoot.com
ICQ#:   31651436
URL:    http://www.bigfoot.com/~rberdin

1999\06\29@012226 by Eric Smith

flavicon
face
Tony Nixon <Tony.NixonEraseMEspam.....ENG.MONASH.EDU.AU> wrote:
> Test if a value is in a certain range
>
> W = value
>
> addlw 255 - Hi
> addlw (Hi - Lo) + 1
>
> Carry is set if W is in range Lo - Hi

That can't possibly work!  There aren't enough instructions!  And I refuse
to try it out to see if it works, since I know I'm right!

Sorry, I couldn't resist.  That's the sort of feedback I got when I posted
this method here years ago.  I normally use a macro for this, so that
I don't have to remember the details.

On the 12-bit core, there is no addlw instruction, so doing the equivalent
requires a temporary register and three more instructions:

       movwf   temp
       movlw   255-Hi
       addwf   temp
       movlw   (hi-lo)+1
       addwf   temp,w

Cheers,
Eric

1999\06\29@221029 by Tony Nixon

flavicon
picon face
Eric Smith wrote:

> > Test if a value is in a certain range
> >
> > W = value
> >
> > addlw 255 - Hi
> > addlw (Hi - Lo) + 1
> >
> > Carry is set if W is in range Lo - Hi
>
> That can't possibly work!  There aren't enough instructions!  And I refuse
> to try it out to see if it works, since I know I'm right!
>

Let's see :-)


Lower = 50
Upper = 60

addlw 255 - Hi equates to addlw 195
addlw (Hi - Lo) + 1 equates to addlw 11

W = 50          ; lower limit

addlw 195       ; w = 245
addlw 11        ; w = 0, C = 1, result = OK

W = 55          ; in range

addlw 195       ; w = 250
addlw 11        ; w = 5, C = 1, result = OK

W = 60          ; upper limit

addlw 195       ; w = 255
addlw 11        ; w = 11, C = 1, result = OK

W = 49          ; under minimum

addlw 195       ; w = 244
addlw 11        ; w = 255, C = 0, result = Not OK

W = 61          ; over maximum

addlw 195       ; w = 0
addlw 11        ; w = 11, C = 0, result = Not OK


--
Best regards

Tony

'The Engine' - Design your own programmer.

http://www.picnpoke.com
Email EraseMEpicnpokespamcdi.com.au

1999\06\30@034003 by Regulus Berdin

picon face
Tony Nixon wrote:
{Quote hidden}

Hi,

Of what I understand is that Eric was the source of this snippet.  He
just emphasized the previous response he got when he first posted it
here.

BTW, the snippet works.

regards,
Reggie

--
e-mail: RemoveMErberdinEraseMEspamEraseMEbigfoot.com
ICQ#:   31651436
URL:    http://www.bigfoot.com/~rberdin

1999\06\30@082826 by John Pfaff

flavicon
face
I think most (if not all of the confusion came from people not knowing
wether Hi and Lo were in RAM or constants at assembly time.

...and yes, it does work.

{Original Message removed}

1999\06\30@085332 by Walter Banks

picon face
> I think most (if not all of the confusion came from people not knowing
> wether Hi and Lo were in RAM or constants at assembly time.
>
> ...and yes, it does work.

It works for all values of Hi and Lo except 255 and 0.

It is not the only sequence of the same length to do
range checking.  Of the several sequences that we have
in our compilers each of them trade destructive testing for
code size and bias the tested variable against either
0 or 255 allowing a single conditional branch.

Walter Banks
http://www.bytecraft.com

1999\06\30@191550 by Tony Nixon

flavicon
picon face
Hi Dennis,

> No, just a comment from the price in Black Adder.

Just kidding :-)

> "addlw 255-Hi"
> is this not a high level bit of code or does the PIC suddenly do //
> processing? because if Hi is RAM or variable as you indicate the compiler
> will take the absolute value of it PORTB for example, and thus the result
> will always be the same.

This value is not a variable or high level code. It is a constant and is
calculated only at assembly time. The programmer desides on the values.

> I am just saying that I think some code is
> missing! And that the original poster is correct not enoguh instructions.

Nothing missing, except as Eric Smith mentioned, the assembler must be
able to truncate values over 255 or do it yourself.


Just a bit of trivia...

My soldering iron tip broke the other day, and while waiting for a
replacement, I had to use a small 3mm desoldering tip. It works a treat
for soldering IC pins.

--
Best regards

Tony

'The Engine' - Write your own programmer software.

http://www.picnpoke.com
Email RemoveMEpicnpokespam_OUTspamKILLspamcdi.com.au

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