Author Topic: Doubt asm 16 bits 16F877  (Read 2105 times)

0 Members and 1 Guest are viewing this topic.

Offline aless2056Topic starter

  • Contributor
  • Posts: 35
  • Country: br
Doubt asm 16 bits 16F877
« on: May 27, 2021, 12:32:02 am »
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
 

Offline ucanel

  • Regular Contributor
  • *
  • Posts: 134
  • Country: tr
Re: Doubt asm 16 bits 16F877
« Reply #1 on: May 27, 2021, 12:52:42 am »
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.
« Last Edit: May 27, 2021, 12:55:10 am by ucanel »
 

Offline aless2056Topic starter

  • Contributor
  • Posts: 35
  • Country: br
Re: Doubt asm 16 bits 16F877
« Reply #2 on: May 27, 2021, 01:27:32 am »
HAVE THE LINK WITH THESE EXAMPLES
 

Offline aless2056Topic starter

  • Contributor
  • Posts: 35
  • Country: br
Re: Doubt asm 16 bits 16F877
« Reply #3 on: May 27, 2021, 01:39:30 am »
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.
 

Offline Dabbot

  • Regular Contributor
  • *
  • Posts: 192
  • Country: au
 

Offline nigelwright7557

  • Frequent Contributor
  • **
  • Posts: 690
  • Country: gb
    • Electronic controls
Re: Doubt asm 16 bits 16F877
« Reply #5 on: May 27, 2021, 02:41:06 am »
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

 

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: Doubt asm 16 bits 16F877
« Reply #6 on: May 27, 2021, 03:05:11 am »
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.
 

Offline ucanel

  • Regular Contributor
  • *
  • Posts: 134
  • Country: tr
Re: Doubt asm 16 bits 16F877
« Reply #7 on: May 27, 2021, 03:51:52 am »
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.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5914
  • Country: es
Re: Doubt asm 16 bits 16F877
« Reply #8 on: May 27, 2021, 07:18:21 am »
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.
« Last Edit: May 30, 2021, 06:32:50 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Doubt asm 16 bits 16F877
« Reply #9 on: May 27, 2021, 09:39:42 am »
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...
« Last Edit: May 30, 2021, 12:52:43 am by westfw »
 

Offline lapi

  • Contributor
  • Posts: 21
  • Country: fi
Re: Doubt asm 16 bits 16F877
« Reply #10 on: May 29, 2021, 08:11:22 pm »
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.
 

Offline nigelwright7557

  • Frequent Contributor
  • **
  • Posts: 690
  • Country: gb
    • Electronic controls
Re: Doubt asm 16 bits 16F877
« Reply #11 on: May 30, 2021, 01:30:42 am »
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.
 

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: Doubt asm 16 bits 16F877
« Reply #12 on: May 30, 2021, 05:39:27 pm »
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.
 

Online jpanhalt

  • Super Contributor
  • ***
  • Posts: 3479
  • Country: us
Re: Doubt asm 16 bits 16F877
« Reply #13 on: May 30, 2021, 06:19:58 pm »
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.

Code: [Select]
     movlw     20
     sublw     10          ;subtract W from literal
     nop                   ;STATUS,0 = 0, i.e., a borrow occurred 
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5914
  • Country: es
Re: Doubt asm 16 bits 16F877
« Reply #14 on: May 30, 2021, 06:29:02 pm »
Why are there two threads asking the same thing from the same OP :palm:?

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!
« Last Edit: May 30, 2021, 06:32:09 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline nigelwright7557

  • Frequent Contributor
  • **
  • Posts: 690
  • Country: gb
    • Electronic controls
Re: Doubt asm 16 bits 16F877
« Reply #15 on: June 01, 2021, 09:44:51 pm »
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 ?



 

Online AlfBaz

  • Super Contributor
  • ***
  • Posts: 2184
  • Country: au
Re: Doubt asm 16 bits 16F877
« Reply #16 on: June 02, 2021, 01:52:14 am »
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?
 

Offline nigelwright7557

  • Frequent Contributor
  • **
  • Posts: 690
  • Country: gb
    • Electronic controls
Re: Doubt asm 16 bits 16F877
« Reply #17 on: June 03, 2021, 01:04:32 am »
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.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5914
  • Country: es
Re: Doubt asm 16 bits 16F877
« Reply #18 on: June 03, 2021, 12:38:04 pm »
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.
« Last Edit: June 03, 2021, 12:39:50 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf