Home     Contact     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
   
 Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   



Mathematic Instructions

This set of instructions has to do with the mathematic functions provided with for the PIC.

Instruction Description
ADDLW k Add the content of W register with a literal value and store the result in the W register

ADDWF f , d Add the content of W register with the content of a file register 'f'. If d is 0 the result is stored in the W register, if d is 1 the result is stored back to the file register 'f'.

SUBLW k Subtract the content of W register from a literal value and store the result in the W register

SUBWF f , d Subtract the content of W register from the content of a file register 'f'. If d is 0 the result is stored in the W register, if d is 1 the result is stored back to the file register 'f'.

COMF f , d The contents of the file register 'f' are complemented. If d is 0 the result is stored in the W register, if d is 1 the result is stored back to the file register 'f'.

Now, let's see the instructions one by one:



ADDLW k

With this instruction, you add the contents of the W register with a literal 8-bit value. The result is stored back to the W register. The status that may be affected from this instruction are:

  • C - Carry
  • DC - Digit Carry
  • Z - Zero

The mathematic operation performed is:

  • W = W + k
    Where 0 <= k <= 255


Example
        movlw d'100'   ;The W register has now the decimal value '100'
        addlw d'50'    ;The decimal value '50' is added to the W register.
                       ;Now the W registe
r has the decimal value '150'



ADDWF f , d

With this instruction, you add the contents of the W register with the contents of a file register. If d is 0 the result is stored in the W register, if d is 1 the result is stored back to the file register 'f'. The status that may be affected from this instruction are:

  • C - Carry
  • DC - Digit Carry
  • Z - Zero

The mathematic operation performed is:

  • Destination (d) = W + f
    Where 0 <= f <= 127


Example
;This example requires that you have declare a file register for example
;TempRegister          equ 0x20
        movlw d'100'         ;The W register has the value '100'
        movwf TempRegister   ;The TempRegister has now the value '100'
        movlw d'50'          ;The W register has the value '50'
        addwf TempRegister,1 ;The values of W and TempRegister are added
                             ;and the result (150) is stored to TempRegister



SUBLW k

With this instruction, you subtract the content of W register from a literal value. The result is stored back to the W register. The status that may be affected from this instruction are:

  • C - Carry
  • DC - Digit Carry
  • Z - Zero

The mathematic operation performed is:

  • W = k - W
    Where 0 <= k <= 255


Example
        movlw d'30'   ;The W register has now the value '30'
        sublw d'100'  ;The content of W register is subtracted from the value '100'
                      ;Now the W register has the decimal value '70'



SUBWF f , d

With this instruction, you subtract the contents of the W register from the contents of a file register. If d is 0 the result is stored in the W register, if d is 1 the result is stored back to the file register 'f'. The status that may be affected from this instruction are:

  • C - Carry
  • DC - Digit Carry
  • Z - Zero

The mathematic operation performed is:

  • Destination (d) = f - W
    Where 0 <= f <= 127


Example
;This example requires that you have declare a file register for example
;TempRegister          equ 0x20
        movlw d'100'         ;The W register has the value '100'
        movwf TempRegister   ;The TempRegister has now the value '100'
        movlw d'30'          ;The W register has the value '30'
        subwf TempRegister,1 ;The value of W is subtracted from TempRegister 
                             ;and the result (70) is stored back to TempRegister


Is the result of a subtraction positive, zero or negative?

Many times you need to know if the result from a subtraction, using the sublw or the subwf instruction, is positive, zero or negative. This can be achieved by watching the Carry (C) and the Zero (Z) status:

Result Carry Zero
Positive 1 0
Zero 1 1
Negative 0 0



COMF f , d

With this instruction, you complement the contents of a file register. If d is 0 the result is stored in the W register, if d is 1 the result is stored back to the file register 'f'. The status that may be affected from this instruction are:

  • Z - Zero

The mathematic operation performed is:

  • Destination (d) = .NOT. f
    Where 0 <= f <= 127


Example
;This example requires that you have declare a file register for example
;TempRegister          equ 0x20
        movlw b'11000101'    ;The W register has the value '11000101'
        movwf TempRegister   ;The TempRegister has now the value '11000101'
        comf TempRegister,1  ;The values of TempRegister is complimented
                             ;and the result (00111010) is stored to TempRegister






Confirm your knowledge

There is an online test to check your knowledge on this page. You may reveal the test with the following button:






Previous page ---- Next page



Go back to the book contents

Go to the discussion forum of this book





 

Comments

  Name

  Email (shall not be published)

  Website

Notify me of new posts via email


Write your comments below:
BEFORE you post a comment:You are welcome to comment for corrections and suggestions on this page. But if you have questions please use the forum instead to post it. Thank you.


      

  • At 12 May 2012, 18:36:47 user PASHA HASSAN wrote:   [reply @ PASHA HASSAN]
    • GOD bless all of your team.Who has done an amazing job.Very to the point,precise and easy to understand.I really appreciate.
     






    No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise without the prior written permission of the author.

    Read the Disclaimer


    All trademarks used are properties of their respective owners.
    Copyright © 2007-2009 Lazaridis Giorgos.
    All rights reserved.






     HOT in heaven!


  • Disclaimer
  • Book Contents
  • Discussion forum

  • Basics
  • What will you need
  • Choosing the right PIC
  • The MPLAB
  • Getting familiar with the MPLAB environment
  • Creating a new project
  • Open and close projects
  • Creating new files and including them in the project
  • Your very first assembly program
  • Compile a program and transfer to the PIC
  • Section 1: Beginner's theory
  • Memory Organization
  • The Data Memory Organization
  • The Program Memory Organization
  • The instructions
  • General knowledge about instructions
  • Value Loading Instructions
  • Program Flow Instructions
  • Mathematic Instructions
  • Logic Function Instructions
  • Bit Orientated Instructions
  • Byte Orientated Instructions
  • Miscellaneous Instructions
  • The Basic Special Function Registers
  • The Status Register
  • The Option_Reg Register
  • The TRIS and PORT registers
  • Beginner's PIC Tutorials
  • How to use our PIC Tutorials
  • A Pushbutton turning an LED on and off
  • A Simple LED Flasher
  • Interfacing Multiple Switches - The internal Pull-Up resistors
  • An LED Sequencer
  • Interface a Single 7seg Digit
  • Interface Multiple 7seg Digits
  • A 3-digits Decimal Counter
  • A Clever Button
  • Section 2: Intermediate theory
  • Instruction Cycle Duration and Calculated Delays
  • The Timer Modules - Timer0
  • The Timer Modules - Timer1
  • The Timer Modules-Timer2



  • NEW in heaven!



    New Theory: AC electric motor working principle






     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS   

    Site design: Giorgos Lazaridis
    © Copyright 2008
    Please read the Terms of services and the Privacy policy