  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:

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 register has the decimal value '150'
```

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'
;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
```

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

 Name  Email (shall not be published)  Website Notify me of new posts via emailWrite 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.

All trademarks used are properties of their respective owners.    HOT in heaven! Disclaimer Book Contents Discussion forum                NEW in heaven! New Theory: AC electric motor working principle