So I’ve been programming some mathematical functions in assembly for the Freescale HCS08 core for the last couple of weeks. It’s been a really nice experience because I had not programmed in assembly for a while. Before you start judging me on not using C, consider that mathematical functions are one of the few places where assembly does makes sense, particularly when performance is a priority (like in motor control applications).
Apart from the horribly fun process of remembering the instruction set, the great thing about this project is that I’ve been able to go back to the roots of the CPU. I got to remember the cost of declaring a variable in a function and of passing too many arguments instead of pointers or structures. I also remembered how nice it is to have a CPU with several registers instead of only having three. But by far, the most valueable thing I got to remember was just how much an 8-bit CPU has to do to handle 16 bit variables. It’s almost ridiculous. A simple shift operation will take some 18 CPU cycles if done in variables stored in the stack. Depending on the input values, a 16 bit division could take from 100 to 900 CPU cycles depending on the algorithm being used.
All in all, a valuable (and gray-hair-inducing) experience. But I would definitely would recommend it to anyone who has gotten too comfortable with the high level languages.
Real masochists program in 8-bit assembly
September 17, 2009So I’ve been programming some mathematical functions in assembly for the Freescale HCS08 core for the last couple of weeks. It’s been a really nice experience because I had not programmed in assembly for a while. Before you start judging me on not using C, consider that mathematical functions are one of the few places where assembly does makes sense, particularly when performance is a priority (like in motor control applications).
Apart from the horribly fun process of remembering the instruction set, the great thing about this project is that I’ve been able to go back to the roots of the CPU. I got to remember the cost of declaring a variable in a function and of passing too many arguments instead of pointers or structures. I also remembered how nice it is to have a CPU with several registers instead of only having three. But by far, the most valueable thing I got to remember was just how much an 8-bit CPU has to do to handle 16 bit variables. It’s almost ridiculous. A simple shift operation will take some 18 CPU cycles if done in variables stored in the stack. Depending on the input values, a 16 bit division could take from 100 to 900 CPU cycles depending on the algorithm being used.
All in all, a valuable (and gray-hair-inducing) experience. But I would definitely would recommend it to anyone who has gotten too comfortable with the high level languages.
Side note: I also love compilers a lot more now.
Posted in Commentary, Embedded software | Tagged assembly, code, Creativity, embedded, embedded systems, software development tools |