Arithmetic and Logic Instructions. Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

Similar documents
Arithmetic,logic Instruction and Programs

PIC Architecture & Assembly Language Programming. Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

The University of Texas at Arlington Lecture 7

ECE Test #1: Name

Section 31. Instruction Set


Chapter 4 Sections 1 4, 10 Dr. Iyad Jafar

4.5.1) The Label Field ) The Mnemonic Field. 4.5) Assembly Language Program Structure A PIC18 ALP consists of 3 type of statements:

Lecture (04) PIC16F84A (3)

Assembly Language Instructions

16.317: Microprocessor Systems Design I Fall 2013 Exam 3 Solution

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT. Spring 2006

PIC 16F84A programming (II)

Chapter 5. Problems All programming problems should include design pseudo code either as a separate design document on embedded comments in the code.

Instuction set

PIC16F87X 13.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

LAB WORK 2. 1) Debugger-Select Tool-MPLAB SIM View-Program Memory Trace the program by F7 button. Lab Work

Chapter 13. PIC Family Microcontroller

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 5 Solution

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

EEE111A/B Microprocessors

16.317: Microprocessor-Based Systems I Fall 2012

Lesson 14. Title of the Experiment: Introduction to Microcontroller (Activity number of the GCE Advanced Level practical Guide 27)

TOPIC 3 INTRODUCTION TO PIC ASSEMBLY LANGUAGE. E4160 Microprocessor & Microcontroller System. Prepared by : Puziah Yahaya JKE, POLISAS / DEC 2010

Flow Charts and Assembler Programs

ALU and Arithmetic Operations

Embedded Systems. PIC16F84A Sample Programs. Eng. Anis Nazer First Semester

ELEG3923 Microprocessor Ch.6 Arithmetic and Logics

SOLUTIONS!! DO NOT DISTRIBUTE!!

16.317: Microprocessor-Based Systems I Summer 2012

Architecture. Harvard Architecture. PIC18 Review. Architecture, Instruction Set, and Assembly Language Programming

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002. Semester 2. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J2. Time allowed: 3 Hours

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE6008 Microcontroller based system design

PIC16C63A/65B/73B/74B

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002 RESIT. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J1. Time allowed: 3 Hours

Q1: Multiple choice / 20 Q2: Protected mode memory accesses. / 40 Q3: Reading PIC. / 40 assembly language TOTAL SCORE / 100

movwf prevcod ; a new button is pressed - rcnt=3 movwf (mtx_buffer+1) movlw 3 movwf rcnt

The University of Texas at Arlington Lecture 5

Fortune. Semiconductor Corporation 富晶半導體股份有限公司. 8-bit MCU with 1k program ROM, 64-byte RAM, 1 R2F module and 3 13 LCD driver. TD Rev. 1.

16.317: Microprocessor-Based Systems I Spring 2012

ECE Homework #3

PIC PROGRAMMING START. The next stage is always the setting up of the PORTS, the symbol used to indicate this and all Processes is a Rectangle.

Hitchhiker s Guide to FlashForth on PIC18 Microcontrollers

16.317: Microprocessor Systems Design I Spring 2015

CHAPTER 6 ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS

PIC16F84A 7.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

ET355 Microprocessors Thursday 6:00 pm 10:20 pm

Outlines. PIC Programming in C and Assembly. Krerk Piromsopa, Ph.D. Department of Computer Engineering Chulalongkorn University

Chapter 6. Problems All programming problems should include design pseudo code either as a separate design document on embedded comments in the code.

Dept. of Computer Engineering Final Exam, First Semester: 2016/2017

Interfacing PIC Microcontrollers. ADC8BIT2 Schematic. This application demonstrates analogue input sampling

/ 28 HLL assembly Q4: Conditional instructions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

/ 40 Q3: Writing PIC / 40 assembly language TOTAL SCORE / 100 EXTRA CREDIT / 10

Math Utility Routines. Name Comments Program Memory Instruction Cycles Scratch RAM W Register

Input/Output Ports and Interfacing

Week1. EEE305 Microcontroller Key Points

University of Jordan Faculty of Engineering and Technology Department of Computer Engineering Embedded Systems Laboratory

16.317: Microprocessor Systems Design I Fall 2015

CONNECT TO THE PIC. A Simple Development Board

PTK8756B 8 Bit Micro-controller Data Sheet

MPASM and MPLINK PICmicro QUICK REFERENCE GUIDE

8051 Programming: Arithmetic and Logic

TUTORIAL Assembly Language programming (2)

LAB WORK NO. 2 THE INTERNAL DATA REPRESENTATION

AN587. Interfacing to an LCD Module. Interfacing to an LCD Module INTRODUCTION OPERATION CONTROL SIGNAL FUNCTIONS TABLE 2: CONDITIONAL ASSEMBLY FLAGS

16.317: Microprocessor Systems Design I Fall 2014

Controller Provides Display of Monitored Process Deviation

Principle and Interface Techniques of Microcontroller

D:\PICstuff\PartCounter\PartCounter.asm

An Introduction & Guide to JALV2

Hitchhiker s Guide to FlashForth on PIC and AVR Microcontrollers

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

Binary Outputs and Timing

S w e d i s h c r. w e e b l y. c o m j a l i l a h m e l i v e. c o m Page 1

Microcontroller. Instruction set of 8051

Inverted Pendulum Microprocessor and FPGA Manual Sheldon Logan July 3, 2006

Computer Engineering Capstone Design Summer A : Handheld Video Games

/* PROGRAM FOR BLINKING LEDs CONEECTED TO PORT-D */

Lesson 4 Fun with W and F

PIC Discussion. By Eng. Tamar Jomaa

MPASM 5.46 AT89C2051_PROGRAMMER.ASM :21:50 PAGE 1 VALUE

APPLICATION NOTE 2361 Interfacing an SPI-Interface RTC with a PIC Microcontroller

Section 4. Architecture

Arithmetic Instructions

EXPERIMENT 4: Parallel Input/Output. Objectives Introduction to the Parallel Input/Output (I/O) Familiarization to Interfacing

More (up a level)... Connecting the Nokia 3510i LCD to a Microchip PIC16F84 microcontroller


A Better Mouse Trap. Consumer Appliance, Widget, Gadget APPLICATION OPERATION: Ontario, Canada

Micro II and Embedded Systems

Chapter 2 Number System

Chapter 3 BRANCH, CALL, AND TIME DELAY LOOP

Introduction to PICL and PICLab

EEM336 Microprocessors I. Arithmetic and Logic Instructions

When JP1 is cut, baud rate is Otherwise, baud rate is Factory default is that JP1 is shorted. (JP1 is jumper type in some model)

Tutorial for PICMON18 Debug Monitor

SOLAR TRACKING SYSTEM USING PIC16F84A STEPPER MOTOR AND 555TIMER

Introduction. Embedded system functionality aspects. Processing. Storage. Communication. Transformation of data Implemented using processors

Solutions - Homework 2 (Due date: October 4 5:30 pm) Presentation and clarity are very important! Show your procedure!

Transcription:

Arithmetic and Logic Instructions Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan chanhl@mail.cgu.edu.tw

Find the sum of the values from 40H to 43H. Put the sum in filereg locations 6 (low byte) and 7 (high byte) L_Byte EQU 0x6 H_Byte EQU 0x7 0x0 MOVWF H_BYTE 0x40 ADDLW 0x41 BNC N_2 INCF H_byte, F N_2 ADDWF 0x42, W N_3 ADDLW 0x43,W BNC N_4 INCF H_byte, F N_4 MOVWF L_Byte 2

Add 16-bit numbers of 3CE7H and 3B8DH. Put the sum in filereg locations 6 (low byte) and 7 (high byte) L_Byte EQU 0x6 H_Byte EQU 0x7 0x8D MOVWF L_BYTE 0x3B MOVWF H_BYTE ADDWF ADDWFC 0xE7 L_BYTE, F 0x3C H_BYTE, F 3

BCD (binary coded decimal) number system Unpacked BCD 59 is represented by 0000 0101 and 0000 1001 Packed BCD 59 is represented by 59H 0101 1001 Efficient in storing data 4

DAW instruction 0x47 ; WREG = 47H ADDLW 0x25 ; WREG = 6CH, DC = 0, C = 0 DAW ; Adjust for BCD addition by adding 6 (WREG=72H) C =0 0x09 ; WREG = 09H ADDLW 0x08 ; WREG = 11H, DC = 1, C = 0 DAW ; Adjust for BCD addition by adding 6 (WREG=17H) C =0 0x52 ; WREG = 52H ADDLW 0x87 ; WREG = D9H, DC = 0, C =0 DAW ; Adjust for BCD addition by adding 60H (WREG=39H) C =1 0x57 ; WREG = 57H ADDLW 0x77 ; WREG = CEH, DC = 0, C =0 DAW ; Adjust for BCD addition by adding 66H (WREG=34H) C =1 Add 0110 to lower 4 bits if lower nibble > 9 or if DC = 1 Add 0110 to upper 4 bits if upper nibble >9 or if C = 1 5

Subtraction of unsigned numbers 0x23 SUBLW 0x3F ; WREG = 3F WREG 3F 23 = 3F + 2 s complement of 23 = 3F + DC + 1 = 1 1C C = 1 N = 0 (D7 is negative flag) 6

Subtract 4C 6E using SUBWF MYREG EQU 0x20 0x4C MOVWF MYREG 0x6E SUBWF MYREG, W ; WERG = MYREG - WREG BNN NEXT NEGF WREG ; take 2 s complement NEXT MOVWF MYREG 4C 6E = 4C + 91 + 1 = DE C = 0 N = 1 (negative, FF-DE+1, -22) 7

Subtract two 16-bit numbers: 2762H - 1296H. Put the difference in filereg locations 6 (low byte) and 7 (high byte) L_Byte EQU 0x6 H_Byte EQU 0x7 0x62 MOVWF L_BYTE 0x27 MOVWF H_BYTE SUBWF SUBWFC 0x96 L_BYTE, F ; F = F W = 62 96 = CCH ; C = borrow = 0, N = 1 0x12 H_BYTE, F ; F = F W borrow ; = 27 12 1 = 14H ; 2762H - 1296H = 14CCH 8

Multiplication of unsigned numbers MULLW 0x25 0x65 25H x 65H = E99H special function registers PRODH = 0EH PRODL = 99H 9

Division of unsigned numbers by repeat subtraction NUM EQU 0x19 ; reminder MYQ EQU 0x20 ; quotient MYNMB EQU D 95 ; numerator MYNDEN EQU D 10 ; denominator CLRF MYQ MYNMB MOVWF NUM MYDEN B1 INCF MYQ, F SUBWF NUM, F BC B1 ; keep subtraction until C = 0 DECF MYQ, F ADDWF NUM, F 10

Signed number arithmetic Positive vs. negative numbers Range for 8-bit number Unsigned numbers: 0 ~ 255 Signed numbers: -128 ~ 127 11

Overflow in signed number operation OV is set if either two conditions occurs 1. A carry from D6 to D7 but no carry out of D7 (C=0) 2. A carry from D7 out (C=1) but no carry from D6 to D7 ADDLW +D 96 +D 70 ; 0110 0000 + 0100 0110 = 1010 0110 (A6H) ; N = 1, OV = 1, sum = -90 ; condition 1 ADDLW -D 128 -D 2 ; 1000 0000 + 1111 1110 = 0111 1110 (7EH) ; N = 0, OV = 1, sum = 126 ; condition 2 12

Overflow in signed number operation (cont.) ADDLW -D 2 -D 5 ; 1111 1110 + 1111 1011 = 1111 1001 (F9H) ; N = 1, OV = 0, sum = -7 ADDLW +D 7 +D 18 ; 0000 0111 + 0001 0010 = 0001 1001 (19H) ; N = 0, OV = 0, sum = 25 ; condition 2 13

Logic operations ANDLW K IORLW K ; Inclusive OR XORLW K ; Exclusive OR 14

RB2 and RB5 of PORTB are used to control an outdoor light and indoor light respectively. BCF TRISB, 2 BCF TRISB, 5 BSF PORTB, 2 BCF PORTB, 5 BCF TRISB, 2 BCF TRISB, 5 B 00000100 IORWF PORTB, F B 11011111 ANDWF PORTB, F 15

Test PortB to see whether it has value 45H CLRF CLRF SETF XORWF BNZ MOVWF EXIT TRISC PORTC TRISB 0x45 PORTB, W EXIT 0x99 PORTC 16

Compare instructions 17

Monitor PORTD for the value 63H SETF BACK CPFSEQ BRA TRISD 0x63 PORTD BACK 18

PORTD is connected to a temperature sensor. Test it for the value 75 if (T > 75) GREG=T; else if (T<75) LREG=T; else WREG=T; LREG EQU 0x20 GREG EQU 0x21 SETF TRISD D 75 CPFSGT PORTD BRA LEQ MOVFF PORTD, GREG BRA OVER LEQ CPFSLT PORTD BRA OVER MOVFF PORTD, LREG OVER 19

Rotate instructions 7 6 5 4 3 2 1 0 C 7 6 5 4 3 2 1 0 Figure 2.17 Operation performed by the rlcf f,d,a instruction Figure 2.18 Operation performed by the rlncf f,d,a instruction C 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 Figure 2.19 Operation performed by the rrcf f,d,a instruction Figure 2.20 Operation performed by the rrncf f,d,a instruction 20

Do multiplication/division of unsigned numbers by rotating left/right through carry MYREG EQU 0x20 0x25 MOVWF MYREG MYREG EQU 0x20 0xD5 MOVWF MYREG BCF STATUS, C BCF STATUS, C RLCF MYREG, F RRCF MYREG, F BCF STATUS, C BCF STATUS, C RLCF MYREG, F RRCF MYREG, F 21

Find number of 1s in a given byte R1 EQU 0x20 COUNT EQU 0x21 VALREG EQU 0x22 CLRF R1 0x8 MOVWF RCNT 0x97 MOVWF VALREG AGAIN RLCF VALREG, F BNC NEXT INCF R1, F NEXT DECF COUNT, F BNZ AGAIN 22

SWAPF filereg, d MYREG EQU MOVWF SWAPF 0x20 0x72 MYREG MYREG, F 23

Reference M.A. Mazidi, R.D. Mckinlay, D Causey, PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18, Pearson Education Inc., 2008. Han-Way Huang, PIC Microcontroller: An Introduction to Software and Hardware Interfacing, Thomson Delmar Learning, 2005. 24