A friendly introduction to assembly for high-level programmers — Functions & Loops

submited by
Style Pass
2024-09-30 21:30:05

In the previous article we learned about Control Transfer Instructions, and we have seen how they are the cornerstone of control flow in assembly. We looked at their simplest, jump, and learned how to implement conditionals.

Today we will look at reproducing functions. Starting with no arguments, void functions, all the way up to functions returning multiple values.

From what we have seen in the previous article, jumps seem like a good approximation of this concept: they provide a mechanism to execute code from elsewhere, and they have a human-readable name. What could one desire more?

In high-level languages, functions do their job and hand back control to the caller once done, typically with the return keyword. Jumps, conversely, are one-way control transfer: once you jump to a location there is no way back unless the callee knows where to jump to.

The premise that we need to control callee and caller hinders reusability, one property we expect from functions. Imagine authoring a library; functions would have to know details about calling code’s structure to jump back, making it impossible to write modular or reusable code.

Leave a Comment