Solar and lunar distances
I'm going to write a few posts about programming in machine language. It seems that many more people are interested in learning about the ARM processor, so that's what I'll be writing about. In particular, I'm going to be working with the Raspberry Pi running Raspbian linux. For those who aren't familiar with it, the Pi is a super-inexpensive computer that's very easy to program, and very easy to interface with the outside world. It's a delightful little machine, and you can get one for around $50!
Anyway, before getting started, I wanted to talk about a few things. First of all, why learn machine language? And then, just what the heck is the ARM thing anyway?
My answer might surprise you. Or, if you've been reading this blog for a while, it might not.
Let's start with the wrong reason. Most of the time, people say that you should learn machine language for speed: programming at the machine code level gets you right down to the hardware, eliminating any layers of junk that would slow you down. For example, one of the books that I bought to learn ARM assembly (Raspberry Pi Assembly Language RASPBIAN Beginners: Hands On Guide) said:
even the most efficient languages can be over 30 times
slower than their machine code equivalent, and that’s on a good
This is pure, utter rubbish. I have no idea where he came up with that 30x figure, but it's got no relationship to reality. (It's a decent book, if a bit elementary in approach; this silly statement isn't representative of the book as a whole!)
In modern CPUs - and the ARM definitely does count as modern! - the fact is, for real world programs, writing code by hand in machine language will probably result in slower code!
If you're talking about writing a single small routine, humans can be very good at that, and they often do beat compilers. Butonce you get beyond that, and start looking at whole programs, any human advantage in machine language goes out the window. The constraints that actually affect performance have become incredibly complex - too complex for us to juggle effectively. We'll look at some of these in more detail, but I'll explain one example.
The CPU needs to fetch instructions from memory. But memory is dead slow compared to the CPU! In the best case, your CPU can execute a couple of instructions in the time it takes to fetch a single value from memory. This leads to an obvious problem: it can execute (or at least start executing) one instruction for each clock tick, but it takes several ticks to fetch an instruction!
To get around this, CPUs play a couple of tricks. Basically, they don't fetch single instructions, but instead grab entire blocks of instructions; and they start retrieving instructions before they're needed, so that by the time the CPU is ready to execute an instruction, it's already been fetched.
So the instruction-fetching hardware is constantly looking ahead, and fetching instructions so that they'll be ready when the CPU needs them. What happens when your code contains a conditional branch instruction?
The fetch hardware doesn't know whether the branch will be taken or not. It can make an educated guess by a process called branch prediction. But if it guesses wrong, then the CPU is stalled until the correct instructions can be fetched! So you want to make sure that your code is written so that the CPUs branch prediction hardware is more likely to guess correctly. Many of the tricks that humans use to hand-optimize code actually have the effect of confusing branch prediction! They shave off a couple of instructions, but by doing so, they also force the CPU to sit idle while it waits for instructions to be fetched. That branch prediction failure penalty frequently outweighs the cycles that they saved!
That's one simple example. There are many more, and they're much more complicated. And to write efficient code, you need to keep all of those in mind, and fully understand every tradeoff. That's incredibly hard, and no matter how smart you are, you'll probably blow it for large programs.
If not for efficiency, then why learn machine code? Because it's how your computer really works! You might never actually use it, but it's interesting and valuable to know what's happening under the covers. Think of it like your car: most of us will never actually modify the engine, but it's still good to understand how the engine and transmission work.
Your computer is an amazingly complex machine. It's literally got billions of tiny little parts, all working together in an intricate dance to do what you tell it to. Learning machine code gives you an idea of just how it does that. When you're programming in another language, understanding machine code lets you understand what your program is really doing under the covers. That's a useful and fascinating thing to know!
As I said, we're going to look at machine language coding on the
ARM processor. What is this ARM beast anyway?
It's probably not the CPU in your laptop. Most desktop and laptop computers today are based on a direct descendant of the first microprocessor: the Intel 4004.
Yes, seriously: the Intel CPUs that drive most PCs are, really, direct descendants of the first CPU designed for desktop calculators! That's not an insult to the intel CPUs, but rather a testament to the value of a good design: they've just kept on growing and enhancing. It's hard to see the resemblance unless you follow the design path, where each step follows directly on its predecessors.
The Intel 4004, released in 1971, was a 4-bit processor designed for use in calculators. Nifty chip, state of the art in 1971, but not exactly what we'd call flexible by modern standards. Even by the standards of the day, they recognized its limits. So following on its success, they created an 8-bit version, which they called the 8008. And then they extended the instruction set, and called the result the 8080. The 8080, in turn, yielded successors in the 8088 and 8086 (and the Z80, from a rival chipmaker).
The 8086 was the processor chosen by IBM for its newfangled personal computers. Chip designers kept making it better, producing the 80286, 386, Pentium, and so on - up to todays CPUs, like the Core i7 that drives my MacBook.
The ARM comes from a different design path. At the time that Intel was producing the 8008 and 8080, other companies were getting into the same game. From the PC perspective, the most important was the 6502, which
was used by the original Apple, Commodore, and BBC microcomputers. The
6502 was, incidentally, the first CPU that I learned to program!
The ARM isn't a descendant of the 6502, but it is a product of the 6502 based family of computers. In the early 1980s, the BBC decided to create an educational computer to promote computer literacy. They hired a company called Acorn to develop a computer for their program. Acorn developed a
beautiful little system that they called the BBC Micro.
The BBC micro was a huge success. Acorn wanted to capitalize on its success, and try to move it from the educational market to the business market. But the 6502 was underpowered for what they wanted to do. So they decided to add a companion processor: they'd have a computer which could still run all of the BBC Micro programs, but which could do fancy graphics and fast computation with this other processor.
In a typical tech-industry NIH (Not Invented Here) moment, they decided that none of the other commercially available CPUs were good enough, so they set out to design their own. They were impressed by the work done by the Berkeley RISC (Reduced Instruction Set Computer) project, and so they adopted the RISC principles, and designed their own CPU, which they called the Acorn RISC Microprocessor, or ARM.
The ARM design was absolutely gorgeous. It was simple but flexible
and powerful, able to operate on very low power and generating very little heat. It had lots of registers and an extremely simple instruction set, which made it a pleasure to program. Acorn built a lovely computer with a great operating system called RiscOS around the ARM, but it never really caught on. (If you'd like to try RiscOS, you can run it on your Raspberry Pi!)
But the ARM didn't disappear. Tt didn't catch on in the desktop computing world, but it rapidly took over the world of embedded devices. Everything from your cellphone to your dishwasher to your iPad are all running on ARM CPUs.
Just like the Intel family, the ARM has continued to evolve: the ARM family has gone through 8 major design changes, and dozens of smaller variations. They're no longer just produced by Acorn - the ARM design is maintained by a consortium, and ARM chips are now produced by dozens of different manufacturers - Motorola, Apple, Samsung, and many others.
Recently, they've even starting to expand even beyond embedded platforms: the Chromebook laptops are ARM based, and several companies are starting to market server boxes for datacenters that are ARM based! I'm looking forward to the day when I can buy a nice high-powered ARM laptop.
Barton Gellman and Ashkan Soltani are doing some fantastic reporting on the Snowden NSA documents. I hope to be able to do the same again, once Pierre Omidyar's media venture gets up and running.
This article points out that as people are logging into Wi-Fi networks from their Android phones, and backing up those passwords along with everything else into Google's cloud, that Google is amassing an enormous database of the world's Wi-Fi passwords. And while it's not every Wi-Fi password in the world, it's almost certainly a large percentage of them.
Leaving aside Google's intentions regarding this database, it is certainly something that the US government could force Google to turn over with a National Security Letter.
Something else to think about.
With lots of kids heading to school this week, an old question comes back to the fore: Can thinking be separated from knowing?
Many people, and not a few educators, believe that the answer is yes. Schools, they suggest, should focus on developing students’ “critical thinking skills” rather than on helping them beef up their memories with facts and other knowledge about the world. With the Internet, they point out, facts are always within easy reach. Why bother to make the effort to cram stuff into your own long-term memory when there’s such a capacious store of external, or “transactive,” memory to draw on? A kid can google the facts she needs, plug them into those well-honed “critical thinking skills,” and – voila! – brilliance ensues.
That sounds good, but it’s wrong. The idea that thinking and knowing can be separated is a fallacy, as the University of Virginia psychologist Daniel Willingham explains in his book Why Don’t Students Like School. This excerpt from Willingham’s book seems timely:
I defined thinking as combining information in new ways. The information can come from long-term memory — facts you’ve memorized — or from the environment. In today’s world, is there a reason to memorize anything? You can find any factual information you need in seconds via the Internet. Then too, things change so quickly that half of the information you commit to memory will be out of date in five years — or so the argument goes. Perhaps instead of learning facts, it’s better to practice critical thinking, to have students work at evaluating all that information available on the Internet, rather than trying to commit some small part of it to memory.
This argument is false. Data from the last thirty years lead to a conclusion that is not scientifically challengeable: thinking well requires knowing facts, and that’s true not simply because you need something to think about. The very processes that teachers care about most — critical thinking processes such as reasoning and problem solving — are intimately intertwined with factual knowledge that is in long-term memory (not just found in the environment).
It’s hard for many people to conceive of thinking processes as intertwined with knowledge. Most people believe that thinking processes are akin to those of a calculator. A calculator has available a set of procedures (addition, multiplication, and so on) that can manipulate numbers, and those procedures can be applied to any set of numbers. The data (the numbers) and the operations that manipulate the data are separate. Thus, if you learn a new thinking operation (for example, how to critically analyze historical documents), it seems like that operation should be applicable to all historical documents, just as a fancier calculator that computes sines can do so for all numbers.
But the human mind does not work that way. When we learn to think critically about, say, the start of the Second World War, it does not mean that we can think critically about a chess game or about the current situation in the Middle East or even about the start of the American Revolutionary War. Critical thinking processes are tied to the background knowledge. The conclusion from this work in cognitive science is straightforward: we must ensure that students acquire background knowledge with practicing critical thinking skills.
Willingham goes on the explain that once a student has mastered a subject — once she’s become an expert — her mind will become fine-tuned to her field of expertise and she’ll be able to fluently combine transactive memory with biological memory. But that takes years of study and practice. During the K – 12 years, developing a solid store of knowledge is essential to learning how to think. There’s still no substitute for a well-furnished mind.
The FDA has announced that it will be studying the effects of nutrient-content claims on consumers attitudes about food products.
FDA does not encourage the addition of nutrients to certain food products (including sugars or snack foods such as [cookies] candies, and carbonated beverages). FDA is interested in studying whether fortification of these foods could cause consumers to believe that substituting fortified snack foods for more nutritious foods would ensure a nutritionally sound diet.
Here’s one of my favorite examples of what the FDA is talking about.
I’m guessing the FDA’s new research project is a response to increasing pressure from food companies to be allowed to add nutrients to cookies, candies, and soft drinks.
Food marketers know perfectly well that nutrients sell food products. The whole point of doing so is to be able to make nutrient-content claims on package labels.
The FDA has never been happy about the practice of adding nutrients to junk foods just to make them seem healthy. Its guidance includes what is commonly known as the “jelly bean rule.” You may not add nutrients to jelly beans to make them eligible to be used in school lunches.
But this does not stop food manufacturers—especially soft drink manufacturers—from trying. Hence: Vitamin Water (now owned by Coca-Cola).
Plenty of research demonstrates that nutrients sell food products. Any health or health-like claim on a food product—vitamins added, no trans fats, organic—makes people believe that the product has fewer calories and is a health food.
As I keep saying, added vitamins are about marketing, not health.
“I see a lot of new faces. But, you know the old saying, ‘out with the old, in with the nucleus.’” -The Simpsons
Looking around the Universe today, there’s no doubt that there’s plenty of hydrogen and helium around; after all, it’s the nuclear fusion of hydrogen into helium that powers the vast majority of stars illuminating the entire cosmos!
But here on Earth, hydrogen and helium are only a small part of the world we inhabit. By mass, hydrogen and helium combined make up far less than 1% of the Earth, and even if we restrict ourselves to the Earth’s crust, it’s still just a tiny percentage compared to the other, heavier elements.
Practically all of these heavy elements were formed in generations of stars: stars that lived, burned their fuel into heavier elements, died and shed their heavy, enriched elements back into the cosmos, and were incorporated into the next generations of stars and — when the heavier elements became abundant enough — rocky planets.
But the Universe didn’t start off with these heavier elements at all. In fact, if you’ll remember what the Big Bang says, the Universe is expanding (and cooling) now, meaning that all the matter in it was closer together — and the radiation in it was hotter — in the past. If you go back to a sufficiently early time, you’ll find that the density was high enough and the temperature was hot enough that you couldn’t even form neutral atoms without them immediately being blasted apart! When the Universe cooled through that phase, that’s when neutral atoms formed for the first time, and where the cosmic microwave background comes from.
At that time, the Universe was made out of about 92% hydrogen atoms and 8% helium atoms by number (or about 75-76% hydrogen and 24-25% helium by mass), with trace amounts of lithium and beryllium, but not much else. But you might wonder how it got to have exactly that ratio? After all, it didn’t have to be that way; if the Universe was hot and dense enough to undergo nuclear fusion early on, why did it only fuse atoms up to helium, and why didn’t more of the Universe become helium than it did?
To find the answer, we need to go way back in time. Not just to the first few hundred thousand years of the Universe, when it was making the first atoms, nor even to the first years, days, or hours. No, we need to go back to when the temperatures were so high, when the Universe was so hot, that not only could atomic nuclei not form (for they’d be immediately be blasted apart), but to a time when the Universe was so hot that the Universe was filled with nearly equal amount of matter-and-antimatter, when it was just a fraction of a second old!
It was once so hot that the Universe was filled with nearly equal amount of matter and antimatter: protons and antiprotons, neutrons and antineutrons, electrons and positrons, neutrinos and antineutrinos, and of course photons (which are their own antiparticle), among others. (They’re not exactly equal; see here for more on that.) When the Universe is hot — and by hot, I mean above the temperature needed to spontaneously create a matter/antimatter pair from two typical photons — you get huge amounts of that form of matter and antimatter. They get spontaneously created from photons just as quickly as they find one another and annihilate back into photons. But as the Universe cools, those matter/antimatter pairs begin to annihilate faster, and it becomes more difficult to find photons energetic enough to make them. Eventually, it cools enough that all the exotic particles go away, and all the antiprotons and antineutrons annihilate with protons and neutrons, leaving only a small asymmetry of matter (in the form of protons and neutrons) over antimatter, bathed in a sea of radiation.
At this point, when the Universe is a fraction of a second old, there are roughly equal amounts of protons and neutrons: about a 50/50 split. These protons and neutrons will eventually become the atoms in our Universe, but they’ve got a lot to go through first. On the other hand, electrons (and positrons) are much lighter, so they still exist in huge numbers (and at great energies) for a while longer.
It’s still hot enough that protons and neutrons can convert into one another very easily: a proton can combine with an electron to make a neutron and (an electron) neutrino, while a neutron can combine with (an electron) neutrino to make a proton and an electron. While there aren’t that many protons and neutrons in the Universe at this time, electrons and neutrinos outnumber them by around a billion-to-one. This is why, early on, there’s about a 50/50 split of protons and neutrons.
Neutrons, as you’ll remember, are slightly heavier than protons: by about 0.2%. As the Universe cools (and the excess positrons annihilate away), it becomes rarer and rarer to find a proton-electron pair with enough energy to create a neutron, while it’s still relatively easy for a neutron-neutrino pair to create a proton-electron pair. This converts a substantial fraction of neutrons into protons during the first one-to-three seconds of the Universe. By time these interactions have become insignificant, the proton-to-neutron ratio has changed from about 50/50 to 85/15!
Now, these protons and neutrons are abundant, hot, and dense enough that they can fuse together into heavier elements, and believe me, they’d love to. But photons — particles of radiation – outnumber protons-and-neutrons by more than a billion to one, so for minutes of the Universe expanding and cooling, it’s still energetic enough that every time a proton and neutron fuse together to form deuterium, the first stepping-stone in nuclear fusion, a high-enough energy photon immediately comes along and blasts them apart! This is known as the deuterium bottleneck, as deuterium is relatively fragile, and its fragility prevents further nuclear reactions from occurring.
In the meantime, while the minutes tick by, something else is going on. A free proton is stable, so nothing happens to them, but a free neutron is unstable; it will decay into a proton, electron, and an (electron) antineutrino with a half-life of about ten minutes. By time the Universe has cooled enough that the created deuterium wouldn’t be immediately be blasted back apart, more than three minutes have gone by, further changing the 85%-proton/15%-neutron split to nearly 88% protons and just a hair over 12% neutrons.
Finally, with deuterium forming, nuclear fusion can proceed, and it proceeds extremely rapidly! Through a couple of different fusion chains, the Universe is still hot and dense enough that pretty much every neutron around wind up combining with one other neutron and two protons to form helium-4, an isotope of helium that’s much more energetically stable than deuterium, tritium, or helium-3!
By time this happens, though, the Universe is nearly four minutes old, and is far too diffuse and cold to undergo the next major step of fusion that happens in stars, which is to fuse three helium-4 atoms into carbon-12; that process will have to wait tens of millions of years until the Universe’s first stars form!
But these nuclei are stable, and there will also be a trace amount of helium-3 (which tritium will also decay into, eventually), deuterium (hydrogen-2), and very small amounts of lithium (and probably even smaller amounts of beryllium) formed by very rare fusion reactions.
But the overwhelming majority of neutrons — 99.9%+ of them — wind up locked up in helium-4 nuclei. If the matter in the Universe contained just a hair over 12% neutrons and just a hair under 88% protons just prior to nucleosynthesis (the fusion into heavier elements), that means that all of those neutrons and and equal amount (just over 12% of the Universe) of protons winds up becoming helium-4: a total of 24-to-25% of the mass, leaving 75-to-76% of the Universe as protons, or hydrogen nuclei.
So that’s why, by mass, we say 75-76% was hydrogen and 24-25% was helium. But each helium nucleus is around four times the mass of a hydrogen nucleus, which means that, by number of atoms, the Universe is around 92% hydrogen and 8% helium.
This primordial, unprocessed material has actually been detected observationally, and is one of the three cornerstones of the Big Bang, along with Hubble expansion and the cosmic microwave background. And that’s where all the elements in the Universe started from! Everything you are, everything you know, and every material object you’ve ever interacted with came from this primordial sea of protons and neutrons, and was once a mere collections of hydrogen and helium atoms. And then the Universe happened…
and here it all is! And that’s where — if you go way, way back — all the atoms came from.