Files
eth-summaries/semester3/spca/parts/00_asm/04_control-flow.tex
2026-01-11 16:52:34 +01:00

34 lines
1.5 KiB
TeX

\newpage
\subsection{Control Flow}
Control flow structures from \verb|C| like \verb|if/else| or \verb|for| are compiled into assembly mainly using jumps and conditional \verb|move|.
By the nature of Assembly and thanks to compilers optimizing aggressively, there is no \textit{single} definitive translation of the \verb|C| control structures: The compiler may translate it very differently depending on the context of the program.
\subsubsection{Conditional statements}
A function using an \verb|if/else| construct to choose the maximum of $2$ numbers might compile like this:
\inputcodewithfilename{gas}{code-examples/01_asm/}{02_max.s}
A function computing the absolute difference $|x-y|$ using an \verb|if/else| construct, might use a conditional \verb|move| instead:
\inputcodewithfilename{gas}{code-examples/01_asm/}{03_absdiff.s}
\subsubsection{While Loops}
A recursive factorial function using a \verb|do while| loop may be compiled like this:
\inputcodewithfilename{gas}{code-examples/01_asm/}{04_factorial.s}
\newpage
The same function, using a \verb|while| loop instead may lead to this:
\inputcodewithfilename{gas}{code-examples/01_asm/}{05_factorial.s}
\subsubsection{For Loops \& Switch}
\verb|for| loops follow the same idea as \verb|while| loops, albeit with a few more jumps.
\verb|switch| statements are implemented differently depending on size and case values: Sparse switch statements are compiled as decision trees, whereas large switch statements may become \textit{jump tables}.