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.
CLRWDT
MOVF PROCESS_KW,W
SUBWF SETPOINT,W
BTFSC STATUS,Z
GOTO EXIT
MOVLW .5
SUBWF PROCESS,W
BTFSS STATUS,C
MOVLW .0
SUBWF SETPOINT,W
BTFSC STATUS,Z
GOTO EXIT
BTFSS STATUS,C
GOTO PWM_DEC
MOVLW .5
ADDWF PROCESS,W
SUBWF SEPOINT,W
BTFSC STATUS,Z
GOTO EXIT
BTFSC STATUS,C
GOTO PWM_INC
GOTO EXIT
PWM_DEC
MOVLW .0
XORWF PWM,W
BTFSC STATUS,Z
GOTO EXIT
DECF PWM,F
GOTO EXIT
PWM_INC
MOVLW .255
XORWF PWM,W
BTFSC STATUS,Z
GOTO EXIT
INCF PWM,F
GOTO EXIT
EXIT
It is same as how you do it in decimal system.
You will use pwmH and pwmL instead of PWM
and think them as if
tens and ones digits if you were in decimal systems.
When you wanna decrement this 16bit pwm value
you 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.
HAVE THE LINK WITH THESE EXAMPLES
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.
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
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.
HAVE THE LINK WITH THESE EXAMPLES
All capital letters means shouting - yelling.
If you have parts you could not understand from examples online feel free to ask.
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 reversed If the bit is 0 it means borrow, so you subtract 1 to the higher byte.
In asm you'll have to use 2 variables for the high/low bytes.
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...
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.
Assembler 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.
Assembler 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.
Yes 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.
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. If the bit is 1 it means borrow, so you subtract 1 to the higher byte.
In asm you'll have to use 2 variables for the high/low bytes.
Sorry 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.
movlw 20
sublw 10 ;subtract W from literal
nop ;STATUS,0 = 0, i.e., a borrow occurred
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!
Assembler 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.
Yes 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.
What is he going to do when Microchip obsolete assembler too ?
They have already stopped MPASM in new versions.
Why go down a dead end ?
Assembler 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.
Yes 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.
What is he going to do when Microchip obsolete assembler too ?
They have already stopped MPASM in new versions.
Why go down a dead end ?
You 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?
You 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?
And since when were you a moderator ?
My advice comes from 40+ years as a programmer. Ditch the assembler.
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.