Author Topic: ADDWF instruction in pic microcontroller  (Read 1143 times)

0 Members and 1 Guest are viewing this topic.

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 146
  • Country: gl
ADDWF instruction in pic microcontroller
« on: December 23, 2023, 06:17:54 pm »
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 22H

in 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) = 22H
6. ADDWF 12H, W: This instruction repeats the addition, resulting in the W register having 22H.
W = (W= 22 H +0) = 22H
7. ADDWF 12H, W: The final addition takes place, and the W register now holds 22H.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: ADDWF instruction in pic microcontroller
« Reply #1 on: December 23, 2023, 08:03:03 pm »
It seems wrong, your understanding is correct.
Likely there's a errata there.

MOVLW 22H
MOVWF 12H
MOVLW 0H           W =0H
ADDWF 12H, W    W=22H
ADDWF 12H, W    W=44H
ADDWF 12H, W    W=66H
ADDWF 12H, W    W=88H
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline woofy

  • Frequent Contributor
  • **
  • Posts: 334
  • Country: gb
    • Woofys Place
Re: ADDWF instruction in pic microcontroller
« Reply #2 on: December 23, 2023, 08:13:08 pm »
Which pic?
Provide the rest of the paper so we can put this in context.

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 146
  • Country: gl
Re: ADDWF instruction in pic microcontroller
« Reply #3 on: December 23, 2023, 08:49:06 pm »
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"

 

Offline ozcar

  • Frequent Contributor
  • **
  • Posts: 322
  • Country: au
Re: ADDWF instruction in pic microcontroller
« Reply #4 on: December 23, 2023, 09:02:25 pm »
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.
« Last Edit: December 23, 2023, 09:07:21 pm by ozcar »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: ADDWF instruction in pic microcontroller
« Reply #5 on: December 23, 2023, 10:03:17 pm »
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?
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: Ian.M

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14481
  • Country: fr
Re: ADDWF instruction in pic microcontroller
« Reply #6 on: December 23, 2023, 11:18:33 pm »
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.)

 

Online Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3365
  • Country: nl
Re: ADDWF instruction in pic microcontroller
« Reply #7 on: January 02, 2024, 08:43:34 pm »
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.
 
The following users thanked this post: Odd-Job


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf