Individual bit manipulation techniques in assembly

The unit of information is the bit. It takes only one binary digit, either the value 0 or the value 1. A byte is comprised of 8 bits.

People created the byte as an ordered set of 8 bits. And they used the byte as the unit of addressable information. By that, I mean that each memory address in a computer points to a byte, not a bit.

So, memory address number 0 points to the first byte in memory. Memory address number 1 points to the second byte in memory. And so on. And the same holds true for storage. In storage, whole bytes are stored and retrieved, not individual bits.

This is for efficiency. Thus, a byte is treated as the smallest bundle of information. I guess, when people first created the bundles of information that they would use, they made an ingenious decision.

The unit of information was the bit, but to use a small ordered bundle of bits to hold an encoding of a letter or number or punctuation symbol, they would need more than four bits. This is because 4 bits correspond to 2^4=16 different permutations. And the English alphabet alone is 26 letters. Add 10 for the individual digits of the numbers (0 to 9) and add more for the punctuation symbols and other control characters that they needed for the operation of the computer (line feeds, etc.), people saw that 2^7=128 permutations were needed, which correspond to 7 bits per byte, since one byte would correspond to a letter or number or punctuation symbol or control character.

Electronic components were scarce the days those decisions were made. So 7 bits per byte would be perfectly adequate. But people made the ingenious decision to use 8 bits per byte. 8 is a power of 2, so people could calculate sizes in either bits or bytes and use the same base (2). Also, a byte could represent more symbols of information (2^8=256 permutations). Another huge reason that a byte was made to comprise of 8 bits was that in this way a byte could be represented with two hexadecimal numbers. Let me give you an example. The byte 10011110 can be represented as 9E in hexadecimal notation, since 1001 in binary is 9 in hexadecimal, and 1110 in binary is E in hexadecimal.

So, people used the concept of the byte as an ordered set of 8 bits to hold the smallest amount of information that they would address or store. A byte (which is comprised of 8 bits) would represent a character or a number from 0 to 9 or a punctuation symbol or a computer control character or another graphical symbol from a small set of predefined symbols. And of course, if a single byte would be considered to contain a purely numeric value, this would be from 00000000 in binary or 0 in decimal to 11111111 in binary or 255 in decimal.

According to all the above, it will be no surprise to learn that assembly commands also operate on full bytes. So, how will you be able to manipulate a byte’s individual bits, if the need arises?

In this blog post, I will teach you exactly that. I will show you how to use assembly commands to manipulate each individual bit of a byte. All we need in order to understand the concepts, is to study the truth tables of AND, OR, and XOR.

AND

Truth table for AND
-------------------------------------
Input bit   Input bit   Resulting bit
0           0           0
0           1           0
1           0           0
1           1           1

From the above table, we can gather the following:

If you AND any bit with 0, the result is 0.

If you AND any bit with 1, the result is the bit.

Therefore, AND can be used to set any bit of a byte to 0, regardless of the bit’s previous value.

Here is how: Suppose we have a byte and we want to set one or more of its bits to 0. All we need to do is to AND the byte with a byte that has 0’s in the bit positions that we want to set to 0 and 1’s in the bit positions that we want to remain unchanged.

Example question: Given the byte 10110010, set both the second bit from the right and the fourth bit from the right to 0.

Answer:

MOV AL, 10110010b
AND AL, 11110101b

The result is in AL (and it should be equal to 10110000).

(In the two assembly commands above, please note the suffix b, which stands for “binary”, and is used to denote that the byte is given in its 8 binary bits form.)

OR

Truth table for OR
-------------------------------------
Input bit   Input bit   Resulting bit
0           0           0
0           1           1
1           0           1
1           1           1

From the above table, we can gather the following:

If you OR any bit with 0, the result is the bit.

If you OR any bit with 1, the result is 1.

Therefore, OR can be used to set any bit of a byte to 1, regardless of the bit’s previous value.

Here is how: Suppose we have a byte and we want to set one or more of its bits to 1. All we need to do is to OR the byte with a byte that has 1’s in the bit positions that we want to set to 1 and 0’s in the bit positions that we want to remain unchanged.

Example question: Given the byte 10110010, set both the second bit from the right and the fourth bit from the right to 1.

Answer:

MOV AL, 10110010b
OR AL, 00001010b

The result is in AL (and it should be equal to 10111010).

XOR

Truth table for XOR
-------------------------------------
Input bit   Input bit   Resulting bit
0           0           0
0           1           1
1           0           1
1           1           0

From the above table, we can gather the following:

If you XOR any bit with 0, the result is the bit.

If you XOR any bit with 1, the result is the bit flipped.

Therefore, XOR can be used to flip any bit of a byte.

Here is how: Suppose we have a byte and we want to flip one or more of its bits. All we need to do is to XOR the byte with a byte that has 1’s in the bit positions that we want to flip and 0’s in the bit positions that we want to remain unchanged.

Example question: Given the byte 10110010, flip both the second bit from the right and the fourth bit from the right.

Answer:

MOV AL, 10110010b
AND AL, 00001010b

The result is in AL (and it should be equal to 10111000).

Conclusion

With AND, OR, and XOR, we can manipulate individual bits, even though these commands (as all assembly commands) operate on full bytes.

All you have to remember is these six facts:

• AND a bit with 0 and the result is 0.
• OR a bit with 1 and the result is 1.
• XOR a bit with 1 and the bit is flipped.
• AND a bit with 1 and the result is the original bit.
• OR a bit with 0 and the result is the original bit.
• XOR a bit with 0 and the result is the original bit.

I cannot let you leave like this. Here is one more example for the road:

Example question: Write a program that transforms the byte stored in the AL register. The byte should be transformed as follows: Its first bit from the right must be set to 0. Its second bit from the right must be set to 1. Its third bit from the right must be flipped. That’s it. Let’s roll. And let’s be careful out there.

Answer: These transformations cannot be performed with one operation. We will need three distinct operations. Here they are:

AND AL, 11111110b
OR AL, 00000010b
XOR AL, 00000100b

And I hope that you find obvious the fact that these operations (assembly commands) can be performed in any order. After all three operations are performed, the result will be in the AL register.

Advertisements
Posted in Development

The importance of “A living programmable biocomputing device based on RNA”

Please visit A living programmable biocomputing device based on RNA

Quoting:

“…have developed a living programmable “ribocomputing” device…

…based on networks of precisely designed, self-assembling synthetic RNAs (ribonucleic acid).

The RNAs can sense multiple biosignals and make logical decisions to control protein production with high precision.”

This will lead to numerous highly effective drugs that will operate “within the cell” and will be able to correct the deficiencies of each individual’s DNA.

Here is how things work.

  • The human body is made up of cells.
  • Each individual cell has a nucleus.
  • Inside the nucleus exists a DNA molecule.
  • This DNA molecule is exactly the same in every nucleus, in every cell.
  • This DNA molecule contains the instructions on how the cell will produce proteins.
  • Proteins are the structural and functional ingredients of the human body.
  • The STRUCTURAL and FUNCTIONAL ingredients of the human body.
  • This means that proteins control how the body is structured and how the body functions.
  • So, proteins control everything!
  • And the “recipe” for their formation is contained in the DNA.

So, if the DNA molecule of a person has a defect, the corresponding protein that will be produced from that DNA part will be defective and the corresponding structural or functioning part of the human body will suffer.

Now, these drugs will be made such as to control the protein formation by bypassing the defective DNA part. For these specific drugs, the tell-tale sign, from which they discover the anomaly, is found in the RNA of the cell.

These researchers have made very important progress.

Posted in Science

The most important facts about code reuse

Code reuse can happen in two levels, one simple and one advanced.

At the simple level, you create a function and you use that function instead of copying and pasting.

At the advanced level, in OOP you use inheritance, whereas in functional programming you use function composition.

Posted in Development

And justice for all

My late father went to court on several occasions, either as a witness or as the accused. I will tell you about these court battles in other blog posts, but they were mostly unimportant.

What I want to point out in this blog post is quite important. When my late father came back from the court one day, he told me that he went to the court quite early that day. The “court” here in Athens,. Greece is in a large space where a lot of court buildings are located. Being there early, he killed his time by strolling around the court buildings. What he noticed, saddened him.

And when he came home, he told me about it. He told me that all the counters in the court rooms were set up to point to 666.

As I understand it, each court building has a lot of court rooms, and each court room has an electronic counter that denotes the number of the case being in session at the time. One would expect that when the court is not in operation, each counter may display 000, or an arbitrary number, or the number of the last case that was handled, or nothing.

But in the court rooms in Athens, Greece, the number all counters displayed when not in operation was 666.

What does that mean? Well, it means that Masons control the court system and the judicial system. It means that Masons favor other Masons when it comes to court rulings.

Why do Masons pick the number 666 as one of their symbols? Well, they do it for many reasons, and I will not claim that I know all of them. But 666 is a number that has a lot of “powers”. One of its “powers” is that it is made from the number 6 repeated three times. And from ancient times comes the notion that when you repeat something three times, you augment its power.

So, what are the “powers” of the number 6? Well, first and most of all, 6 is the first perfect number. A perfect number is a number that is equal to the sum of its divisors. Indeed, 6’s divisors are 1, 2, and 3. And 1+2+3 = 6.

Another thing about 6 is its shape. It resembles a swirl and a spiral. A spiral corresponds to an enormous body of mathematical knowledge and “power” that to analyze would need a very lengthy blog post. I believe that the Masons would agree with me if I said that the mathematical analysis of the spiral would resemble the mathematical analysis of the Divine.

There is another thing about the spiral shape that I think Masons adore.  A spiral depicts what I call “intelligent augmentation”. Let me explain. Take a snake. Put it on the floor. Observe it. It is just something that is long. And what will happen to it when it needs to grow? It will grow mostly in the direction that already defines it. So, it will grow by becoming longer. Well, this method of augmentation, this method of progress, may be a little bit “naive”. The snake keeps getting bigger and bigger by becoming longer and longer, and this may be considered as “obvious”, “easy”, “naive”, “messy”.

Now take a snail. Put it on the floor. Observe it. It has a spiral shape. Way more advanced than a straight line, which is the shape of the snake. And what will happen to it when it needs to grow? The spiral will grow, but it will do so in a way that the augmentation will not consume space awkwardly. Whereas the snake growth is a an embarrassment, the snail growth constitutes a clever progress, that respects resources, the resources, here, being space. Snail growth, snail augmentation happens with divine economy.

Now, Masons study these and other things and use them as symbols and guidance. They also try to sneak them wherever they can. One thing that I do not like about Masons is that they are always secretive and sneaky. But I also understand that they had to be like that in order to survive.

You see, when Christianity took over the world, a huge body of knowledge and beliefs were deemed opposite to Christianity’s teachings. Masons tried to preserve that knowledge by hiding it in their sects. Masons convened secretively and each passed the forbidden knowledge to the other Masons. They also held secrets on architectural methods that allowed them to obtain lucrative jobs in the building industry, constructing the most difficult structures like cathedrals and bridges.

Masons were able to thrive because of their secrecy. Because other people did not know the Masons’ true beliefs, they did not turn against them. It is not that the Masons beliefs were incorrect. But their beliefs were and remain politically and religiously incorrect , according to the beliefs of the majority of the people in the society.

By hiding their true identity, beliefs and ideology,  and also their tricks of the trade when it came to architecture and scientific knowledge, Masons were not only able to function as accepted members of each community, but they also became very rich.

The problem is that their secrecy and their wealth were always benefiting only them as opposed to the society as a whole. What started as a genuine community that was trying to survive the menace of early Christianity, later became a cancer that is found inside every modern society.

Nowadays, Masons are a closed, powerful community that stops at nothing in order to protect their interests. And the judicial system is their playground. Masons are everywhere and they win trials just because of their influence and connections. Masons have made justice disappear from the courtrooms.

Posted in Politics

Two kinds of people

There are two kinds of people:
People who care about others and people who care about themselves.
People who care about others are those that should become politicians.
People who care about themselves are those that do become politicians.

There are two kinds of people:
People who care about their obligations and people who care about their rights.
People who care about their obligations are those that should become entrepreneurs.
People who care about their rights are those that do become entrepreneurs.

Posted in Management

Bare-metal backup software

In a previous post, I talked about “Veeam Endpoint Backup FREE” and how it does not support bare-metal backup.

Well, here are two programs that do support bare-metal backup:

Please try then and see which one is most helpful to you.

Being able to perform a bare-metal backup is important, because a bare-metal backup does not need to alter the system before, during, or after the backup process. This means that a bare-metal backup alleviates the need of having to install the backup software to the  system. Thus, a bare-metal backup is needed in cases where the system must not or cannot be altered, or must be or might be needed to be returned to its original/initial state before the backup.

In order to perform a bare-metal backup, you will have to boot the system from a CD/DVD drive or USB drive. Your particular bare-metal backup software will guide you on how to create a bootable CD, DVD, or USB stick, which also contains the backup/restore software.

By now, it must be obvious that if a program provides backup/restore functionality, but it needs to be installed to the system that will be backed up, then this program does not provide bare-metal backup.

Posted in Administration

Feature request for Hyper-V having to do with the conversion of VHDs to containers

What we need is a utility that converts a Hyper-V VM’s virtual hard disk to a container.

Such a utility already exists for VMware VM’s virtual hard disks.

Hey, Microsoft, are you going to take the insult and let this pass?

Update (May 13, 2017): So, there does exist a way to convert Hyper-V virtual disks into Docker containers: Image2Docker. I stand corrected. Please refer to Working with Windows Containers and Docker: Into your Stride

Posted in Administration