Anda di halaman 1dari 8

EXPERIMENT 1: Data Manipulation

Objective:
Write assembly programs to perform simple arithmetic operation and function.

Prelab: Study and understand the principle of looping delay subroutine.

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.

Performing Addition Operations

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.

addwf F,D,A ; add WREG and F


addwfc F,D,A ; add WREG, carry bit and F (three-operand)
addlw k ; add literal k to WREG

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.

Performing Subtraction Operations

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.

Three-operand subtraction instructions are provided mainly to support the implementation of


multibyte substraction. A multibyte subtraction is also called multprecision subtraction. A

EEEB371 E11
multiprecision subtraction must be performed from the least significant byte toward the most
significant byte.

subfwb F,D,A ; subtract F from WREG withy borrow (three operand)


subwf F,D,A ; subtract WREG from F
subwfb F,D,A ; subtract WREG from F with borrow (three operand)
sublw k ; subtract WREG from literal

Performing Logic Operations

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

Applications of Logic Instructions


1. Set a few bits in a byte
2. Clear certain bits in a byte
3. Toggle certain bits in a byte

To set bits 7, 6, and 0 of PORTA to 1


movlw B11000001
iorwf PORTA,F,A

To clear bits 4, 2, and 1 of PORTB to 0


movlw B11101001
andwf PORTB,F,A

To toggle bits odd bits of PORTC


movlw B10101010
xorwf PORTC

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.

rlcf f, d, a ; rotate left f through carry

7 6 5 4 3 2 1 0 C

Figure 1.1 Operation performed by the rlcf f,d,a instruction

rlncf f, d, a ; rotate left f ( not trough carry)

7 6 5 4 3 2 1 0

Figure 1.2 Operation performed by the rlncf f,d,a instruction

rrcf f, d, a ; rotate right f through carry

C 7 6 5 4 3 2 1 0

Figure 1.3 Operation performed by the rrcf f,d,a instruction

rrncf f, d, a ; rotate right f (not through carry)

7 6 5 4 3 2 1 0

Figure 1.4 Operation performed by the rrncf f,d,a instruction

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.

Enter the following program:


#include<p18F4550.inc>

org 0x00
goto start
org 0x08
retfie
org 0x18
retfie

start CLRF TRISD,A


CLRF PORTD,A
MOVLW 0x03
MOVWF 0x10,A
MOVLW 0x0A
MOVWF 0x11,A
MOVF 0x10,W,A
ADDWF 0x11,W,A
MOVWF PORTD,A
NOP
END

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.

6. Repeat step 2 to step 4.

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.

Enter the following program:


#include<p18F4550.inc>
loop_cnt1 set 0x00
loop_cnt2 set 0x01
org 0x00
goto start
org 0x08
retfie
org 0x18
retfie
dup_nop macro kk
variable i
i=0
while i < kk
nop
i += 1
endw
endm

start CLRF TRISD,A ;configure PORTD as output


CLRF PORTD,A ;clear PORTD before applying OR
CALL DELAY ;delay for 1sec
MOVLW B'11000001' ;masking bit
IORWF PORTD,F,A ;set the respective bits

EEEB371 E15
CALL DELAY ;delay for 2sec before
CALL DELAY ;the next operation

SETF PORTD,A ;set PORTD before applying AND


CALL DELAY ;delay for 1sec
MOVLW B'11101001' ;masking bit
ANDWF PORTD,F,A ;clear the respective bits

CALL DELAY ;delay for 2sec before


CALL DELAY ;the next operation

SETF PORTD,A ;set PORTD before applying XOR


CALL DELAY ;delay for 1sec
MOVLW B'10101010' ;masking bit
XORWF PORTD,F,A ;toggle the respective bits

DELAY MOVLW D'80' ;1sec delay subroutine for


MOVWF loop_cnt2,A ;20MHz
AGAIN1 MOVLW D'250'
MOVWF loop_cnt1,A
AGAIN2 dup_nop D'247'
DECFSZ loop_cnt1,F,A
BRA AGAIN2
DECFSZ loop_cnt2,F,A
BRA AGAIN1
NOP
RETURN

END

Once you have finished entering the code, select File>Save and save the file as
EXP2logic.asm.

8. Repeat step 2 to step 4.

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.

Enter the following program:


#include<p18F4550.inc>
loop_cnt1 set 0x00
loop_cnt2 set 0x01
org 0x00
goto start
org 0x08
retfie
org 0x18
retfie
dup_nop macro kk
variable i
i=0
while i < kk
nop
i += 1
endw
endm

start CLRF TRISD,A


CLRF PORTD,A
BSF PORTD,7,A
CALL DELAY
MOVLW 0x08
LOOP RRNCF PORTD,F,A
CALL DELAY
DECFSZ WREG,W,A
BRA LOOP

DELAY MOVLW D'80' ;1sec delay subroutine for


MOVWF loop_cnt2,A ;20MHz
AGAIN1 MOVLW D'250'
MOVWF loop_cnt1,A
AGAIN2 dup_nop D'247'
DECFSZ loop_cnt1,F,A
BRA AGAIN2
DECFSZ loop_cnt2,F,A
BRA AGAIN1
NOP
RETURN

END

EEEB371 E17
Once you have finished entering the code, select File>Save and save the file as
EXP2rot.asm.

10. Repeat step 2 to step 4.


Q7. Copy and Paste ASM file to Word Doc and print.

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

Anda mungkin juga menyukai