Gcc: Some Assembly Required

There was a time when you pretty much had to be an assembly language programmer to work with embedded systems. Yes, there have always been high-level languages available, but it took improvements in tools and processors for that to make sense for anything but the simplest projects. Today, though, high-quality compilers are readily available for a lot of languages and even an inexpensive CPU is likely to outperform even desktop computers that many of us have used.

So assembly language is dead, right? Not exactly. There are several reasons people still want to use assembly. First, sometimes you need to get every clock cycle of performance out of a chip. It can be the case that a smart compiler will often produce better code than a person will write off the cuff. However, a smart person who is looking at performance can usually find a way to beat a compiler’s generated code. Besides, people can make value trades of speed versus space, for example, or pick entirely different algorithms. All a compiler can do is convert your code over as cleverly as possible.

Besides that, some people just like to program in assembly. Morse code, bows and arrows, and steam engines are all archaic, but there are still people who enjoy mastering them anyway. If you fall into that category, you might just want to write everything in assembly (and that’s fine). Most people, though, would prefer to work with something at a higher level and then slip into assembly just for that critical pieces. For example, a program might spend 5% of its time reading data, 5% of its time writing data, and 90% of the time crunching data. You probably don’t need to recreate the reading and writing parts. They won’t go to zero, after all, and so even if you could cut them in half (and you probably can’t) you get a 2.5% boost for each one. That 90% section is the big target.

The Profiler

Sometimes it is obvious what’s taking time in your programs. When it isn’t, you can actually turn on profiling. If you are running GCC under Linux, for example, you can use the -pg option to have GCC add profiling instrumentation to your code automatically. When you compile the code with -pg, it doesn’t appear to do anything different. You run your program as usual. However, the program will now silently write a file named gmon.out during execution. This file contains execution statistics you can display using gprof (see partial output below). The function b_fact takes up 65.9% of CPU time.

screenshot_232

If you don’t have a profiling option for your environment, you might have to resort to toggling I/O pins or writing to a serial port to get an idea of how long your code spends doing different functions. However you do it, though, it is important to figure it out so you don’t waste time optimizing code that doesn’t really affect overall performance (this is good advice, by the way, for any kind of optimization).

Assembly

If you start with a C or C++ program, one thing you can do is ask the compiler to output assembly language for you. With GCC, use a file name like test.s with the -o option and then use -S to force assembly language output. The output isn’t great, but it is readable. You can also use the -ahl option to get assembly code mixed with source code in comments, which is useful.

You can use this trick with most, if not all, versions of GCC. Of course, the output will be a lot different, depending. A 32-bit Linux compiler, a 64-bit Linux compiler, a Raspberry Pi compiler, and an Arduino compiler are all going to have very different output. Also, you can’t always figure out how the compiler mangles your code, so that is another problem.

If you find a function or section of code you want to rewrite, you can still use GCC and just stick the assembly language inline. Exactly how that works depends on what platform you use, but in general, GCC will send a string inside asm() or __asm__() to the system assembler. There are rules about how to interact with the rest of the C program, too. Here’s a simple example from the a GCC HOWTO document (from a PC program):

__asm__ ("movl %eax, %ebx\n\t"
"movl $56, %esi\n\t"
"movl %ecx, $label(%edx,%ebx,$4)\n\t"
"movb %ah, (%ebx)");

You can also use extended assembly that lets you use placeholders for parts of the C code. You can read more about that in the HOWTO document. If you prefer Arduino, there’s a document for that, too. If you are on ARM (like a Raspberry Pi) you might prefer to start with this document.

So?

You may never need to mix assembly language with C code. But if you do, it is good to know it is possible and maybe not even too difficult. You do need to find what parts of your program can benefit from the effort. Even if you aren’t using GCC, there is probably a way to mix assembly and your language, you just have to learn how. You also have to learn the particulars of your platform.

On the other hand, what if you want to write an entire program in assembly? That’s even more platform-specific, but we’ll look at that next time.

Control The Real World With An Arduino-Enabled Minecraft Mod

Minecraft modding has become almost as popular as the block-based game itself, with tons of editors and tools available to create new kinds of blocks, mobs, and weapons. And now, with this mod framework that can talk to an Arduino, modders can build blocks that break out of the Minecraft world to control the real world.

While turning on a light from Minecraft is not exactly new, the way that MCreator for Arduino goes about it is pretty neat. MCreator is a no-code framework for building Minecraft mods, which allows modders to build new game capabilities with a drag and drop interface. The MCreator Arduino toolkit allows modders to build custom Minecraft blocks that can respond to in-game events and communicate with an Arduino over USB. Whatever an Arduino can do – light an LED, sense a button press – can be brought into the game. It’s all open-source and free for non-commercial use, which is perfect for the upcoming STEM-based summer camp season. We can think of some great projects that would really jazz up young hackers when presented through a Minecraft interface.

Continue reading “Control The Real World With An Arduino-Enabled Minecraft Mod”

Morse Code: Paris In The Mint Box

TinyLilyThumbnail[Rob Bailey] likes to build things and he likes ham radio. We are guessing he likes mints too since he’s been known to jam things into Altoids tins. He had been thinking about building a code practice oscillator in a Altoids Smalls tin, but wasn’t sure he could squeeze an Arduino Pro Mini in there too. Then he found the TinyLily Mini. The rest is history, as they say, and 1CPO was born.

The TinyLily Mini is a circular-shaped Arduino (see right) about the size of a US dime. most of the pads are arranged around the circle and there is a small header that takes a USB programmer. A small rechargeable battery can run the device for a long time.

Continue reading “Morse Code: Paris In The Mint Box”

Paraffin Oil And Water Dot Matrix Display

In preparation for Makerfaire, [hwhardsoft] needed to throw together some demos. So they dug deep and produced this unique display.

The display uses two synchronized peristaltic pumps to push water and red paraffin through a tube that switches back over itself in a predictable fashion. As visible in the video after the break, the pumps go at it for a few minutes producing a seemingly random pattern. The pattern coalesces at the end into a short string of text. The text is unfortunately fairly hard to read, even on a contrasting background. Perhaps an application of UV dye could help?

Once the message has been displayed, the water and paraffin drop back into the holding tank as the next message is queued up. The oil and water separate just like expected and a pump at the level of each fluid feeds it back into the system.

We were deeply puzzled at what appeared to be an Arduino mounted on a DIN rail for use in industrial settings, but then discovered that this product is what [hwhardsoft] built the demo to sell. We can see some pretty cool variations on this technique for art displays.

Continue reading “Paraffin Oil And Water Dot Matrix Display”

Hackaday Prize Entry: Ears On The Back Of Your Head

From context clues, we can tell that [TVMiller] has been in and around NYC for some time now. He has observed a crucial weakness in the common metropolitan. Namely, they deafen themselves with earphones, leaving them senseless in a hostile environment.

To fix this problem, he came up with a simple hack, the metrophone. An ultrasonic sensor is hung from a backpack. The user’s noise making device of choice is plugged into one end, and the transducer into the other. When the metropolitan is approached from the rear by a stalking tiger or taxi cab, the metrophone will reduce the volume and allow the user to hear and respond to their impending doom. Augmentation successful.

The device itself consists of an off-the-shelf ultrasonic sensor, an Arduino, and a digital potentiometer. It all fits in a custom 3D printed enclosure and runs of two rechargeable coin cells. A simple bit of code scales the volume to the current distance being measured by the ultrasonic sensor once a threshold has been met.

In the video after the break, you can observe [TVMiller]’s recommended method for tranquilizing and equipping a metropolitan in its natural habitat without disturbing its patterns or stressing it unduly.

Continue reading “Hackaday Prize Entry: Ears On The Back Of Your Head”

Motorized Music Box Cranks Out Stairway To Heaven

[Bokononestly] found a lil’ music box that plays Stairway to Heaven and decided those were just the kinds of dulcet tones he’d like to wake up to every morning. To each his own; I once woke up to Blind Melon’s “No Rain” every day for about six months. [Bokononestly] is still in the middle of this alarm clock project right now. One day soon, it will use a *duino to keep track of the music box’s revolutions and limit the alarm sound to one cycle of the melody.

stairway-musicbox-alarm-clock[Bokononestly] decided to drive the crank of the music box with a geared DC motor from an electric screwdriver. After making some nice engineering drawings of the dimensions of both and mocking them up in CAD, he designed and printed a base plate to mount them on. A pair of custom pulleys mounted to the motor shaft and the crank arm transfer motion using the exact right rubber band for the job. You can’t discount the need for a big bag ‘o rubber bands.
In order to count the revolutions, he put a wire in the path of the metal music box crank and used the body of the box as a switch. Check out the build video after the break and watch him prove it with the continuity function of a multimeter. A clever function that should at some point be substituted out for a leaf switch.

We’ve covered a lot of cool clock builds over the years, including one or two that run Linux. And say what you will about Stairway; it’s better than waking up to repeated slaps in the face.

Continue reading “Motorized Music Box Cranks Out Stairway To Heaven”

DIY Air Quality Meter And Emissions Tester

Handheld measuring devices make great DIY projects. One can learn a lot about a sensor or sensor technology by just strapping it onto a spare development board together with an LCD for displaying the sensor output. [Richard’s] DIY air quality meter and emissions tester is such a project, except with the custom laser-cut enclosure and the large graphic LCD, his meter appears already quite professional.

Continue reading “DIY Air Quality Meter And Emissions Tester”