| ||
Mathematic Instructions This set of instructions has to do with the mathematic functions provided with for the PIC.
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:
The mathematic operation performed is:
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 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:
The mathematic operation performed is:
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:
The mathematic operation performed is:
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:
The mathematic operation performed is:
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:
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:
The mathematic operation performed is:
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: Comments
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. |
|
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 |