ADDWF instruction in pic microcontroller - Page 1
Forum section: electronics / general

author: khatus
author_rank: Regular Contributor
timestamp: December 23, 2023, 06:17:54 pm
content: I can’t understand How the W register is incremented every time with the value 22H since the content of 12H register is 0 and every time we add it with 0 and store the result back in the w register it becomes 0+22H = 22H. I think the result will be a fixed value i.e 22Hin line 4 ADDWF 12H, W: This instruction adds the value in register 12H (which is 0) to the value in the W register (which is 22H) and stores the result back in the W register. The W register now contains 22H.in line 5 ADDWF 12H, W: This instruction again adds the value in register 12H (still 0) to the value in the W register (which is 22H) and stores the result back in the W register. The W register now contains 22H.I wrote here what I understood,1.MOVLW 0: This instruction loads the literal value 0 into the W register.2.MOVWF 12H: This instruction moves the value in the W register (which is now 0) into register 12H.3.MOVLW 22H: This instruction loads the literal value 22H into the W register.4. ADDWF 12H, W: This instruction adds the value in register 12H (which is 0) to the value in the W register (which is 22H) and stores the result back in the W register. The W register now contains 22H.5.ADDWF 12H, W: This instruction again adds the value in register 12H (still 0) to the value in the W register (which is 22H) and stores the result back in the W register. The W register now contains 22H.W = (W=22H +0) = 22H6.ADDWF 12H, W: This instruction repeats the addition, resulting in the W register having 22H.W = (W= 22 H +0) = 22H7.ADDWF 12H, W: The final addition takes place, and the W register now holds 22H.

author: DavidAlfa
author_rank: Super Contributor
timestamp: December 23, 2023, 08:03:03 pm
content: It seems wrong, your understanding is correct.Likely there's a errata there.MOVLW 22HMOVWF 12HMOVLW 0H W =0HADDWF 12H, W W=22HADDWF 12H, W W=44HADDWF 12H, W W=66HADDWF 12H, W W=88H

author: woofy
author_rank: Frequent Contributor
timestamp: December 23, 2023, 08:13:08 pm
content: Which pic?Provide the rest of the paper so we can put this in context.

author: khatus
author_rank: Regular Contributor
timestamp: December 23, 2023, 08:49:06 pm
content: This page is part of the book "PIC Microcontroller and Embedded Systems: Using assembly and C for PIC 18"----------------written by "Danny Causey, Rolin D. McKinlay, Muhammad Ali Mazidi"

author: ozcar
author_rank: Frequent Contributor
timestamp: December 23, 2023, 09:02:25 pm
content: I has been around 25 years since I did any PIC coding but I believe your assessment is correct.To get the outcome as suggested in the "Solution", you would have to interchange the two MOVLW instructions (that you numbered 1 and 3).Or, if some unwise person has interchanged the equated values of "W" and "F" (would normally be in an included header file), then location 12H would end up as 88H, and WREG = 22H.Edit:Ah. PIC18? I donno, I tinkered with PIC16 and I don't know how different they are.

author: DavidAlfa
author_rank: Super Contributor
timestamp: December 23, 2023, 10:03:17 pm
content: Pic16 and 18 are very much the same thing, wouldn't change this example.Maybe I got it wrong, is it a test asking to modify the program?

author: SiliconWizard
author_rank: Super Contributor
timestamp: December 23, 2023, 11:18:33 pm
content: Yes, this can't work.Alternatively to DavidAlfa's suggestion, you can otherwise replace the 3 first ADDWF with a F destination instead of W, but obviously in this case, at the end, the 12H location will contain 66H, and W 88H.You could write to the editor of this book. (Although it's 15 yo, so not sure how much the editor still cares.)

author: Doctorandus_P
author_rank: Super Contributor
timestamp: January 02, 2024, 08:43:34 pm
content: In a more general sense, why are you writing assembly for the PIC?A very long time ago I did some assembly for 8085 and Z80 and it was a useful experience to learn how the hardware of computers / processors work. Later I bought some 16F84 uC's (one of the first with Flash) and I found their assembly horribly convoluted. The way in which you have to combine multiple instructions to do even simple things was a big nuisance, and it also makes it more difficult to understand.But overall, it's been more then 20 years ago I wrote any assembly. Every decent uC gets accompanied with a C compiler. If you're interested in getting some project "done", writing your code in C is much more practical and efficient. The extra overhead for the compiled code is also quite small.If you're interested in writing assembly in itself, then the PIC is still one of the worst choices. (At least the older one I used. There are apparently relatively big differences between the PIC's in this area.)Also, if you switch to a different uC now, the work you have already done is not completely lost. ASM itself is not portable (Code re-use is another good reason to use C) but having some asm knowledge of different uC architectures is good as a learning experience.