ARMAssembly:BinaryIntro A

Copyright 2018, Clinton A Staley

Concepts

  1. Bits and Bytes
  2. Binary Integers
  3. Trinary numbers

Binary fundamentals

Before looking at assembly code examples, let's talk a bit about how information is stored in the computer's memory. What follows, especially the first parts, may be review for you, but at least skim the transcript and be sure you understand all the ideas.

Bits and Bytes

Memory is described using two important terms, which you may already have encountered in other study: bit and byte. A bit is simply a 1 or a 0 -- a switch turned on or off. This may take different forms depending on the type of memory. In RAM it may be an electrical charge held on a transistor. On a disk, it may be a north- or south- oriented magnetic spot on the surface of the disk. However it's stored, an individual bit may be either on or off. A single on/off or 1/0 value is the fundamental unit of computer memory.

Now of course just a 1 or a 0 in memory is not all that useful. After all, how much can you say with a single on/off or 1/0? So, these bits or switches are grouped into larger units. After a few experiments and adjustments in the early days of computer design, it has long since been settled that groups of 8 bits are the best choice, and such a group is termed a byte. You can imagine a byte as a bank of 8 switches if you like or, as I show it in the diagram, as a series of eight 1s or 0s. A byte may store any pattern of eight 1s and 0s that you like.

Any computer memory, whether it is disk or RAM or otherwise, may be thought of as a long series of bytes. We really don't deal with individual bits when accessing computer memory; a byte is the smallest unit of practical use.

And please be clear on the difference between these terms! I often find students mixing "byte" and "bit", perhaps because of the similar sound. A bit is just a single 1 or 0; a byte is 8 bits.

Storing integer numbers

Grouping bits still doesn't answer the bigger question of how to use 1s and 0s, in groups or otherwise, to store the kind of information that is actually useful: numbers, text, or even more elaborate and less clearly numerical things like colors or pictures. How do we use this basic pattern of 1s and 0s to store such information?

The first example is highlighted here in the diagram: counting integers in binary. Obviously, we often need to store numbers in a computer program, and we'll need to use 1s and 0s to do so, since that's all the memory has. Numerical values stored in a computer are represented in binary or base-2 form.

To understand base-2, recall the concept of place value we all learned somewhere in grade school. A decimal number has a 1's place, a 10's place a 100's place, etc. As we move to the left, each successive digit has a value 10 times greater than the one to its right.

Binary or base-2 arithmetic works the same way, except that the place values are different. We start with the 1s place as shown in the diagram, but to the left of it is the 2's place and then the 4's place and so on. Each place value as we move left is multiplied not by a factor of 10 but by a factor of 2. The example of 1101 in the diagram thus represents the value 13.

Now you can get much deeper into computer arithmetic in a computer architecture class but let's try using this system to count a little bit in binary, starting with just one byte. We'll assign binary place values to the bits of the byte as shown in the diagram, starting with the 1's place on the rightmost bit, and moving up to the 128's place (after doubling 7 times) for the leftmost bit. This makes a byte into an 8-digit binary value. We can view a byte of all 0s, for instance as representing the value 0:

00000000

Turn the rightmost bit to 1:

00000001

and we have the value 1.

Setting the last two bits to 10 is a little more interesting:

00000010

That's not the value ten, but rather the value two. There is a 1 in the 2's place and 0 in the 1's place and all other places, so the total value is two. Setting the last two bits to 11 gives us the value three (one 2 and one 1):

00000011

If we increase this by one more we arrive at:

00000100

with a 1 in the 4's place and all other places 0. This is not the value one hundred, but the value four. You can think of the 11, or 3 in binary, as analogous to 99 in base 10, in that the bottom two places are totally filled, and adding one results in them flipping back to zero and adding 1 to the next place value (the 4s place). Place values get filled really fast in binary: when you reach 1, that's like reaching 9 in base 10. So, continuing on a few steps, we have

00000101 is 5

00000110 is 6

and so on. We can count upward arbitrarily using this system. The only digits we need are 0 and 1. You don't ever need the digit 2 in this system, because if you want to represent 2 in a given place, you instead add 1 to the place value above it, just as in the base-10 system we don't need a single digit for the value ten, because if you want a value ten you just add 1 in the next place up. So counting in binary allows us to use bits containing just 0 or 1 as digits, and we can count as high as we like if we have enough bits to do it. In fact the term bit is an acronym. It stands for binary digit.

Question 1 Please extend the ideas just discussed to count upward in binary to the value 20. Show an 8-bit value for each of the numbers 7 through 20 (we've already done up to 6, of course).

Answer 1

The values are:

00000111 7
00001000 8
00001001 9
00001010 10
00001011 11
00001100 12
00001101 13
00001110 14
00001111 15
00010000 16
00010001 17
00010010 18
00010011 19
00010100 20

LSB and MSB

Since the rightmost bit has the least mathematical place value, it's generally termed the least significant bit or just LSB. Likewise, the leftmost bit is the most significant bit or MSB. We'll use these terms from now on.

Bigger values, more bits

If we go all the way up to all 1s in a byte, that's the highest value we could get using just 8 bits. It turns out to be 255. You can add it up if you like. The place values are 128, 64, 32, 16, 8, 4, 2, and 1, and they total to 255.

This is probably a good place to bring up a block of vocabulary that I've added to the vocabulary list for this lecture. You can see them on the top of the diagram. Those are the powers of 2 from 2^1 to 2^16 (from 2 to 65536). Please treat these as vocabulary terms, meaning memorize them. You'll be tested on this. This is not just tedious mathematical memorization; these powers of two are useful to know. I'm a geek, but I'm not the kind of geek who memorizes numbers just for fun. Still, I have the first 16 powers of 2 memorized, because they come up all the time in my work with computer software and hardware.

So, what if we want to count higher than 255? We use more bytes. As I said earlier, we always get information from memory at least one byte at at time, but in fact we often draw information from memory several bytes at a time. It's common, for instance, to use multiple bytes for larger numerical values, so we might store a larger number using 2 bytes, 4 bytes, or these days as memory becomes even cheaper, 8 bytes.

Let's see what happens if we count larger integers using 2 bytes, or 16 bits. We count in binary, following the same principles but with a lot more bits to use. With 16 bits, the MSB (leftmost bit) will have the value of 2^15 or 32768, and if you add up all the bits the total value of 16 1's is:

1111111111111111 = 65535

That's better, and may be large enough for some purposes, but we'll often need more. Let's see what happens with 4 bytes, or 32 bits. Now the MSB has place value 2^31, or a little over 2 billion. Counting this, and all the lesser bits, we find that 32 1's comes to over 4 billion. That's enough for most purposes, unless you're storing the national debt or something. And if you go to an 8 byte number you get a maximum value considerably larger -- around 10^19 (10 quintillion), which, one would hope, should be sufficient even to store the national debt. Bytes are pretty cheap, so using them in larger groups is common. As we get deeper into programming we'll see that patterns of data may occupy quite a number of bytes.

Max values by size of integer

Summarizing, we can count up to all-1s to get the largest possible value for a given number of bits. Those values, for the most common integer sizes, are:

Historical Note on Ternary Logic

Virtually all computers in existence use binary logic because it's the simplest to implement in hardware. But there's no law of nature requiring binary logic and arithmetic in computers. For instance, there have been experiments in the past with memories that have fundamental units with three different states – a sort of off, on, and in-between switch. This is called ternary logic, but it never turned out to be worth the added complexity.