mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-01-12 14:18:23 +00:00
[SPCA] more assembly
This commit is contained in:
8
semester3/spca/code-examples/01_asm/04_factorial.s
Normal file
8
semester3/spca/code-examples/01_asm/04_factorial.s
Normal 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
|
||||||
9
semester3/spca/code-examples/01_asm/05_factorial.s
Normal file
9
semester3/spca/code-examples/01_asm/05_factorial.s
Normal 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
|
||||||
@@ -25,7 +25,7 @@ Arithmetic / logic operations need a size postfix (replace \texttt{X} below with
|
|||||||
\end{tables}
|
\end{tables}
|
||||||
|
|
||||||
\newpage
|
\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}.
|
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}):
|
The following condition codes were covered in the lecture (operation: \texttt{t = a + b}):
|
||||||
\begin{itemize}
|
\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}
|
\caption{Condition code postfixes for jump and set instructions}
|
||||||
\label{tab:condition-codes}
|
\label{tab:condition-codes}
|
||||||
\end{table}
|
\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.
|
||||||
@@ -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:
|
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}
|
\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}.
|
||||||
|
|||||||
1
semester3/spca/parts/00_asm/05_stack.tex
Normal file
1
semester3/spca/parts/00_asm/05_stack.tex
Normal file
@@ -0,0 +1 @@
|
|||||||
|
\subsection{The Stack}
|
||||||
Binary file not shown.
Reference in New Issue
Block a user