Doubt asm 16 bits 16F877 - Page 1
Forum section: electronics / general

author: aless2056
author_rank: Contributor
timestamp: May 27, 2021, 12:32:02 am
content: Guys, I'm starting with assembly programming and I have a lot of doubt on how to deal with 16 bits, 2 bytes.I have the code below, how this code would be if the variables Process, Setpoint and PWM were to have an example number up to 1024, instead of 256.CLRWDTMOVF PROCESS_KW,WSUBWF SETPOINT,WBTFSC STATUS,ZGOTO EXITMOVLW .5SUBWF PROCESS,WBTFSS STATUS,CMOVLW .0SUBWF SETPOINT,WBTFSC STATUS,ZGOTO EXITBTFSS STATUS,CGOTO PWM_DECMOVLW .5ADDWF PROCESS,WSUBWF SEPOINT,WBTFSC STATUS,ZGOTO EXITBTFSC STATUS,CGOTO PWM_INCGOTO EXITPWM_DECMOVLW .0XORWF PWM,WBTFSC STATUS,ZGOTO EXITDECF PWM,FGOTO EXITPWM_INCMOVLW .255XORWF PWM,WBTFSC STATUS,ZGOTO EXITINCF PWM,FGOTO EXITEXIT

author: ucanel
author_rank: Regular Contributor
timestamp: May 27, 2021, 12:52:42 am
content: It is same as how you do it in decimal system.You will use pwmH and pwmL instead of PWMand think them as iftens and ones digits if you were in decimal systems.When you wanna decrement this 16bit pwm valueyou will decrement pwmL first and then check if it gets smaller than zero ( wraps to 255)if so you will get a borrow from pwmH this step means you just decrement pwmH,if you decrement pwmH you need to check if it gets below zero.There are so many examples about 16 bit add. sub. inc. dec. compare on 8 bit mcu, there are even examples especially for 16F asm.

author: aless2056
author_rank: Contributor
timestamp: May 27, 2021, 01:27:32 am
content: HAVE THE LINK WITH THESE EXAMPLES

author: aless2056
author_rank: Contributor
timestamp: May 27, 2021, 01:39:30 am
content: My question is how to deal with subtraction, addition and comparison with 16bit on the PIC16F877, with 8 bit, I have no doubt, but with a number that uses 16bit complicates.

author: Dabbot
author_rank: Regular Contributor
timestamp: May 27, 2021, 02:24:04 am
content: http://www.piclist.com/techref/microchip/compcon.htm

author: nigelwright7557
author_rank: Frequent Contributor
timestamp: May 27, 2021, 02:41:06 am
content: Save yourself a lot of grief and move to C compiler.It will take care of 16 bits for you and be more like English to understand.Loads of C tutorials on youtube etc

author: james_s
author_rank: Super Contributor
timestamp: May 27, 2021, 03:05:11 am
content: I'm sure he's aware of C compilers, but he asked for help with assembly programming so pointing him to a C compiler is not going to help. Maybe he wants to learn assembly to gain a deeper understanding of how the microcontroller works. I studied 6502 assembly for the same reason, not because I thought it would be a good way to develop something on modern hardware.

author: ucanel
author_rank: Regular Contributor
timestamp: May 27, 2021, 03:51:52 am
content: Quote from: aless2056 on May 27, 2021, 01:27:32 amAll capital letters means shouting - yelling.If you have parts you could not understand from examples online feel free to ask.

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 27, 2021, 07:18:21 am
content: You always start with the lower byte.After the operation check Carry/borrow bit (status register bit 0).After a sum, if the bit is 1 (Carry) you should add 1 to the higher byte.Same happens when subtracting,but reversedIf the bit is0it means borrow, so you subtract 1 to the higher byte.In asm you'll have to use 2 variables for the high/low bytes.

author: westfw
author_rank: Super Contributor
timestamp: May 27, 2021, 09:39:42 am
content: Edited: fixed link.There's actually an app note for that:https://www.microchip.com/wwwAppNotes/AppNotes.aspx?appnote=en011000(not necessarily the best code, especially for subtraction, but... official!)Some (many) processors have separate "add with carry" and "subtract with borrow/carry" instructions that make multi-byte arithmetic much easier. These even include the "enhanced midrange" PIC16F chips, and all the PIC18F chips. But not the older architectures chips like the PIC16F877; they'll require that you examine the carry flag yourself...

author: lapi
author_rank: Contributor
timestamp: May 29, 2021, 08:11:22 pm
content: For PIC16 Microchip provides the assembly math routines in application notes AN526 (http://ww1.microchip.com/downloads/en/Appnotes/00526e.pdf) and AN544 (http://ww1.microchip.com/downloads/en/Appnotes/00544d.pdf). Even though old (from PIC16C-era) they are still valid.

author: nigelwright7557
author_rank: Frequent Contributor
timestamp: May 30, 2021, 01:30:42 am
content: Quote from: james_s on May 27, 2021, 03:05:11 amAssembler is an antiquated language and there is no need for the grief that comes with it.I have moved all my programs over to C now.The MPLAB X assembler MPASM has been made obsolete and replaced with a pile of rubbish.I took the leap to C and havent regretted it at all.

author: james_s
author_rank: Super Contributor
timestamp: May 30, 2021, 05:39:27 pm
content: Quote from: nigelwright7557 on May 30, 2021, 01:30:42 amYes you've stated your opinion, we know how you feel about this but it is irrelevant. He didn't ask about C, he asked about assembler and bringing up C doesn't answer his question at all, I'm sure he is already aware that C exists. Sometimes people enjoy learning something for the sake of learning, you may not see any value in that but just recognize that many others do.

author: jpanhalt
author_rank: Super Contributor
timestamp: May 30, 2021, 06:19:58 pm
content: Quote from: DavidAlfa on May 27, 2021, 07:18:21 amSorry to come late to this, but I don't believe the description of subtract is correct. That is, when STATUS,0 = 1 there is no borrow.Code:movlw 20sublw 10 ;subtract W from literalnop ;STATUS,0 = 0, i.e., a borrow occurred

author: DavidAlfa
author_rank: Super Contributor
timestamp: May 30, 2021, 06:29:02 pm
content: Why are there two threads asking the same thing from the same OP?Carry/Borrow is reversed:When you make an addition, if STATUS.C=1 there was Carry.When you make a subtraction, if STATUS.C=0 there was Borrow.Edit: Oh, I see I put it reversed. You know. All those bits. Yes, you were correctly fixing my mistake!

author: nigelwright7557
author_rank: Frequent Contributor
timestamp: June 01, 2021, 09:44:51 pm
content: Quote from: james_s on May 30, 2021, 05:39:27 pmWhat is he going to do when Microchip obsolete assembler too ?They have already stopped MPASM in new versions.Why go down a dead end ?

author: AlfBaz
author_rank: Super Contributor
timestamp: June 02, 2021, 01:52:14 am
content: Quote from: nigelwright7557 on June 01, 2021, 09:44:51 pmYou made your point in your fist post, can't you just leave it at that or must you be wright to the point of being disruptive?

author: nigelwright7557
author_rank: Frequent Contributor
timestamp: June 03, 2021, 01:04:32 am
content: Quote from: AlfBaz on June 02, 2021, 01:52:14 amAnd since when were you a moderator ?My advice comes from 40+ years as a programmer. Ditch the assembler.

author: DavidAlfa
author_rank: Super Contributor
timestamp: June 03, 2021, 12:38:04 pm
content: Asm is very outdated, slow to develop, pain the a** to understand quickly unless full of comments...But the OP asked that, it's been answered, and that's all.Please don't fight for personal opinions.You never know why he needs to use ASM.