Advanced Arithmetic Operations: A-First Example
Advanced Arithmetic Operations: A-First Example
A- First example
(13-6) + (4+1) ) = 12
Solution:
1. The CONFI and INIT parts remain the same as before because they set the
direction of ports. So the change starts at MAIN.
2. We start by instructions of first bracket (D-2) and we write the answer (save) in
a temporary memory address (RAM) of my choice, let us say H’20’ (0x20) .
Instead of using numbers for memory location we define names for them,
(TEMP1 EQU 0x20) on the top of the program .
(TEMP1 could be indicating temporary 1).
3. Then we do the second bracket (4+1) and write the answer in another memory
location, let us say H’21’ (0x21). We define TEMP2 as name for address H’21’.
1
Microlab File 3 – by Joseph Massoud
4. Now we have to read the content of TEMP1 and add it with content of TEMP2.
The result of this addition will be automatically saved in W.
Therefore we start by reading TEMP1 into W and then adding W with TEMP2.
Thus we are using W as a bridge and accumulator.
5. Now we need to save the result of W in PORTD and in memory address H’27’, as
per the given.
I will define a name for 0x27 , let us say RESULT ( RESULT EQU 0x27).
2
Microlab File 3 – by Joseph Massoud
6. The full program on MPLAB will look as follows (ALWAYS USE CAPITAL)
3
Microlab File 3 – by Joseph Massoud
4
Microlab File 3 – by Joseph Massoud
B- Further examples
Solution:
10. Then we do the second bracket and we save result in TEMP2 ( 0X21)
MOVLW H’02’
SUBWF H’05’
MOVWF TEMP2 ; TEMP2 content will be 03
11. Now the third bracket and save result in TEMP3 ( 0X22)
MOVLW H’06’
SUBWF H’0D’
MOVWF TEMP3 ; TEMP3 content will be 07
5
Microlab File 3 – by Joseph Massoud
12. Then we read TEMP1 in W and add it with TEMP2 ( result will be in W)
MOVF TEMP1,W
ADDWF TEMP2,W ; W=TEMP1 +TEMP2 = 0D +03=10 hexa=16 decimal
13. We have now a subtraction between 2 files : ( TEMP1 + TEMP2) and TEMP3.
And SUBWF (like SUBLW ) also executes the instruction in a reversed way
(bringing the second party first to W); so I need to bring TEMP3 to W.
6
Microlab File 3 – by Joseph Massoud
15. The full program on MPLAB will look as follows (ALWAYS USE CAPITAL)
7
Microlab File 3 – by Joseph Massoud
8
Microlab File 3 – by Joseph Massoud
19.Assignment 7
GOOD LUCK