Objective:
Write assembly programs to perform simple arithmetic operation and function.
Introduction
The PIC18 MCU has instructions for performing 8-bit addition, subtraction, and
multiplication operations. Operations that deal with operands longer than eight bits can be
synthesized by using a sequence of appropriate instructions. The PIC18 MCU provides no
instruction for division, and hence this operation must also be synthesized by an appropriate
sequence of instructions. In this lab, smaller programs that perform simple computations will
be used to demonstrate how a program is written.
The PIC18 MCU has two ADD instructions with two operands and one ADD instruction with
three operands. These ADD instructions are designed to perform 8-bit additions. The
execution will affect all the flag bits of the STATUS register.
The three-operand ADD instruction will be needed in performing multibyte ADD operations.
For an 8-bit MCU, a multibyte addition is also called a multiprecision addition. A
multiprecision addition must be performed from the least significant byte toward the most
significant byte, just like numbers are added from the least significant digit toward the most
significant digit.
When dealing with multibyte numbers, there is an issue regarding how the number is stored in
memory. If the least significant byte of the number is stored at the lowest address, then the
byte order is called little-endian. Otherwise, the byte order is called big-endian. This lab and
onwards will follow the little-endian byte order to be compatible with the MPLAB IDE
software from Microchip.
The PIC18 MCU has two two-operand and two three-operand SUBTRACT instructions.
SUBTRACT instructions will also affect all the flag bits of the STATUS register. Like other
MCUs, the PIC18 MCU executes a SUBTRACT instruction by performing twos
complement addition. When the subtrahend is larger than the minuend, a borrow is needed,
and the PIC18 MCU flags this situation by clearing the C flag of the STATUS register.
EEEB371 E11
multiprecision subtraction must be performed from the least significant byte toward the most
significant byte.
The PIC18 MCU provides a group of instructions that perform logical operations.These
instruction allow the user to perform AND, OR, exclusive-OR, and complementing on 8-bit
numbers.
Mnemonic Description
operator
andwf f,d,a AND WREG with f
comf f,d,a Complement f
iorwf f,d,a Inclusive OR WREG with f
negf f,a Negate f
xorwf f,d,a Exclusive OR WREG with f
andlw k AND literal with WREG
iolw k Inclusive OR literal with WREG
xorlw k Exclusive OR literal with WREG
EEEB371 E12
Performing Rotate Instructions
The PIC18 MCU provides four rotate instructions. Rotate instructions can be used to
manipulate bit fields and multiply or divide a number by a power of 2. The operation of
multiplying by the power of 2 can be implemented by shifting the operand to the left an
appropriate number of positions, whereas dividing by the power of 2 can be implemented by
shifting the operand to the right a certain number of positions.
7 6 5 4 3 2 1 0 C
7 6 5 4 3 2 1 0
C 7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
Since the PIC18 MCU does not provide any shifting instructions, the shift operation must be
implemented by using one of the rotate-through-carry instructions. The carry flag must be
cleared before the rotate instruction is executed.
Subroutine
Subroutine is a sequence of instructions that can be called from many different places in a
program. Parameters can be passed to the subroutine so that it can perform operations on
different variables. Subroutines allow the user to use divide-and-conquer strategy to solve
complicated problems. Using subroutines can in some cases reduce the code size
significantly.
EEEB371 E13
Procedure
A. Addition Operation
1. Select File>New to open an empty editor window to type in your source code.
The task is to add two 8-bit integers stored in data registers at 0x10 and 0x11,
respectively and display the sum at PORTD.
org 0x00
goto start
org 0x08
retfie
org 0x18
retfie
Once you have finished entering the code, select File>Save and save the file as
EXP2add.asm. You may save it to any directory; you will move it, if necessary, into
the project directory in the next step.
2. Next, build and the project. Once the assembly process is complete, the Output
Window will appear.
The Output window will display BUILD SUCCEEDED. File .LST and .HEX will
be generated.
Q1. Copy and Paste ASM file to Word Doc and print.
3. To include the program configuration settings into your code: File>Export. Click OK
and save your hex file in the same folder as your project file. Click on the existing
hex file and overwrite it with this hex file. Without performing this procedure, your
hardware might not work properly.
4. Next, load the hex file into PTK40A Training Kit via PICKit 2 programmer. Your lab
instructor will verify your program.
Q2. Perform the addition operation using another set of 8-bit integers. Use
debugging mode to perform program tracing and use WATCH function to
write your observations regarding the addition operation, related registers
and PORT register. Explain the procedures taken to achieve your task.
EEEB371 E14
B. Subtraction Operation
5. Select File>New to open an empty editor window to type in your source code. The
task is to write a program to subtract the number stored in data registers at 0x20 from
the number stored at 0x10 and display the difference at PORTD. Load the following
values: 0x0B and 0x05 to memory address 0x10 and 0x20 respectively. Once you
have finished entering the code, select File>Save and save the file as EXP2sub.asm.
Q3. Copy and Paste ASM file to Word Doc and print.
Q4. Perform the subtraction operation using another set of 8-bit integers. Use
debugging mode to perform program tracing and use WATCH function to
write your observations regarding the subtraction operation, related
registers and PORT register. Explain the procedures taken to achieve your
task.
C. Logic Operation
7. Select File>New to open an empty editor window in which to type your source code.
The task is to:
a) Set bits 7, 6, and 0 of PORTD to high.
b) Clear bits 4, 2, and 1 of PORTD to low.
c) Toggle bits 7, 5, 3 and 1 of PORTD.
EEEB371 E15
CALL DELAY ;delay for 2sec before
CALL DELAY ;the next operation
END
Once you have finished entering the code, select File>Save and save the file as
EXP2logic.asm.
Q5. Copy and Paste ASM file to Word Doc and print.
Q6. Use debugging mode to perform program tracing and use WATCH function
to write your observations regarding the logic operation, related registers
and PORTD. Explain the procedures taken to achieve your task. What is
the purpose of the masking bits?
EEEB371 E16
D. Rotation Operation
9. Select File>New to open an empty editor window in which to type your source code.
The task is to create a running LED at PORTD which will rotate from left to right
with 1sec interval between each rotation.
END
EEEB371 E17
Once you have finished entering the code, select File>Save and save the file as
EXP2rot.asm.
Q8. Use debugging mode to perform program tracing and use WATCH function
to write your observations regarding the logic operation, related registers
and PORTD.
Additional task: Create a running LED at PORTD which will rotate from
right to left with 0.5sec interval between each rotation. Show the
calculations for the 0.5sec delay subroutine. Explain the procedures taken to
achieve your task. Copy and Paste ASM file to Word Doc and print. Make
sure that the ASM file is properly commented.
11. Turn off the PTK40A power supply, rearrange the USB cable back into the Training
Kit. Exit from MPLAB and PICKit 2 programmer. Shutdown your PC and rearrange
your workstation before you leave.
EEEB371 E18