Subwf, Sublw

Status
Not open for further replies.

EXODUS

New Member
Hello all it's me once again Iam having a hard time trying to understand how the command SUBLW and SUBWF works all the books im reading and the instruction i get in class is not doing it I need a good example of the command in action for example if W, has 10 in it and F,has 5 your going to get negative 5 I dont know I dont want to confuse anyone please help if you can. and Ive alredy looked at the flash presentation on the PIC
 
The answer is 11111011 in 2’s compliment and C = 0 (result is negative). You can try it with the Windows calculator set to scientific. Enter 5 minus 10 in decimal if you like and change it to binary and byte just before you hit =.
 
The mnemonic for one of these instructions is wrong.

SUBLW is NOT Subtract Literal from Work. It is Subtract Work from Literal.

Apart from that it does what it says. What is it that you don't understand.

Examples of how you would use these instructions,

When writing a clock program,
Code:
	movlw	d'60'		;W = 60
	subwf	Seconds,W	;W = Seconds-60
	btfss	STATUS,Z	;is it zero?
	goto	DoneTime	;No, so finished
	incf	Minutes,F	;yes so increment minutes

;or you could do it the other way round
	movfw	Seconds
	sublw	d'60'

I posted an example of 16 comparison in this thread that you may want to have a look at.

Mike.
 
Say F = 5 = 0101 and W = 10 = 1010

you have to write W in 2's complement :
1010 (invert each bit)=> 0101 (add 1)=> 0110

now add the two
0101
+ 0110
= 1011

now because your MSB = 1 you have to do 2's complement on the answer
again and that gives you 0110 which is 5 and you know it was a negative
number because of the MSB = 1

Hope this will help you!
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…