0% found this document useful (0 votes)
79 views

Advanced Arithmetic Operations: A-First Example

This document provides examples of writing PIC assembly language programs to evaluate arithmetic expressions involving constants and bracket operations. It demonstrates using temporary memory locations to evaluate sub-expressions and the accumulator (W register) to perform operations and store final results in ports and memory locations. Several programming assignments are provided to write and test similar programs for additional expressions.

Uploaded by

Iman Haidar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Advanced Arithmetic Operations: A-First Example

This document provides examples of writing PIC assembly language programs to evaluate arithmetic expressions involving constants and bracket operations. It demonstrates using temporary memory locations to evaluate sub-expressions and the accumulator (W register) to perform operations and store final results in ports and memory locations. Several programming assignments are provided to write and test similar programs for additional expressions.

Uploaded by

Iman Haidar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Microlab File 3 – by Joseph Massoud

Advanced Arithmetic operations


(Addition/subtraction of bracket operations of constant values)

A- First example

Write a program which executes the following operations:

(D – 6 ) + (4 + 1 ) PORTD and in memory H’27’ ( 0x27).


The result on simulator should give: 0C in hexa or 12 in decimal:

(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).

MAIN MOVLW H’06’ ; W=06


SUBLW H’0D’ ; D-6 ; result saved in W …W= 07
MOVWF TEMP1 ;MOVE W to memory TEMP1 (writing )..TEMP1=07

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’.

MOVLW H’04’ ; W=04


ADDLW H’01’ ; W=05 ( 4+1)
MOVWF TEMP2; MOVE W to memory TEMP2 (writing)…TEMP2=05

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.

MOVF TEMP1,W ; read content of TEMP1 into W ….W=07


ADDWF TEMP2,W ; Add W with content of TEMP2 ( answer in W)..W=0C

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).

MOVWF PORTD ; write W into PORTD ;W remains 0C and PORTD=0C


MOVWF RESULT ; write W in REUSLT ; W=0C and RESULT=0C
LOOP GOTO LOOP
END

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

7. NOT TO FORGET THE QUICKBUILD ( till we have build succeeded)


Then we enable the simulator and open the WATCH window. We may add Wreg,
PORTD, 0x20, 0x21,0x27 ( ADD SFR) to the watch window.
We click reset than may use animate for a faster simulation. We press
pause when execution is done We should get the right answer in the respective
registers and memory addresses.

4
Microlab File 3 – by Joseph Massoud

8. Assignment 4 ( you may screen shot your own results)

Create a new file on MPLAB and save it under name lab21.asm


Write the same program above (and add your comments) , quick build,
then make the simulation using animate or step into .
Verify your results with picture in step 7.

B- Further examples

Write a program which executes the following operations:

(A + 3 ) + (5- 2) – (D – 6 ) PORTD and in memory H’28’ ( 0x28)

Solution:

9. We start by A+3 and save answer in TEMP1 ( 0x20)

MAIN MOVLW H’0A’


ADDLW H’03’
MOVWF TEMP1 ; TEMP1 content will be 0D

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.

So we have to use SUBWF instead of SUBLW.

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.

So I have to preserve what is already in W before bringing TEMP3.


I move W to TEMP4 , which becomes now the result of TEMP1+ TEMP2.

I read now TEMP3 into W, and write SUBWF with TEMP4.

MOVWF TEMP4 ; TEMP4= TEMP1 +TEMP2 = 16 decimal


MOVF TEMP3,W ; READ TEMP3 IN W
SUBWF TEMP4,W ; TEMP4 – W = TEMP4 – TEMP3 …W becomes= 16-7= 9

14. Saving results in PORTD and address h’28’ ( RESULT)

MOVWF PORTD ; PORTD = 7


MOVWF RESULT ; RESULT = 7
LOOP GOTO LOOP
END

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

16. NOT TO FORGET THE QUICKBUILD ( till we have build succeeded)


Then we enable the simulator and open the WATCH window. We may add Wreg,
PORTD, 0x20, 0x21,0X22, 0X23 0x28 ( ADD SFR) to the watch window.
We click reset than may use animate for a faster simulation. We press
pause when execution is done We should get the right answer in the respective
registers and memory addresses.

8
Microlab File 3 – by Joseph Massoud

17.Assignment 5 ( you may screen shot your own results)

Create a new file on MPLAB and save it under name lab22.asm


Write the program above (and add your comments), quick build, then
make the simulation using animate or step into.
Verify your results with picture in step 16.

18.Assignment 6 ( you may screen shot your own results)

(E – 3) - (5 – 4) – (1+ 5) + F PORTD and in memory H’29’ (0x29)

The final result should be: 19 decimal or 13 Hexa

Create a new file on MPLAB and save it under name lab23.asm


Write the program as solution for the given above (and add your
comments), quick build, then make the simulation using animate or
step into.
Verify your results.

19.Assignment 7

(9 + 3) + 4 – ( (6 - 2) – (1+ 2) ) – A PORTD and in H’2A’ (0x2A)

The final result should be: 5

Create a new file on MPLAB and save it under name lab24.asm


Write the program as solution for the given above (and add your
comments), quick build, then make the simulation using animate or
step into.
Verify your results.

GOOD LUCK

You might also like