Doubt asm 16 bits 16F877

Thread Starter

massanetabr

Joined Jun 3, 2019
24
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.
Code:
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
 
Last edited by a moderator:

Thread Starter

massanetabr

Joined Jun 3, 2019
24
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.
 

John P

Joined Oct 14, 2008
1,897
Well, it is more complicated, no question. But this is one of the advantages of using a compiler: you the programmer can just write operations like add, subtract, compare, and the compiler does the work for you of making it all happen in assembly language. It could be like this, where you could define the variables as a single byte or larger (if you'll tolerate using a GOTO):
Code:
  if (PROCESS_KW == SETPOINT)
    goto EXIT;
 

BobaMosfet

Joined Jul 1, 2009
1,776
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.
Code:
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
Stop thinking about it as 2 bytes. It's just memory. What it is and how you _LOGICALLY_ look at it are two different things. If you can only work with 8-bits at a time, you use shifting to move bits into the space you can work with.

I recommend you Google for tutorials on binary arithmetic- once you understand that, you can easily implement it in assembly. Another poster suggested fixed-point- that works well, I've written fixed point libraries as well, which you can also change precision on the fly because the decimal point is wherever you say it is.
 

BobTPH

Joined Jun 5, 2013
3,316
Add the low two bytes, then add with carry the high two bytes.

For subtract, same but use subtract and subtract with borrow.

Bob
 
Top