EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

The incfsz command

Started by Jason Hsu May 16, 2008
Page 11 of the Elmer 160 course (lesson 5) shows an example of a
counter. The code:

clrf Spot1
loop
incfsz Spot1, F
goto loop

The clrf command sets all bits of register Spot1 to 0.
The incfsz command increments register Spot1. If F='0', the result of
this operation is placed in the W register. If F='1', the result of
this operation is placed back in register Spot1.

Exactly what controls whether the goto loop command is executed?
Under what circumstances do we stay in the loop, and under what
circumstances do we skip the "goto loop" command and move on to the
rest of the program?
> The incfsz command increments register Spot1. If F='0', the result of
> this operation is placed in the W register. If F='1', the result of
> this operation is placed back in register Spot1.

There is 'little bit' more to the infsz operation (the 'sz' part).
Re-read the description of the instruction.

Older PIC datasheets (try 16F84A) had a good description of each
instruction.

--

Wouter van Ooijen

-- -------
Van Ooijen Technische Informatica: www.voti.nl
consultancy, development, PICmicro products
docent Hogeschool van Utrecht: www.voti.nl/hvu
----- Original Message -----
From: "Wouter van Ooijen"
To:
Sent: Friday, May 16, 2008 3:20 AM
Subject: Re: [piclist] The incfsz command
> Older PIC datasheets (try 16F84A) had a good description of each
> instruction.

Perhaps this hasn't been emphasized enough lately -- READ THE DATASHEET.
Whatever your problem is, the datasheet is usually the place to go. For the
PIC16, the Mid-Range Manual is excellent, but it is a little long. For newer
PIC16's, as Wouter implies, you may need more than the datasheet, but you
still need the datasheet.

Jason revealed in another forum that he is trying to understand a program
written for the PIC16C71. Taking a quick peek at the PIC16C71X datasheet
(30272a), the instruction descriptions look to be slightly better than the
F84 datasheet.

72/73 de WB8RCR http://www.qsl.net/wb8rcr
didileydadidah QRP-L #1446 Code Warriors #35
> Page 11 of the Elmer 160 course (lesson 5) shows an example of a
> counter. The code:
>
> clrf Spot1
> loop
> incfsz Spot1, F
> goto loop
>
> The clrf command sets all bits of register Spot1 to 0.
> The incfsz command increments register Spot1. If F='0', the result of
> this operation is placed in the W register. If F='1', the result of
> this operation is placed back in register Spot1.
>
> Exactly what controls whether the goto loop command is executed?
> Under what circumstances do we stay in the loop, and under what
> circumstances do we skip the "goto loop" command and move on to the
> rest of the program?

The key is the "sz". This is "increment f and skip if zero." It skips the
next instruction if the result of the increment was zero. So, the goto
loop is executed until Spot1 is incremented to zero (it rolls over from
0xff). Note also that F is defined in the processor include files as 1,
and W is defined as zero. So,

incfsz Spot1, 0 ; adds 1 to Spot1 and puts result in w. Skips next if
result zero.

incfsz Spot1, w ; adds 1 to Spot1 and puts result in w. Skips next if
result zero.

incfsz Spot1, 1 ; adds 1 to Spot1 and puts result in Spot1. Skips next
if result zero.

incfsz Spot1, f ; adds 1 to Spot1 and puts result in Spot1. Skips next
if result zero.
Harold

--
FCC Rules Updated Daily at http://www.hallikainen.com - Advertising
opportunities available!

I think I get it now. (The explanations at
http://users.tpg.com.au/users/talking/explaining_instruction_set.html
were more useful to me than the ones in the datasheet.)

In the statement "incfsz Spot1, F", F=0 if and only if all the bits in
register Spot1 are also equal to 0. If any bit in register Spot1 is
1, then F='1'.

Given the code and the included file that specifies F=1:
clrf Spot1
loop
incfsz Spot1, F
goto loop

Spot1 is set by the clrf command to "0000000".
Iteration 1: We enter the increment command with F='1', so that means
Spot1 is incremented (becomes "0000001") and the next command is
executed. Because the resulting value in Spot1 is nonzero, F remains
equal to 1.
Iteration 2: F='1' still, so that means Spot1 is incremented (becomes
"0000001") and the next command is executed. Because the resulting
value in Spot1 is nonzero, F remains equal to '1'.
Later Iteration: We enter a later iteration with Spot1="1111111" and
F='1', so that means Spot1 is incremented (becomes "0000000") and the
next command is executed. But because the resulting value in Spot1
has all zeros, F is now changed to '0'.
Final Iteration: We enter the final iteration with Spot1="0000000" and
F='0'. Because F='0', the resulting value of "0000001" goes into the
W register instead of Spot1. Also, because F='0', the next
instruction is skipped, and we move out of the loop.
Jason, you are STILL missing the point of the ,F. I thought that Harold's
example was very clear.

incfsz spot1,F

means take the contents of the location spot, increment it, and store the
result in spot1. If the result was zero, skip the next instruction

incfsz spot1,W

means take the contents of the location spot, increment it, and store the
result in the accumulator (W register). If the result was zero, skip the
next instruction

Note that the web page you references is for a 12C processor, which is
different than the 16C, although for this particular purpose they are the
same,

Note that you *COULD* use 1 for F and 0 for W, but that would not be good
practice. The use of the F or W makes your intent clear. If only for this
reason, I wouldn't put a lot of credence in that web page. Note that using
a 0 or 1 will work, but writing 0 or 1 instead of W or F is a mark of an
inexperienced programmer. Experienced programmers recognize that making the
program work is a much simpler problem than being able to understand it next
week.

The web page also refers to address ranges that ONLY apply to the 12C508A.
Those address ranges are different for the 16C71. His description of how
the instruction works is good, however, except you need to remember that
when he says "file" he means a memory location in the PIC's "File Register"
area.

Remember - W means the result goes in the W register, which is the PIC's
accumulator. F means put the result back into a memory location.

72/73 de WB8RCR http://www.qsl.net/wb8rcr
didileydadidah QRP-L #1446 Code Warriors #35

----- Original Message -----
From: "Jason Hsu"
To:
Sent: Friday, May 16, 2008 11:57 AM
Subject: [piclist] Re: The incfsz command
>I think I get it now. (The explanations at
> http://users.tpg.com.au/users/talking/explaining_instruction_set.html
> were more useful to me than the ones in the datasheet.)
>
> In the statement "incfsz Spot1, F", F=0 if and only if all the bits in
> register Spot1 are also equal to 0. If any bit in register Spot1 is
> 1, then F='1'.
>
> Given the code and the included file that specifies F=1:
> clrf Spot1
> loop
> incfsz Spot1, F
> goto loop
>
> Spot1 is set by the clrf command to "0000000".
> Iteration 1: We enter the increment command with F='1', so that means
> Spot1 is incremented (becomes "0000001") and the next command is
> executed. Because the resulting value in Spot1 is nonzero, F remains
> equal to 1.
> Iteration 2: F='1' still, so that means Spot1 is incremented (becomes
> "0000001") and the next command is executed. Because the resulting
> value in Spot1 is nonzero, F remains equal to '1'.
> Later Iteration: We enter a later iteration with Spot1="1111111" and
> F='1', so that means Spot1 is incremented (becomes "0000000") and the
> next command is executed. But because the resulting value in Spot1
> has all zeros, F is now changed to '0'.
> Final Iteration: We enter the final iteration with Spot1="0000000" and
> F='0'. Because F='0', the resulting value of "0000001" goes into the
> W register instead of Spot1. Also, because F='0', the next
> instruction is skipped, and we move out of the loop.
>
>
> to unsubscribe, go to http://www.yahoogroups.com and follow the
> instructions
Thanks for the responses.

I see now that f is constant (remains at 1). Since that's the second
argument in the incfsz command, that means that the result of the
decrement ALWAYS is fed back into the count register. In order for
the program to skip the next instruction and thus leave the loop, the
result of the decrement must be "0000000", hence the sz (skip if zero).
OK, now I understand why w=0 and f=1. If you use w as the second
argument for an -sz command, the result is stored in the W directory.
If you use f as the second argument for an -sz command, the result is
stored in the file register. Now things are making sense.
> OK, now I understand why w=0 and f=1. If you use w as the second
argument for an -sz command, the result is stored in the W directory.
> If you use f as the second argument for an -sz command, the result is
> stored in the file register. Now things are making sense.

It's not just a SkipifZero instruction, but any instruction that allows
the destination to be either W or a memory location (which may actually be
an I/O device). The second argument in these instructions is the
destination. If the argument is zero, the destination is the w register.
If the argument is 1, the destination is memory (a "file register"). To
make that more clear, we use w and f instead of 0 and 1 to show the
destination.

Harold

--
FCC Rules Updated Daily at http://www.hallikainen.com - Advertising
opportunities available!

--
FCC Rules Updated Daily at http://www.hallikainen.com - Advertising
opportunities available!

Hi Jason,
The value of register "Spot1" controls weather the the skip
is executed. This is just a delay loop. As "Spot1" is initialized to
zero the loop is executed 256 times before "Spot1" overflows and has
the value zero again. At this point the skip takes you past the goto
instruction and continues the program.
Les.

--- In p..., "Jason Hsu" wrote:
>
> Page 11 of the Elmer 160 course (lesson 5) shows an example of a
> counter. The code:
>
> clrf Spot1
> loop
> incfsz Spot1, F
> goto loop
>
> The clrf command sets all bits of register Spot1 to 0.
> The incfsz command increments register Spot1. If F='0', the result of
> this operation is placed in the W register. If F='1', the result of
> this operation is placed back in register Spot1.
>
> Exactly what controls whether the goto loop command is executed?
> Under what circumstances do we stay in the loop, and under what
> circumstances do we skip the "goto loop" command and move on to the
> rest of the program?
>


The 2024 Embedded Online Conference