├── Cheat Sheet.pdf ├── Makefile ├── LICENSE ├── README.md ├── .gitignore └── Cheat Sheet.tex /Cheat Sheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jstrieb/systems-programming-cheat-sheet/HEAD/Cheat Sheet.pdf -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Created by Jacob Strieb 2 | # June 2021 3 | 4 | # Swap these two if running in WSL 5 | # PDFLATEX=pdflatex.exe 6 | PDFLATEX=pdflatex 7 | 8 | Cheat\ Sheet.pdf: Cheat\ Sheet.tex 9 | $(PDFLATEX) "$<" || (touch "$@"; false) 10 | $(PDFLATEX) "$<" 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Jacob Strieb 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Systems Programming Cheat Sheet 2 | 3 | [Click here to view the cheat sheet 4 | PDF](https://github.com/jstrieb/systems-programming-cheat-sheet/raw/master/Cheat%20Sheet.pdf). 5 | 6 | Discussed on [Hacker News](https://news.ycombinator.com/item?id=27693612) and 7 | [r/programming](https://old.reddit.com/r/programming/comments/ob78tp/i_open_sourced_my_systems_programming_cheat_sheet/). 8 | 9 | # Introduction & Background 10 | 11 | This cheat sheet was originally written while I was taking [Introduction to 12 | Computer Systems (15-213/18-213)](https://www.cs.cmu.edu/~213/) at Carnegie 13 | Mellon University in the Spring 2019 semester. As such, it is based heavily on 14 | the textbook *[Computer Systems: A Programmer's Perspective, Third 15 | Edition](http://csapp.cs.cmu.edu/)* by Randal E. Bryant and David R. 16 | O'Hallaron, as well as other course notes. 17 | 18 | The cheat sheet therefore contains some information that is specifically 19 | tailored to x86-64 Unix systems. For example, the material may include 20 | undefined behaviors particular to such systems. It may also assume 21 | implementation details such as the System V calling convention and AT&T 22 | assembly language syntax. In general, any quirks included are those that 213 23 | students are expected to know. 24 | 25 | The four-page PDF is intended to be printed on two sheets of double-sided 8.5" x 26 | 11" letter paper (sorry non-Americans). 27 | 28 | The course allowed students to bring one double-sided piece of letter paper 29 | with notes for each exam, so I tried to cram as much as possible into the 30 | limited space. These are my cheat sheets from the midterm and final combined 31 | into one PDF. 32 | 33 | If you've opened the PDF and don't know what any of it means, I encourage you 34 | to access the [lecture slides from when I took the 35 | course](http://www.cs.cmu.edu/afs/cs/academic/class/15213-s19/www/schedule.html). 36 | These and the textbook should be enough to supplement the cheat sheet so that 37 | the material (or at least the acronyms) make some sense. 38 | 39 | # Project Status & Contributing 40 | 41 | This project is basically done. I created it while I was a student, and will 42 | happily make any corrections necessary. But I do not plan to expand the cheat 43 | sheet to include new material. Feel free to open an issue and ask about adding 44 | material, though. 45 | 46 | For any corrections or discussions, please [open an 47 | issue](https://github.com/jstrieb/systems-programming-cheat-sheet/issues/new). 48 | Pull requests without prior discussion will be ignored. 49 | 50 | # Support the Project 51 | 52 | There are a few things you can do to support the project: 53 | 54 | - Star the repository (and follow me on GitHub for more) 55 | - Share and upvote on sites like Twitter, Reddit, and Hacker News 56 | - Look carefully for errors and report any that you find 57 | 58 | These things motivate me to to keep sharing what I build, and they provide 59 | validation that my work is appreciated! Thanks in advance! 60 | 61 | # Modifying & Compiling 62 | 63 | Clone the repository. 64 | 65 | ``` bash 66 | git clone https://github.com/jstrieb/systems-programming-cheat-sheet.git && cd systems-programming-cheat-sheet 67 | ``` 68 | 69 | Make any modifications to [`Cheat 70 | Sheet.tex`](https://github.com/jstrieb/systems-programming-cheat-sheet/blob/master/Cheat%20Sheet.tex). 71 | 72 | Recompile with `make`. 73 | 74 | # Acknowledgments 75 | 76 | - The entire (Spring 2019) 15-213 course staff 77 | - Randy Bryant and David O'Hallaron for their excellent textbook 78 | - [Kirubel Aklilu](https://github.com/kirubelaklilu1), Matt Henderson, and 79 | [Hrishikesh Bodas](https://github.com/hbodas) for studying with me when we 80 | all took 213 81 | - Anyone who has taken the time to read the cheat sheet and report errors 82 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Core latex/pdflatex auxiliary files: 2 | *.aux 3 | *.lof 4 | *.log 5 | *.lot 6 | *.fls 7 | *.out 8 | *.toc 9 | *.fmt 10 | *.fot 11 | *.cb 12 | *.cb2 13 | .*.lb 14 | 15 | ## Intermediate documents: 16 | *.dvi 17 | *.xdv 18 | *-converted-to.* 19 | # these rules might exclude image files for figures etc. 20 | # *.ps 21 | # *.eps 22 | # *.pdf 23 | 24 | ## Generated if empty string is given at "Please type another file name for output:" 25 | .pdf 26 | 27 | ## Bibliography auxiliary files (bibtex/biblatex/biber): 28 | *.bbl 29 | *.bcf 30 | *.blg 31 | *-blx.aux 32 | *-blx.bib 33 | *.run.xml 34 | 35 | ## Build tool auxiliary files: 36 | *.fdb_latexmk 37 | *.synctex 38 | *.synctex(busy) 39 | *.synctex.gz 40 | *.synctex.gz(busy) 41 | *.pdfsync 42 | 43 | ## Build tool directories for auxiliary files 44 | # latexrun 45 | latex.out/ 46 | 47 | ## Auxiliary and intermediate files from other packages: 48 | # algorithms 49 | *.alg 50 | *.loa 51 | 52 | # achemso 53 | acs-*.bib 54 | 55 | # amsthm 56 | *.thm 57 | 58 | # beamer 59 | *.nav 60 | *.pre 61 | *.snm 62 | *.vrb 63 | 64 | # changes 65 | *.soc 66 | 67 | # comment 68 | *.cut 69 | 70 | # cprotect 71 | *.cpt 72 | 73 | # elsarticle (documentclass of Elsevier journals) 74 | *.spl 75 | 76 | # endnotes 77 | *.ent 78 | 79 | # fixme 80 | *.lox 81 | 82 | # feynmf/feynmp 83 | *.mf 84 | *.mp 85 | *.t[1-9] 86 | *.t[1-9][0-9] 87 | *.tfm 88 | 89 | #(r)(e)ledmac/(r)(e)ledpar 90 | *.end 91 | *.?end 92 | *.[1-9] 93 | *.[1-9][0-9] 94 | *.[1-9][0-9][0-9] 95 | *.[1-9]R 96 | *.[1-9][0-9]R 97 | *.[1-9][0-9][0-9]R 98 | *.eledsec[1-9] 99 | *.eledsec[1-9]R 100 | *.eledsec[1-9][0-9] 101 | *.eledsec[1-9][0-9]R 102 | *.eledsec[1-9][0-9][0-9] 103 | *.eledsec[1-9][0-9][0-9]R 104 | 105 | # glossaries 106 | *.acn 107 | *.acr 108 | *.glg 109 | *.glo 110 | *.gls 111 | *.glsdefs 112 | *.lzo 113 | *.lzs 114 | 115 | # uncomment this for glossaries-extra (will ignore makeindex's style files!) 116 | # *.ist 117 | 118 | # gnuplottex 119 | *-gnuplottex-* 120 | 121 | # gregoriotex 122 | *.gaux 123 | *.glog 124 | *.gtex 125 | 126 | # htlatex 127 | *.4ct 128 | *.4tc 129 | *.idv 130 | *.lg 131 | *.trc 132 | *.xref 133 | 134 | # hyperref 135 | *.brf 136 | 137 | # knitr 138 | *-concordance.tex 139 | # TODO Uncomment the next line if you use knitr and want to ignore its generated tikz files 140 | # *.tikz 141 | *-tikzDictionary 142 | 143 | # listings 144 | *.lol 145 | 146 | # luatexja-ruby 147 | *.ltjruby 148 | 149 | # makeidx 150 | *.idx 151 | *.ilg 152 | *.ind 153 | 154 | # minitoc 155 | *.maf 156 | *.mlf 157 | *.mlt 158 | *.mtc[0-9]* 159 | *.slf[0-9]* 160 | *.slt[0-9]* 161 | *.stc[0-9]* 162 | 163 | # minted 164 | _minted* 165 | *.pyg 166 | 167 | # morewrites 168 | *.mw 169 | 170 | # newpax 171 | *.newpax 172 | 173 | # nomencl 174 | *.nlg 175 | *.nlo 176 | *.nls 177 | 178 | # pax 179 | *.pax 180 | 181 | # pdfpcnotes 182 | *.pdfpc 183 | 184 | # sagetex 185 | *.sagetex.sage 186 | *.sagetex.py 187 | *.sagetex.scmd 188 | 189 | # scrwfile 190 | *.wrt 191 | 192 | # sympy 193 | *.sout 194 | *.sympy 195 | sympy-plots-for-*.tex/ 196 | 197 | # pdfcomment 198 | *.upa 199 | *.upb 200 | 201 | # pythontex 202 | *.pytxcode 203 | pythontex-files-*/ 204 | 205 | # tcolorbox 206 | *.listing 207 | 208 | # thmtools 209 | *.loe 210 | 211 | # TikZ & PGF 212 | *.dpth 213 | *.md5 214 | *.auxlock 215 | 216 | # todonotes 217 | *.tdo 218 | 219 | # vhistory 220 | *.hst 221 | *.ver 222 | 223 | # easy-todo 224 | *.lod 225 | 226 | # xcolor 227 | *.xcp 228 | 229 | # xmpincl 230 | *.xmpi 231 | 232 | # xindy 233 | *.xdy 234 | 235 | # xypic precompiled matrices and outlines 236 | *.xyc 237 | *.xyd 238 | 239 | # endfloat 240 | *.ttt 241 | *.fff 242 | 243 | # Latexian 244 | TSWLatexianTemp* 245 | 246 | ## Editors: 247 | # WinEdt 248 | *.bak 249 | *.sav 250 | 251 | # Texpad 252 | .texpadtmp 253 | 254 | # LyX 255 | *.lyx~ 256 | 257 | # Kile 258 | *.backup 259 | 260 | # gummi 261 | .*.swp 262 | 263 | # KBibTeX 264 | *~[0-9]* 265 | 266 | # TeXnicCenter 267 | *.tps 268 | 269 | # auto folder when using emacs and auctex 270 | ./auto/* 271 | *.el 272 | 273 | # expex forward references with \gathertags 274 | *-tags.tex 275 | 276 | # standalone packages 277 | *.sta 278 | 279 | # Makeindex log files 280 | *.lpz 281 | 282 | # xwatermark package 283 | *.xwm 284 | 285 | # REVTeX puts footnotes in the bibliography by default, unless the nofootinbib 286 | # option is specified. Footnotes are the stored in a file with suffix Notes.bib. 287 | # Uncomment the next line to have this generated file ignored. 288 | #*Notes.bib 289 | -------------------------------------------------------------------------------- /Cheat Sheet.tex: -------------------------------------------------------------------------------- 1 | % Created by Jacob Strieb 2 | % Spring 2019 3 | 4 | %\documentclass{article} 5 | \documentclass[twocolumn]{article} 6 | 7 | % Formatting 8 | \usepackage[letterpaper, margin=0.5in]{geometry} 9 | \usepackage{microtype} 10 | \usepackage{titlesec} 11 | \pagenumbering{gobble} 12 | 13 | % Sections with no numbers and minimal vertical spacing 14 | \titleformat{\section}{\normalfont\Large\bfseries}{}{0pt}{}[\vspace{-8pt}] 15 | 16 | % Misc. Imports 17 | \usepackage{enumerate} 18 | \usepackage{enumitem} 19 | \usepackage{multicol} 20 | 21 | % Document title and metadata 22 | \usepackage[pdftex, 23 | pdfauthor={Jacob Strieb}, 24 | pdftitle={Unix Systems Programming Cheat Sheet}]{hyperref} 25 | 26 | % Custom commands 27 | \renewcommand{\tt}[1]{\texttt{#1}} 28 | 29 | \begin{document} 30 | 31 | \section{Integers} 32 | \begin{tabular}{| c || c | c | c ||| c | c |} 33 | \hline 34 | $n$ & $2^n$ & Hex & Binary & $n$ & $2^n$\\ 35 | \hline \hline 36 | 0 & 1 & \tt{0x00} & 00000 & 17 & 131072 \\ 37 | \hline 38 | 1 & 2 & \tt{0x01} & 00001 & 18 & 262144 \\ 39 | \hline 40 | 2 & 4 & \tt{0x02} & 00010 & 19 & 524288 \\ 41 | \hline 42 | 3 & 8 & \tt{0x03} & 00011 & 20 & 1048576 \\ 43 | \hline 44 | 4 & 16 & \tt{0x04} & 00100 & 21 & 2097152 \\ 45 | \hline 46 | 5 & 32 & \tt{0x05} & 00101 & 22 & 4194304 \\ 47 | \hline 48 | 6 & 64 & \tt{0x06} & 00110 & 23 & 8388608 \\ 49 | \hline 50 | 7 & 128 & \tt{0x07} & 00111 & 24 & 16777216 \\ 51 | \hline 52 | 8 & 256 & \tt{0x08} & 01000 & 25 & 33554432 \\ 53 | \hline 54 | 9 & 512 & \tt{0x09} & 01001 & 26 & 67108864 \\ 55 | \hline 56 | 10 & 1024 & \tt{0x0A} & 01010 & 27 & 134217728 \\ 57 | \hline 58 | 11 & 2048 & \tt{0x0B} & 01011 & 28 & 268435456 \\ 59 | \hline 60 | 12 & 4096 & \tt{0x0C} & 01100 & 29 & 536870912 \\ 61 | \hline 62 | 13 & 8192 & \tt{0x0D} & 01101 & 30 & 1073741824 \\ 63 | \hline 64 | 14 & 16384 & \tt{0x0E} & 01110 & 31 & 2147483648 \\ 65 | \hline 66 | 15 & 32768 & \tt{0x0F} & 01111 & 32 & 4294967296 \\ 67 | \hline 68 | 16 & 65536 & \tt{0x10} & 10000 & 33 & 8589934592 \\ 69 | \hline 70 | \end{tabular} 71 | 72 | \begin{itemize}[noitemsep] 73 | \item \textbf{Word size} -- nominal size of pointer data 74 | \item Addresses go up to $2^w - 1$ for $w$-bit word size 75 | \item \textbf{Little Endian} -- least significant byte comes first 76 | \item \textbf{Big Endian} -- most significant byte comes first 77 | \item Example of storing \tt{0x1234567} at \tt{0x100} \\ 78 | \begin{tabular}{| r || c | c | c | c |} 79 | \hline 80 | & \tt{0x100} & \tt{0x101} & \tt{0x102} & \tt{0x103} \\ \hline 81 | Big endian & \tt{01} & \tt{23} & \tt{45} & \tt{67} \\ \hline 82 | Little endian & \tt{67} & \tt{45} & \tt{23} & \tt{01} \\ \hline 83 | \end{tabular} \\ 84 | \item Arithmetic between signed and unsigned values automatically casts all signed values to unsigned 85 | \item $-TMin_w = TMin_w$ 86 | \item $\lceil x/2^k \rceil$ is given by \tt{(x + (1 << k) - 1) >> k} 87 | \item $x/2^k$ is given by \tt{(x<0 ? x+(1<> k} 88 | \end{itemize} 89 | 90 | \begin{tabular}{| c || c | c | c |} 91 | \hline 92 | & \multicolumn{3}{c|}{Word size $w$} \\ \cline{2-4} 93 | Value & 8 & 16 & 32 \\ \hline 94 | $UMax_w$ & \tt{0xFF} & \tt{0xFFFF} & \tt{0xFFFFFFFF} \\ 95 | & 255 & 65,535 & 4,294,967,295 \\ \hline 96 | $TMin_w$ & \tt{0x80} & \tt{0x8000} & \tt{0x80000000} \\ 97 | & $-128$ & $-32,768$ & $-2,147,483,648$ \\ \hline 98 | $TMax_w$ & \tt{0x7F} & \tt{0x7FFF} & \tt{0x7FFFFFFF} \\ 99 | & $127$ & $32,767$ & $2,147,483,647$ \\ \hline 100 | $-1$ & \tt{0xFF} & \tt{0xFFFF} & \tt{0xFFFFFFFF} \\ \hline 101 | \end{tabular} 102 | 103 | \section{Bitwise Operations} 104 | \begin{itemize}[noitemsep] 105 | \item \textbf{Logical shift} -- Fills left end with zeros 106 | \item \textbf{Arithmetic shift} -- Sign-extends left end 107 | \end{itemize} 108 | 109 | \begin{multicols}{4} 110 | \begin{tabular}{r | c} 111 | \tt{\~} & \\ \hline 112 | 0 & 1 \\ 113 | 1 & 0 114 | \end{tabular} 115 | \begin{tabular}{r | c c} 116 | \tt{\&} & 0 & 1 \\ \hline 117 | 0 & 0 & 0 \\ 118 | 1 & 0 & 1 119 | \end{tabular} 120 | \begin{tabular}{r | c c} 121 | \tt{|} & 0 & 1 \\ \hline 122 | 0 & 0 & 1 \\ 123 | 1 & 1 & 1 124 | \end{tabular} 125 | \begin{tabular}{r | c c} 126 | \tt{\^} & 0 & 1 \\ \hline 127 | 0 & 0 & 1 \\ 128 | 1 & 1 & 0 129 | \end{tabular} 130 | \end{multicols} 131 | 132 | \vfill 133 | 134 | \section{Floating Point} 135 | \begin{itemize}[noitemsep] 136 | \item Floating point lacks associativity 137 | \item $V = (-1)^s \times M \times 2^E$ 138 | \item Sign bit $s$ -- whether the number is positive or negative, represented by 1-bit field 139 | \item Exponent $E$ weights the value by a possibly negative power of 2, represented by $k$-bit \tt{exp} field 140 | \item Significand (mantissa) $M$ -- fractional binary number between 1 and $2 - \varepsilon$ or between 0 and $1 - \varepsilon$, represented by $n$-bit \tt{frac} field ($f_{n - 1}\cdots f_1 f_0$) 141 | \item Normalized values 142 | \begin{itemize}[noitemsep] 143 | \item Most common case 144 | \item \tt{exp} is neither all zeros nor all ones 145 | \item Exponent field represents biased signed integer 146 | \item $E = e - $Bias where $e$ is the unsigned number in \tt{exp} and Bias $= 2^{k - 1} - 1$ 147 | \item \tt{frac} represents $0 \leq f < 1$ with $0.f_{n - 1}\cdots f_1 f_0$ and $M = 1 + f$ -- implied leading 1 148 | \end{itemize} 149 | \item Denormalized values 150 | \begin{itemize}[noitemsep] 151 | \item Exponent field all zeros 152 | \item Exponent value is $E = 1 - $Bias, significand value is $M = f$ (no leading 1) 153 | \item Numbers close to zero (inclusive), evenly spaced near 0.0 154 | \end{itemize} 155 | \item Special values 156 | \begin{itemize}[noitemsep] 157 | \item Exponent field is all ones 158 | \item Fraction field all zeros can represent $\pm \infty$, depending on sign bit 159 | \item Nonzero fraction field is NaN 160 | \end{itemize} 161 | \item Rounding 162 | \begin{itemize}[noitemsep] 163 | \item Rounds to the nearest even 164 | \item \tt{BBGRXXXX} 165 | \item \tt{G} -- Guard bit; least significant bit of result 166 | \item \tt{R} -- Round bit; first bit removed 167 | \item \tt{XXXX} -- Sticky bit; OR of remaining bits 168 | \item Round up conditions: 169 | \begin{itemize}[noitemsep] 170 | \item Round = 1, Sticky = 1 $\to\ > 0.5$ 171 | \item Guard = 1, Round = 1, Sticky = 0 $\to$ Round to even 172 | \end{itemize} 173 | \end{itemize} 174 | \item Multiplication 175 | \begin{itemize}[noitemsep] 176 | \item $(-1)^{s_1}M_12^{E_1} \times (-1)^{s_2}M_22^{E_2}$ \\ 177 | \null \qquad \qquad \qquad \qquad $= (-1)^{s_1 \string^ s_2}(M_1 \times M_2)2^{E_1 + E_2}$ 178 | \item If $M_1 \times M_2 = M \geq 2$ shift $M$ right, increment $E = E_1 + E_2$ 179 | \item If $E$ out of range, overflow 180 | \item Round $M$ to fit \tt{frac} precision 181 | \end{itemize} 182 | \end{itemize} 183 | % TODO: Add table here from page 116 184 | 185 | \vfill 186 | \pagebreak 187 | 188 | \section{x86-64 Data Alignment} 189 | \begin{itemize}[noitemsep] 190 | \item Internal padding -- added between struct elements 191 | \item External padding -- added after struct elements 192 | \item The entire struct is externally padded to align to its largest element 193 | \end{itemize} 194 | 195 | \begin{tabular}{| c || c |} \hline 196 | $K$ & Types \\ \hline 197 | 1 & \tt{char} \\ \hline 198 | 2 & \tt{short} \\ \hline 199 | 4 & \tt{int, float} \\ \hline 200 | 8 & \tt{long, double, char *} \\ \hline 201 | \end{tabular} 202 | 203 | \vfill 204 | %\pagebreak 205 | 206 | \section{Caches} 207 | \begin{itemize}[noitemsep] 208 | \item $M = 2^m$ unique addresses of $m$ bits 209 | \item $S = 2^s$ cache sets 210 | \item Each set consists of $E$ cache lines 211 | \item Each line consists of a data block of $B = 2^b$ bytes, a valid bit and $t = m - (b + s)$ tag bits 212 | \item Capacity of a cache is $C = S \times E \times B$ 213 | \item Address \\ 214 | 215 | \begin{tabular}{| c | c | c |} 216 | $t$ bits & $s$ bits & $b$ bits \\ \hline 217 | $\leftarrow m - 1$ && $0 \rightarrow$ \\ \hline 218 | Tag & Set index & Block offset 219 | \end{tabular} \\ 220 | \item Direct-mapped cache has one line per set ($E = 1$) 221 | \item Non-direct caches sometimes referred to as $E$-way set associative cache 222 | \item Fully-associative cache has one set ($E = C/B$). 223 | \end{itemize} 224 | 225 | \section{Conditional Control} 226 | \begin{itemize}[noitemsep] 227 | \item Carry flag (\tt{CF}) -- most recent op generated carry of most significant bit, detects overflow for unsigned 228 | \item Zero flag (\tt{ZF}) -- most recent op yielded zero 229 | \item Sign flag (\tt{SF}) -- most recent op yielded negative value 230 | \item Overflow flag (\tt{OF}) -- most recent op caused two's complement overflow 231 | \item \tt{test} instruction behaves like \tt{and} instructions but sets condition codes without altering source or destination often see \tt{testq \%rax,\%rax} to check if return val is neg, zero, or pos 232 | \end{itemize} 233 | 234 | \tt{set D} and \tt{jmp} suffixes \\ 235 | \begin{tabular}{| c || c | c | c |} 236 | \hline 237 | \textbf{Instruction} & \textbf{Syn.} & \textbf{Cond.} & \textbf{Desc.} \\ \hline 238 | \tt{-e} & \tt{-z} & \verb|ZF| & $=/0$ \\ \hline 239 | \tt{-ne} & \tt{-nz} & \verb|~ZF| & $!=$/not zero \\ \hline 240 | \tt{-s} & & \verb|SF| & Neg \\ \hline 241 | \tt{-ns} & & \verb|~SF| & Nonneg \\ \hline 242 | \tt{-g} & \tt{-nle} & \verb|~(SF^OF)&~ZF| & signed $>$ \\ \hline 243 | \tt{-ge} & \tt{-nl} & \verb|~(SF^OF)| & signed $=>$ \\ \hline 244 | \tt{-l} & \tt{-nge} & \verb|SF^OF| & signed $<$ \\ \hline 245 | \tt{-le} & \tt{-ng} & \verb=(SF^OF)|ZF= & signed $<=$ \\ \hline 246 | \tt{-a} & \tt{-nbe} & \verb|~CF&~ZF| & unsigned $>$ \\ \hline 247 | \tt{-ae} & \tt{-nb} & \verb|~CF| & unsigned $>=$ \\ \hline 248 | \tt{-b} & \tt{-nae} & \verb|CF| & unsigned $<$ \\ \hline 249 | \tt{-be} & \tt{-na} & \verb=CF|ZF= & unsigned $<=$ \\ \hline 250 | \end{tabular} 251 | 252 | \vfill 253 | \pagebreak 254 | 255 | \section{Assembly Basics} 256 | \begin{itemize}[noitemsep] 257 | \item ``word'' refers to 16-bit data type, with ``double word'' referring to 32-bit (int) and 64-bit quantities referred to as ``quad words'' 258 | \item On 64-bit machines pointers are 8-byte quad words 259 | \item 16 general purpose registers storing 64-bit values (register file) 260 | \item In operands, scaling factor $s$ must be either 1, 2, 4, or 8 261 | \item \tt{mov S, D} has the effect of $S \to D$ 262 | \item \tt{movzbq} moves from byte to quad with zero-extended whereas \tt{movsbq} does the same but sign-extended 263 | \item Stack grows down if increasing addresses grow up -- ``top'' of the stack at the bottom 264 | \item \tt{leaq S, D} has the effect of $\&S \to D$ 265 | \end{itemize} 266 | 267 | \begin{tabular}{| c || c | c |} 268 | \hline 269 | \textbf{Type} & \textbf{Form} & \textbf{Operand value} \\ \hline 270 | Immediate & $\$Imm$ & $Imm$ \\ \hline 271 | Register & $r_a$ & $R[r_a]$ \\ \hline 272 | Memory & $Imm(r_b, r_i, s)$ & M[$Imm + R[r_b] + R[r_i] \cdot s$] \\ \hline 273 | \end{tabular} \\ \ \\ 274 | 275 | \begin{tabular}{| c || c | c | c | c |} 276 | \hline 277 | \textbf{Type} & \textbf{64-bits} & \textbf{32-bits} & \textbf{16-bits} & \textbf{8-bits} \\ \hline 278 | Return val & \tt{\%rax} & \tt{\%eax} & \tt{\%ax} & \tt{\%al} \\ \hline 279 | Callee & \tt{\%rbx} & \tt{\%ebx} & \tt{\%bx} & \tt{\%bl} \\ \hline 280 | 1st arg & \tt{\%rdi} & \tt{\%edi} & \tt{\%di} & \tt{\%dil} \\ \hline 281 | 2nd arg & \tt{\%rsi} & \tt{\%esi} & \tt{\%si} & \tt{\%sil} \\ \hline 282 | 3rd arg & \tt{\%rdx} & \tt{\%edx} & \tt{\%dx} & \tt{\%dl} \\ \hline 283 | 4th arg & \tt{\%rcx} & \tt{\%ecx} & \tt{\%cx} & \tt{\%cl} \\ \hline 284 | 5th arg & \tt{\%r8} & \tt{\%r8d} & \tt{\%r8w} & \tt{\%r8b} \\ \hline 285 | 6th arg & \tt{\%r9} & \tt{\%r9d} & \tt{\%r9w} & \tt{\%r9b} \\ \hline 286 | Callee & \tt{\%rbp} & \tt{\%ebp} & \tt{\%bp} & \tt{\%bpl} \\ \hline 287 | Stack ptr & \tt{\%rsp} & \tt{\%esp} & \tt{\%sp} & \tt{\%spl} \\ \hline 288 | Caller & \tt{\%r10} & \tt{\%r10d} & \tt{\%r10w} & \tt{\%r10b} \\ \hline 289 | Caller & \tt{\%r11} & \tt{\%r11d} & \tt{\%r11w} & \tt{\%r11b} \\ \hline 290 | Callee & \tt{\%r12} & \tt{\%r12d} & \tt{\%r12w} & \tt{\%r12b} \\ \hline 291 | Callee & \tt{\%r13} & \tt{\%r13d} & \tt{\%r13w} & \tt{\%r13b} \\ \hline 292 | Callee & \tt{\%r14} & \tt{\%r14d} & \tt{\%r14w} & \tt{\%r14b} \\ \hline 293 | Callee & \tt{\%r15} & \tt{\%r15d} & \tt{\%r15w} & \tt{\%r15b} \\ \hline 294 | \end{tabular} 295 | 296 | \section{Linking} 297 | \begin{itemize}[noitemsep] 298 | \item Relocatable object files -- combine with other relocatables at compile time to create executables, made by compiler and assembler 299 | \item Executable object files -- contain binary data that can be directly copied to memory and executed, made by linker 300 | \item Shared object files -- special relocatable objects that can be linked dynamically at load or run time 301 | \item Static symbols -- defined locally to an object file (module) 302 | \item Global symbols -- defined locally and referred to elsewhere 303 | \item Externals -- Global symbols referenced locally but defined elsewhere 304 | \item Local linker symbols are different from local program variables 305 | \item Functions and initialized global variables are exported to the assembler as strong by the compiler 306 | \item Uninitialized global variables are weak 307 | \item Linkers error on multiple same-name strong symbols, pick strong over weak, and randomly choose from weak symbols 308 | \end{itemize} 309 | 310 | \section{Processes} 311 | \begin{itemize}[noitemsep] 312 | \item \tt{getpid(void)} -- \tt{pid\_t} of the process 313 | \item \tt{getppid(void)} -- \tt{pid\_t} of the parent process 314 | \item Processes are running, stopped, or terminated 315 | \item \tt{exit(int status)} -- called once, never returns 316 | \item \tt{fork(void)} -- \tt{pid\_t} of child in parent or 0 if in child; called once, returns twice 317 | \item Open file table and vnode table are managed by OS and shared across processes, file descriptor table is process-specific 318 | \item \tt{waitpid(pid\_t pid, int *statusp, int options)} -- returns \tt{pid\_t} of child, 0 if not waiting (\tt{WNOHANG}) or error 319 | \begin{itemize}[noitemsep] 320 | \item \tt{WNOHANG} -- return (0) immediately and don't wait for child 321 | \item \tt{WUNTRACED} -- check for terminated and stop children 322 | \item \tt{WCONTINUED} -- check for waiting (child) process to be continued 323 | \item OR (\tt{|}) flags together to form a bit vector 324 | \item \tt{WIFEXITED(status)} -- whether child exited normally 325 | \item \tt{WEXITSTATUS(status)} -- returns exit status of terminated child if \tt{WIFEXITED} is true 326 | \item \tt{WIFSIGNALED \& WTERMSIG} -- as above but for signals 327 | \item \tt{WIFSTOPPED \& WSTOPSIG \& WIFCONTINUED} -- as above for stopped/continued processes 328 | \end{itemize} 329 | \item \tt{wait(int *statusp)} $\cong$ \tt{waitpid(-1, \&status, 0)} 330 | \item \tt{sleep(unsigned int secs)} -- returns short counts 331 | \item \tt{pause(void)} -- sleeps until a signal is received 332 | \item \tt{execve(char *filename, char *argv[], char *envp[])} -- does not return unless error; last arg in each array is \tt{NULL} 333 | \end{itemize} 334 | 335 | \section{Virtual Memory} 336 | \begin{itemize}[noitemsep] 337 | \item $N = 2^n$ addresses in $n$-bit virtual address space 338 | \item $M = 2^m$ addresses in $m$-bit physical address space (not necessarily power of 2) 339 | \item $P = 2^p$ bytes per virtual/physical page 340 | \item Virtual pages are either unallocated, cached (allocated in PM), or uncached (allocated, not in PM) 341 | \item DRAM caches are often fully associative 342 | \item Page table maps virtual pages to physical pages 343 | \item Valid bit in PTE set indicates cached in PM, rest indicates virtual or physical address (depending on valid bit) 344 | \item Page fault is DRAM cache miss; triggers exception 345 | \item Translation lookaside buffer (TLB) is a cache of PTEs; each line holds a block with one PTE; highly associative 346 | \item TLB has $T = 2^t$ sets 347 | \item Virtual address \\ 348 | 349 | \begin{tabular}{| c | c | c |} 350 | \multicolumn{2}{|c|}{Virtual Page Number} & \\ \hline 351 | $\leftarrow n - 1$ & $\leftarrow p + t \ |\ p \rightarrow$ & $0 \rightarrow$ \\ \hline 352 | TLB Tag & TLB index & Virtual page offset 353 | \end{tabular} 354 | \end{itemize} 355 | 356 | \section{Dynamic Memory Allocation} 357 | \begin{itemize}[noitemsep] 358 | \item \tt{sbrk(intptr\_t incr)} -- extend the heap by \tt{incr} (basically just an \tt{int}) and return the old break pointer 359 | \end{itemize} 360 | 361 | \vfill 362 | 363 | \section{Signals} 364 | \begin{itemize}[noitemsep] 365 | \item Default action of \tt{SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP} is to ignore and stop (respectively per pair) 366 | \item Signals can be sent to process groups; child inherits parent process group by default; \tt{setpgid(0, 0)} sets the process group ID to the current process id 367 | \item \tt{kill(pid\_t pid, int sig)} -- send a \tt{sig} to \tt{pid}, unless \tt{pid} is 0 then send it to every process in the process group of the calling process; if \tt{pid} $< 0$, send to every process in process group $|$\tt{pid}$|$ 368 | \item \tt{signal(int sig, void *hndlr\_t (int) handler)} -- handle \tt{sig} with \tt{handler} function pointer; \tt{handler} can be \tt{SIG\_IGN} to ignore or \tt{SIG\_DFL} for default 369 | \item \tt{sigprocmask(int how, sigset\_t *set, sigset\_t *oldset)} 370 | \begin{itemize}[noitemsep] 371 | \item \tt{SIG\_BLOCK} -- \tt{blocked = blocked | set} 372 | \item \tt{SIG\_UNBLOCK} -- \tt{blocked = blocked \& ~set} 373 | \item \tt{SIG\_SETMASK} -- \tt{blocked = set} 374 | \item Old bit vector stored in \tt{oldset} (a.k.a. \tt{prev}) 375 | \end{itemize} 376 | \item \tt{sigemptyset(sigset\_t *set)} 377 | \item \tt{sigfillset(sigset\_t *set)} 378 | \item \tt{sigaddset(sigset\_t *set, int sig)} -- add \tt{sig} 379 | \item \tt{sigdelset(sigset\_t *set, int sig)} -- delete \tt{sig} 380 | \item Rules for signal handlers 381 | \begin{enumerate} 382 | \item Keep them simple 383 | \item Only call async-signal-safe functions (reentrant or uninterruptible) 384 | \item Save and restore \tt{errno} 385 | \item Block all signals 386 | \item Declare global variables with \tt{volatile} -- force memory read each time (no storage in registers) 387 | \item Declare flags with \tt{sig\_atomic\_t} -- atomic r/w 388 | \end{enumerate} 389 | \item Signals are \textbf{not} queued 390 | \item \tt{sigsuspend(sigset\_t *mask)} -- atomically replace \tt{blocked} with \tt{mask} and suspend until handler returns after receipt of a signal, then restore \tt{blocked} 391 | \end{itemize} 392 | 393 | \section{Concurrency with Threads} 394 | \begin{itemize}[noitemsep] 395 | \item \tt{pthread\_create(pthread\_t *tid, NULL, void *func(void *), void *arg)} -- run \tt{func} with \tt{arg} in a new thread, joinable by default 396 | \item \tt{pthread\_self(void)} -- return current thread id 397 | \item \tt{pthread\_exit(void *return)} -- exit the current thread 398 | \item \tt{pthread\_cancel(pthread\_t tid)} -- terminate another thread without waiting 399 | \item \tt{pthread\_join(pthread\_t tid, NULL)} -- block and wait for the thread with \tt{tid} to terminate 400 | \item \tt{pthread\_detach(pthread\_t tid)} -- make thread \tt{tid} detached (not joinable), often called on self 401 | \item Threads share everything in memory except for registers and stack, though they can access addresses in other thread stacks 402 | \end{itemize} 403 | 404 | \vfill 405 | 406 | \section{System I/O} 407 | \begin{itemize}[noitemsep] 408 | \item \tt{open(char *filename, int flags, mode\_t mode)} -- returns file descriptor 409 | \begin{itemize}[noitemsep] 410 | \item \tt{O\_RDONLY}, \tt{O\_WRDONLY}, \tt{O\_RDWR} flags 411 | \item \tt{O\_CREAT} -- create a new file if it doesn't exist 412 | \item \tt{O\_TRUNC} -- truncate the file if it exists 413 | \item \tt{O\_APPEND} -- before write, set file pos. to end of file 414 | \item Mode given by OR ($|$) combination of \\ \tt{S\_I\{R, W, X\}\{USR, GRP, OTH\}} 415 | \item Each call creates new open file table entry 416 | \end{itemize} 417 | \item \tt{read(int fd, void *buf, size\_t n)} -- read up to \tt{n} bytes into \tt{buf} and return the number of bytes actually read, update file descriptor table position by return value 418 | \item \tt{write(int fd, void *buf, size\_t n)} -- write up to \tt{n} bytes from \tt{buf}, return the number of bytes actually written, update file descriptor table position by return value 419 | \item Each process has unique descriptor table pointing to entries in global file table 420 | \item OS maintains open file table shared by all processes, each entry has file position, ref count, and pointer to v-node table 421 | \item OS maintains v-node table with information about each file 422 | \item Parent and child process must both close file descriptors for kernel to remove file table entry 423 | \item \tt{dup2(int oldfd, int newfd)} -- copies descriptor entry \tt{oldfd} to \tt{newfd}, overwriting \tt{newfd} (closes \tt{newfd} if open); \textit{i.e.}, \tt{newfd} entry points to \tt{oldfd} entry 424 | \end{itemize} 425 | 426 | \section{Thread Synchronization} 427 | \begin{itemize}[noitemsep] 428 | \item \tt{sem\_init(sem\_t *sem, 0, int val)} -- initialize lock 429 | \item \tt{sem\_wait(sem\_t *sem)} -- \tt{P(sem)}; block until get lock 430 | \item \tt{sem\_post(sem\_t *sem)} -- \tt{V(sem)}; release lock 431 | \item In producer-consumer solution, producer produces whenever there is space in buffer, and consumer consumes whatever is there as fast as it can 432 | \item First readers-writers problem favors readers 433 | \item Second readers-writers problem favors writers 434 | \item Readers-writers solutions can result in starvation 435 | \item Deadlock -- threads are waiting for condition that will never be true 436 | \end{itemize} 437 | 438 | \section{Network Programming} 439 | \begin{itemize}[noitemsep] 440 | \item \tt{socket(int domain, int type, int protocol)} -- usually \tt{AF\_INET, SOCK\_STREAM, 0}, respectively; create endpoint of connection 441 | \item \tt{connect(int clientfd, struct sockadd *addr, socklen\_t addrlen)} -- establish connection with a server at \tt{addr} (client) 442 | \item \tt{bind(int sockfd, struct sockaddr *adrr, socklen\_t addrlen)} -- associate server socket address with given socket descriptor (server) 443 | \item \tt{listen(int sockfd, int backlog)} -- set \tt{sockfd} to actively listen (server) 444 | \item \tt{accept(int listenfd, struct sockaddr *addr, int *addrlen)} -- block until a connection is made then return file descriptor for connection 445 | \end{itemize} 446 | 447 | \vfill 448 | 449 | Scratch work: 450 | 451 | \vfill 452 | 453 | \begin{center} 454 | Created for 15-213 at Carnegie Mellon University \\ 455 | in Spring 2019 by Jacob Strieb. \\ 456 | \href{mailto:jstrieb@alumni.cmu.edu}{\tt{jstrieb@alumni.cmu.edu}} \\ 457 | \url{https://git.io/JcZ29} 458 | \end{center} 459 | 460 | \end{document} 461 | 462 | --------------------------------------------------------------------------------