[SPCA] more assembly

This commit is contained in:
RobinB27
2026-01-11 16:52:34 +01:00
parent f0c472bb87
commit 42b7451241
6 changed files with 50 additions and 1 deletions

View File

@@ -0,0 +1,8 @@
factorial:
movl $1, %eax # Setup
.AGAIN:
imull %edi, %eax # %eax *= %edi
subl $1, %edi # %edi--
cmpl $1, %edi # 1 < %edi ?
jg .AGAIN # go back, if yes
ret

View File

@@ -0,0 +1,9 @@
factorial:
jmp .COMP # Check condition
.LOOP:
imull %edx, %eax # %eax *= %edx
decl %edx # %edx--
.COMP:
cmpl $1, %edx # 1 < %edx ?
jg .LOOP # if yes, go to loop
ret

View File

@@ -25,7 +25,7 @@ Arithmetic / logic operations need a size postfix (replace \texttt{X} below with
\end{tables}
\newpage
\subsubsection{Condition Codes and jumping}
\subsubsection{Condition Codes}
Any arithmetic operation (that is truly part of the arithmetic operations group, so not including \texttt{lea} for example) implicitly sets the \bi{condition codes}.
The following condition codes were covered in the lecture (operation: \texttt{t = a + b}):
\begin{itemize}
@@ -68,3 +68,16 @@ and \texttt{SF} is set if \texttt{a \& b < 0}.
\caption{Condition code postfixes for jump and set instructions}
\label{tab:condition-codes}
\end{table}
\content{Conditional Moves}
Similar to \texttt{jX}, the same postfixes can be applied to \texttt{cmovX}, for example:
\begin{minted}{gas}
cmpl %eax, %edx
cmovle %edx, %eax
\end{minted}
Will move \verb|%edx| into \verb|%eax|, only if \verb|%edx| is less or equal (\verb|le|) \verb|%eax|.
This can be used to, for example, compile ternary expressions from \verb|C| to Assembly. However, this requires evaluating both possible expressions, which may lead to a (significant) performance overhead.

View File

@@ -13,3 +13,21 @@ A function using an \verb|if/else| construct to choose the maximum of $2$ number
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}.

View File

@@ -0,0 +1 @@
\subsection{The Stack}

Binary file not shown.