diff --git a/semester3/spca/code-examples/01_asm/04_factorial.s b/semester3/spca/code-examples/01_asm/04_factorial.s new file mode 100644 index 0000000..daf16f8 --- /dev/null +++ b/semester3/spca/code-examples/01_asm/04_factorial.s @@ -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 \ No newline at end of file diff --git a/semester3/spca/code-examples/01_asm/05_factorial.s b/semester3/spca/code-examples/01_asm/05_factorial.s new file mode 100644 index 0000000..c7a2225 --- /dev/null +++ b/semester3/spca/code-examples/01_asm/05_factorial.s @@ -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 \ No newline at end of file diff --git a/semester3/spca/parts/00_asm/03_operations.tex b/semester3/spca/parts/00_asm/03_operations.tex index 9f676ad..ebdb020 100644 --- a/semester3/spca/parts/00_asm/03_operations.tex +++ b/semester3/spca/parts/00_asm/03_operations.tex @@ -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. \ No newline at end of file diff --git a/semester3/spca/parts/00_asm/04_control-flow.tex b/semester3/spca/parts/00_asm/04_control-flow.tex index e3c60ad..971ed5e 100644 --- a/semester3/spca/parts/00_asm/04_control-flow.tex +++ b/semester3/spca/parts/00_asm/04_control-flow.tex @@ -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}. diff --git a/semester3/spca/parts/00_asm/05_stack.tex b/semester3/spca/parts/00_asm/05_stack.tex new file mode 100644 index 0000000..de9beb8 --- /dev/null +++ b/semester3/spca/parts/00_asm/05_stack.tex @@ -0,0 +1 @@ +\subsection{The Stack} \ No newline at end of file diff --git a/semester3/spca/spca-summary.pdf b/semester3/spca/spca-summary.pdf index cc9b911..5710a99 100644 Binary files a/semester3/spca/spca-summary.pdf and b/semester3/spca/spca-summary.pdf differ