mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-03-14 23:10:03 +01:00
[SPCA] Update code imports
This commit is contained in:
@@ -38,6 +38,4 @@ What matters most today are non-technical factors such as existance of code for
|
||||
|
||||
\shade{purple}{Example} A \verb|C| function that simply adds $2$ arguments might be compiled (unoptimized) to this:
|
||||
|
||||
\inputcodewithfilename{gas}{code-examples/01_asm/}{01_sum.s}
|
||||
|
||||
|
||||
\inputcodewithfilename{gas}{}{code-examples/01_asm/01_sum.s}
|
||||
|
||||
@@ -6,4 +6,4 @@ Note that \verb|move| instructions \textit{cannot} move data directly from memor
|
||||
|
||||
The following instruction formats are commonly used:
|
||||
|
||||
\inputcodewithfilename{gas}{code-examples/01_asm/}{00_operations.s}
|
||||
\inputcodewithfilename{gas}{}{code-examples/01_asm/00_operations.s}
|
||||
|
||||
@@ -9,23 +9,23 @@ The compiler may translate it very differently depending on the context of the p
|
||||
\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}
|
||||
\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}
|
||||
\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}
|
||||
\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}
|
||||
\inputcodewithfilename{gas}{}{code-examples/01_asm/05_factorial.s}
|
||||
|
||||
\subsubsection{For Loops \& Switch}
|
||||
|
||||
|
||||
@@ -69,9 +69,9 @@ The stack is commonly used for recursive functions. A recursive factorial functi
|
||||
Note how \texttt{\%rbx} is saved on the stack, since \texttt{rbx} is callee-saved by convention. \\
|
||||
\texttt{\%eax} is used directly, since it is caller-saved by convention.
|
||||
|
||||
\inputcodewithfilename{gas}{code-examples/01_asm/}{06_factorial.s}
|
||||
\inputcodewithfilename{gas}{}{code-examples/01_asm/06_factorial.s}
|
||||
|
||||
A more complex example, passing addresses as arguments: \\
|
||||
This function swaps 2 array elements (using a \texttt{swap} function) and adds the first value to an accumulator.
|
||||
|
||||
\inputcodewithfilename{gas}{code-examples/01_asm/}{07_swap_and_sum.s}
|
||||
\inputcodewithfilename{gas}{}{code-examples/01_asm/07_swap_and_sum.s}
|
||||
|
||||
@@ -9,11 +9,11 @@ The \texttt{setjmp( jmp\_buf env } function stores the current stack / environme
|
||||
The \texttt{longjmp( jmp\_buf env, int val )} function causes a second return, which returns \texttt{val},
|
||||
to the \texttt{setjmp} invocation and jumps back to that place.
|
||||
|
||||
\inputcodewithfilename{c}{code-examples/01_asm/}{08_unorthodox-controlflow.c}
|
||||
\inputcodewithfilename{c}{}{code-examples/01_asm/08_unorthodox-controlflow.c}
|
||||
|
||||
What the above code outputs is: \texttt{second} followed by \texttt{main}.
|
||||
|
||||
\newpage
|
||||
They are implemented in Assembly as follows. Nothing really surprising for the implementation there.
|
||||
The assembly code is from the Musl \lC\ library
|
||||
\inputcodewithfilename{gas}{code-examples/01_asm/}{09_setjmp-longjmp.s}
|
||||
\inputcodewithfilename{gas}{}{code-examples/01_asm/09_setjmp-longjmp.s}
|
||||
|
||||
@@ -6,7 +6,7 @@ An example is a decompresser that calls a parser when it has finished compressin
|
||||
We can implement that either by rewriting the functions into a single function, which often is a bit clumsy.
|
||||
A way around this is to use \bi{Continuations}, where the first function saves its state and the context is switched to the other function.
|
||||
That function can then load its state and continue where it left off, runs until it finishes its task, then saves its state and the context switches back to the original function.
|
||||
\inputcodewithfilename{c}{code-examples/01_asm/}{10_coroutine.h}
|
||||
\inputcodewithfilename{c}{code-examples/01_asm/}{10_coroutine.c}
|
||||
\inputcodewithfilename{c}{}{code-examples/01_asm/10_coroutine.h}
|
||||
\inputcodewithfilename{c}{}{code-examples/01_asm/10_coroutine.c}
|
||||
|
||||
As you can see, the \texttt{setjmp.h} functions are the foundation of all concurrent programming.
|
||||
|
||||
Reference in New Issue
Block a user