How to perform inversions in assembly language

Post here to teach people how to do something.

Moderators: Chuckt, Garth, bitfogav

Post Reply [phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

How to perform inversions in assembly language

Post by brad » Thu Apr 15, 2010 6:13 am

Here is a simple little way to perform a logical inversion in your assembly code.

There are a number of ways that you can go about doing this, but lets say that you wanted to read some data in from PORTB and then you wanted all of that data to get inverted, you would have a variable set up called something like PORTB_INVERTED and then you would have this piece of code:

Code: Select all

movf PORTB, w
xorlw b'11111111'
movwf PORTB_INVERTED
So all that is happening in the above code is that you are copying the contents of PORTB into the working register, you are then using the exclusive or function and are xoring the binary number '11111111' with whatever is stored in the w register.

If you are not up to speed with how an xor gate works then here is the truth table:

Image

So what we do here is we xor two bits at a time, top and bottom.

so lets say that we had 01001101 coming in on portb and we xored it, we would have this:

01001101
11111111
-----------
10110010

If you only wanted to invert the very first bit, then you just need to change all other bits when xoring to logic 0's, like this:

Code: Select all

movf PORTB, w
xorlw b'00000001'
movwf PORTB_INVERTED
now with the same portb input we get this:

01001101
00000001
-----------
01001100

Hope that was a handy hint!

Post Reply [phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Who is online

Users browsing this forum: No registered users and 12 guests