diff --git a/semester3/spca/code-examples/01_asm/02_max.s b/semester3/spca/code-examples/01_asm/02_max.s new file mode 100644 index 0000000..465597c --- /dev/null +++ b/semester3/spca/code-examples/01_asm/02_max.s @@ -0,0 +1,9 @@ +max: + cmpl %esi, %edi // Set condition flags + jle .IF // Conditional jump if %edi <= %esi + movl %edi, %edx // %edi -> return register + jmp .ELSE +.IF: + movl %esi, %edx // &esi -> return register +.ELSE: + ret \ No newline at end of file diff --git a/semester3/spca/code-examples/01_asm/03_absdiff.s b/semester3/spca/code-examples/01_asm/03_absdiff.s new file mode 100644 index 0000000..9b00d35 --- /dev/null +++ b/semester3/spca/code-examples/01_asm/03_absdiff.s @@ -0,0 +1,8 @@ +absdiff: + movl %edi, %eax + subl %esi, %eax // arg2 - arg1 -> eax + movl %esi, %edx + subl %edi, %edx // arg1 - arg2 -> edx + cmpl %esi, %edi // Set condition flags + cmovle %edx, %eax // edx -> eax, only if eax <= edx + 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 464bb8b..00ee198 100644 --- a/semester3/spca/parts/00_asm/03_operations.tex +++ b/semester3/spca/parts/00_asm/03_operations.tex @@ -1,10 +1,12 @@ \subsection{Operations} Assembly operations include performing arithmetic or logic functions on registers or memory data, -transferring data between memory and registers and transferring control (conditional or unconditional jumps) +transferring data between memory and registers and transferring control (conditional or unconditional jumps).\\ +Note that \verb|move| instructions \textit{cannot} move data directly from memory to memory. Most of the memory-related instructions in \texttt{x86} assembly have the format \mint{asm}|movq dest, src| -whereas arithmetic / logic operations have the format inversed (below \texttt{X} is to be replced with size postfix). + +Arithmetic / logic operations have the format inversed (below \texttt{X} is to be replced with size postfix). \begin{tables}{lll}{Mnemonic & Format & Computation} \texttt{addl} & \texttt{Src, Dest} & \texttt{Dest} $\gets$ \texttt{Dest + Src} \\ \texttt{subX} & \texttt{Src, Dest} & \texttt{Dest} $\gets$ \texttt{Dest - Src} \\ diff --git a/semester3/spca/parts/00_asm/04_control-flow.tex b/semester3/spca/parts/00_asm/04_control-flow.tex new file mode 100644 index 0000000..ad36b40 --- /dev/null +++ b/semester3/spca/parts/00_asm/04_control-flow.tex @@ -0,0 +1,15 @@ +\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} \ No newline at end of file diff --git a/semester3/spca/spca-summary.pdf b/semester3/spca/spca-summary.pdf new file mode 100644 index 0000000..be792ad Binary files /dev/null and b/semester3/spca/spca-summary.pdf differ diff --git a/semester3/spca/spca-summary.tex b/semester3/spca/spca-summary.tex index 964aadf..4647f72 100644 --- a/semester3/spca/spca-summary.tex +++ b/semester3/spca/spca-summary.tex @@ -86,6 +86,7 @@ If there are changes and you'd like to update this summary, please open a pull r \input{parts/00_asm/01_syntax.tex} \input{parts/00_asm/02_data-types.tex} \input{parts/00_asm/03_operations.tex} +\input{parts/00_asm/04_control-flow.tex} % ── Intro to C ──────────────────────────────────────────────────────