2

I'm starting to study assembly for PIC18f4550 and I've been trying to do some activities and I don't know how to solve it. According to the activity, Using MPLABX, I'm supposed to sum 2 16bit variables and store the result on a third 16 bit variable.

I was able to sum and store the result on the third variable but I have no idea how to declare these variables in 16bit.

; TODO INSERT CONFIG CODE HERE USING CONFIG BITS GENERATOR
INCLUDE
RES_VECT CODE 0x0000 ; processor reset vector GOTO START ; go to beginning of program

; TODO ADD INTERRUPTS HERE IF USED

MAIN_PROG CODE ; let linker place main program

START

clrw        ;clear the w register

num1 equ 00000  ;declares 3 variables and their initial values
num2 equ 00001
result equ 00002
movlw H'4F'
movwf num1
movlw H'8A'
movwf num2

movf num1,W     ;moves num1 value to w register
addwf num2,W    ;sums num2 and w and stores it in w itself
movwf resultado ;moves w to the result register

END

I need to check if my code is actually correct (Im totally new on assembly) and how to declare these 3 variables in 16bit format. Thanks in advance!

Mike
  • 4,041
  • 6
  • 20
  • 37
  • Possible duplicate of [Adding with carry on PIC16 (or similar)](https://stackoverflow.com/questions/29600859/adding-with-carry-on-pic16-or-similar) – phuclv May 06 '19 at 08:26

1 Answers1

1

The PIC18 is a 8 bit controller. If you want to add two 16 bit variables you had to do it byte by byte.
Maybe you don't want to work with an absolue address and work with the linker:

 udata_acs H'000'
num1_LSB     RES  1     ;reserve one byte on the access bank
num1_MSB     RES  1     ;

You also could reserve two bytes for a name:

 udata_acs H'000'
num1     RES  2     ;reserve two bytes on the access bank

Know you could access the second byte with :

movwf num1+1

And always remember to check the carry bit to get the MSB of an addition.

Mike
  • 4,041
  • 6
  • 20
  • 37