machete.tex 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. \documentclass{article}
  2. \usepackage{lmodern}
  3. \usepackage[T1]{fontenc}
  4. \usepackage{geometry}
  5. \usepackage{array}
  6. \newcolumntype{@}{>{\global\let\currentrowstyle\relax}}
  7. \newcolumntype{^}{>{\currentrowstyle}}
  8. \newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  9. #1\ignorespaces
  10. }
  11. % Configuración de parrafos
  12. \setlength{\parskip}{1em}
  13. % Margenes
  14. \geometry{
  15. a4paper,
  16. total={210mm,297mm},
  17. left=15mm,
  18. right=10mm,
  19. top=15mm,
  20. bottom=10mm,
  21. includehead, includefoot
  22. }
  23. \textwidth=6in
  24. \textheight=9.0in
  25. \begin{document}
  26. \section{Saltos}
  27. \textbf {JA} = Above/not below or equal | (CF or ZF) = 0 \\
  28. \textbf {JAE} = Above or equal/not below | CF = 0 \\
  29. \textbf {JE} = Equal/zero | ZF = 1 \\
  30. \textbf {JZ} = Equal/zero | ZF = 1 \\
  31. \textbf {JGE} = Greater or equal/not less | (SF xor OF) = 0 \\
  32. \textbf {JG} = Greater/not less or equal | ((SF xor OF) or ZF) = 0 \\
  33. \textbf {JO} = Overflow | OF = 1 \\
  34. \textbf {JECXZ} = Register ECX is zero | ECX = 0 \\
  35. \section{Registros}
  36. \begin{itemize}
  37. \setlength\itemsep{1em}
  38. \item \textbf{8 bits}: AL, BL, CL, DL, DIL, SIL, BPL, SPL, R8L, R9L, R10L, R11L, R12L, R13L, R14L, R15L \item \textbf{16 bits}: AX, BX, CX, DX, DI, SI, BP, SP, R8W, R9W, R10W, R11W, R12W, R13W, R14W, R15W
  39. \item \textbf{32 bits}: EAX, EBX, ECX, EDX, EDI, ESI, EBP, ESP, R8D, R9D, R10D, R11D, R12D, R13D, R14D, R15D
  40. \item \textbf{64 bits}: RAX, RBX, RCX, RDX, RSI, RDI, RPB, RSP, R8, R9, R10, R11, R12, R13, R14, R15
  41. \item \textbf{128 bits}: XMM0-XMM15
  42. \end{itemize}
  43. \section{Secciones y Etiquetas}
  44. \subsection*{Secciones}
  45. \textbf{.data}: Variables \\
  46. \textbf{.rodata}: Constantes \\
  47. \textbf{.text}: C\'odigo \\
  48. \subsection*{Etiquetas}
  49. \textbf{global}: Define un s\'imbolo que va a ser visto externamente \\
  50. \textbf{\_start}: Punto de entrada de un programa en linux \\
  51. \textbf{extern}: Llamar funci\'on externa
  52. \subsection*{Pseudoinstrucciones}
  53. Prefijo \textbf{TIMES}, para repetir una cantidad de veces la instrucci\'on que sigue
  54. \section*{Instrucciones}
  55. \begin{tabular}[l]{ |@l|>{\itshape}^l|^l| }
  56. \hline
  57. \rowstyle{\bfseries}
  58. Instruccion & Formato & Descripcion \\
  59. \hline
  60. DB, DW, DD, DQ & -- & Definir datos \\
  61. SHR & reg, imm & Hace un shift right l\'ogico (sin signo) \\
  62. SAR & reg, imm & Hace un shift right aritm\'etico (con signo) \\
  63. LEA & reg1, [ reg * imm + imm/reg ] & Evalua lo que hay entre corchetes y lo almacena en reg1 \\
  64. MOVZX & ???? & En las diapos decia que servia para algo \\
  65. CBW & ???? & En las diapos decia que servia para algo \\
  66. CDQE & ???? & En las diapos decia que servia para algo \\
  67. MOV & (tam), mem/imm & Para el caso de inmediato o mem, especificar tam \\
  68. CMP & (tam), mem/imm & Para el caso de inmediato o mem, especificar tam \\
  69. MOVQ & xmm, reg & Muevo de registro a registro XMM \\
  70. CLD & -- & Seteo flag de direcci\'on (forward) \\
  71. REP & instruccion & Repito RCX veces la instrucci\'on siguiente \\
  72. MOVSB & -- & Mueve el contenido de donde apunta RSI y lo almacena a donde apunta RDI, despues aumenta ¿en 1? los dos registros \\
  73. \hline
  74. \end{tabular}
  75. \section{Convenci\'on C}
  76. \begin{itemize}
  77. \setlength\itemsep{1em}
  78. \item RBX, R12, R13, R14 y R15 \\
  79. \item Retornar el resultado RAX o XMM0 \\
  80. \item No romper la pila \\
  81. \item Antes de llamar, alinear la pila a 16 bytes \\
  82. \end{itemize}
  83. \subsection*{Stack Frame}
  84. \textbf{PUSH RBP} \\
  85. \textbf{MOV RBP, RSP} \\
  86. ... \\
  87. \textbf{SUB RSP, 8 }; para alinear, variables locales \\
  88. ... \\
  89. ... \\
  90. \textbf{POP RBP} \\
  91. \textbf{RET} \\
  92. \subsection*{Pasaje de parametros}
  93. \begin{itemize}
  94. \setlength\itemsep{1em}
  95. \item Si es entero o puntero se pasan respetando el orden usando:
  96. \begin{itemize}
  97. \setlength\itemsep{1em}
  98. \item [$*$] RDI, RSI, RDX, RCX, R8 y R9
  99. \end{itemize}
  100. \item Si es de tipo flotante se pasan en los XMMs
  101. \end{itemize}
  102. Si no hay m\'as registros disponibles de usa la pila, pero deber\'an quedar ordenados desde la direcci\'on m\'as baja a la m\'as alta (se pushean de derecha a izquierda)
  103. \subsection*{Vectores}
  104. \textit {"Puntero al inicio del arreglo"} + \textit {"tama\~no del dato"} * \textit {" índice del elemento"}
  105. \subsection*{Matrices}
  106. \textit {"Puntero al inicio de la matriz"}
  107. + \textit {"cantidad elementos de la fila"} * \textit {"indice de la fila"} * \textit {"tama\~no dato"}
  108. + \textit {"indice de columna"} * \textit {"tama\~no dato"}
  109. \subsection*{El formato general para direccionar es}
  110. \begin{center}[ \textit{\textbf{Base}} + \textit{\textbf{Indice}} * \textit{\textbf{scala}} +/- \textit{\textbf{Desplazamiento}} ]\\
  111. \textbf{Base} = alg\'un registro\\
  112. \textbf{Indice} = alg\'un registro\\
  113. \textbf{scala} = 1, 2, 4 u 8\\
  114. \textbf{Desplazamiento} = inmediato de 32 bits\\
  115. \end{center}
  116. \section{Alineaci\'on}
  117. \begin{itemize}
  118. \setlength\itemsep{1em}
  119. \item \textbf{Alineaci\'on en los campos del struct}:
  120. Cada campo esta alineado a su tama\~no dentro del struct
  121. \item \textbf{Alineaci\'on del struct}:
  122. Se alinea al tama\~no del campo mas grande del struct
  123. \item\textbf{\_\_atribute\_\_((packed)}:
  124. Indica que el struct no va a ser alineado
  125. \end{itemize}
  126. ------------------------------------------ \\
  127. \section{SSE}
  128. Para enteros: Comienzan con P, luego el nombre de la \textit {"operaci\'on"} y terminan con el \textit {"tamaño"} del dato
  129. \textit {\textbf {Ejemplo}}
  130. \begin{itemize}
  131. \setlength\itemsep{1em}
  132. \item \textbf {PADDB}: suma de a \textbf{B}yte
  133. \item \textbf {PADDW}: suma de a \textbf{W}ord
  134. \item \textbf {PADDD}: suma de a \textbf{D}oubleword
  135. \item \textbf {PADDQ}: duma de a \textbf{Q}uadword
  136. \end{itemize}
  137. Para punto flotante: nombre de la \textit {"operaci\'on"}, \textit {"modo"} de operaci\'on y \textit {"tama\~no"} del dato \\
  138. \textit {\textbf {Ejemplo}}
  139. \begin{itemize}
  140. \setlength\itemsep{1em}
  141. \item \textbf {ADDPS}: suma de a \textbf{F}loat
  142. \item \textbf {ADDPD}: suma de a \textbf{D}ouble
  143. \end{itemize}
  144. La P proviene de Packed, podria ser S de Scalar
  145. \subsection*{Multiplicar}
  146. \begin{itemize}
  147. \setlength\itemsep{1em}
  148. \item \textbf {PMULLW}: multiplica de a word y se queda con la parte baja
  149. \item \textbf {PMULHW}: multiplica de a word y se queda con la parte alta
  150. \end{itemize}
  151. \subsection*{Saturacion}
  152. \begin{itemize}
  153. \setlength\itemsep{1em}
  154. \item \textbf {PADDUSB, PSUBUSB}: Suma y resta de enteros byte sin signo con saturaci\'on sin signo
  155. \item \textbf {PADDSB, PSUBSB}: Suma y resta de enteros byte con signo con saturaci\'on con signo
  156. \end{itemize}
  157. \subsection*{Empaquetado/Desempaquetado}
  158. \begin{itemize}
  159. \setlength\itemsep{1em}
  160. \item Desempaquetado: PUNPCK \{L,H\} \{BW,WD,DQ,QDQ\}
  161. \item Empaquetado: PACK{SS,US}{WB,DW}\\
  162. SS, US: \textbf{S}igned/\textbf{u}nsigned, con \textbf{s}aturación
  163. \end{itemize}
  164. \textit{\textbf{Ejemplo}}\\
  165. Estos de aca abajo quiero poner con item, pero si no se puede bueno \\
  166. \textbf{PXOR} XMM7, XMM7 (este fijate si terminan los dos en 7 que por ahi me confundi por si las moscas) \\
  167. \textbf{MOVDQU} XMM1, XMM0 \\
  168. \textbf{PUNPCKLBW} XMM0, XMM7 \\
  169. \textbf{PUNPCKHBW} XMM1, XMM7 \\
  170. \subsection*{Desempaquetar}
  171. \begin{itemize}
  172. \setlength\itemsep{1em}
  173. \item \textbf{PUNPCKLWD}: usa la parte baja de los registros para juntarlos
  174. \item \textbf{PUNPCKHWD}: usa la parte alta de los registros para juntarlos
  175. \end{itemize}
  176. \subsection*{Shuffles}
  177. Las intrucciones de Shuffles permiten reordenar datos en registros. Sus par\'ametros serán el registro a reordenar y una mascara que indicar\'a como hacerlo
  178. \begin{itemize}
  179. \setlength\itemsep{1em}
  180. \item \textbf{PSHUF\{B,W,D\}}: Shuffle Packed Bytes, Words, Doublewords
  181. \item \textbf{PSHUF\{H,L\}W}: Shuffle High, low 16bit values
  182. \item \textbf{SHUFP\{S,D\}}: Shuffle Packed single, Double FP Values
  183. \end{itemize}
  184. \textbf{PSHUFHW} xmm1, xmm2/m128, imm8
  185. \subsection*{Insert/Extract}
  186. \begin{itemize}
  187. \setlength\itemsep{1em}
  188. \item \textbf{INSERTPS}: Insert Packed Single FP Value
  189. \item \textbf{EXTRACTPS}: Extract Packed Single FP Value
  190. \item \textbf{PINSR\{B,W,D,Q\}}: Insert Byte, Word, Dword, Qword
  191. \item \textbf{PEXTR\{B,W,D,Q\}}: Extract Byte, Word, Dword, Qword
  192. \end{itemize}
  193. \textbf{INSERTPS} xmm1, xmm2/m32, imm8 \\
  194. \textbf{EXTRACTPS} reg/m32, xmm2, imm8 \\
  195. \textbf{PINSRD} xmm1, r/m32, imm8 \\
  196. \textbf{PEXTRD} r/m32, xmm2, imm8 \\
  197. \subsection*{Conversiones}
  198. Las intrucciones de conversi\'on son de la forma: CTV\textbf{XX}2\textbf{YY} \\
  199. \textit{Donde \textbf{XX} e \textbf{YY} pueden valer:} \\
  200. \begin{tabular}[l]{ |c|c|c| }
  201. PS-Packed Single FP & PD-Packed Double FP & PI-Packed Integer \\
  202. PS-Packed Single FP & PD-Packed Double FP & PI-Packed Integer \\
  203. & & DQ-Packed Dword \\
  204. \end{tabular}
  205. \section{Instruciones}
  206. \begin{tabular}[l]{ |@l|>{\itshape}^l|^l| }
  207. \rowstyle{\bfseries}
  208. Instruccion & Formato & Descripcion \\
  209. \hline
  210. \multicolumn{3}{ |c| }{Conversiones} \\
  211. \hline
  212. CVTSD2SI & r64/r32, xmm/m64 & Scalar Double FP to Dword Integer \\
  213. CVTSI2SD & xmm, r/m32/m64 & Dword Integer to Scalar Double FP \\
  214. CVTDQ2PS & xmm1, xmm2/m128 & Packed Dword Integers to Packed Single FP (4X)\\
  215. CVTPS2DQ & xmm1, xmm2/m128 & Packed Single FP to Packed Dword Integers (4X)\\
  216. CVTDQ2PD & xmm1, xmm2/m64 & Packed Dword Integers to Packed Double FP (2X)\\
  217. CVTPD2DQ & xmm1, xmm2/m128 & Packed Double FP to Packed Dword Integers (2X)\\
  218. CVTPD2PS & xmm1, xmm2/m128 & Packed Double FP to Packed Single FP (2X)\\
  219. CVTPS2PD & xmm1, xmm2/m64 & Packed Single FP to Packed Double FP (2X)\\
  220. CVTSD2SS & xmm1, xmm2/m64 & Scalar Double FP to Scalar Single FP (1X)\\
  221. CVTSS2SD & xmm1, xmm2/m32 & Scalar Single FP to Scalar Double FP (1X)\\
  222. CVTTPS2DQ & xmm1, xmm2/m128 & Truncation Packed Single FP to Packed Dword Int. (4X)\\
  223. CVTTSS2SI & r32, xmm/m32 & Truncation Scalar Single FP to Dword Integer (1X)\\
  224. CVTTSD2SI & r32, xmm/m64 & Truncation Scalar Double FP to Signed Integer (1X)\\
  225. CVTSI2SS & xmm, r/m32 & Dword Integer to Scalar Single FP\\
  226. CVTSS2SI & r32, xmm/m32 & Scalar Single FP to Dword Integer\\
  227. CVTPI2PS & xmm, mm/m64 & Packed Dword Integers to Packed Single FP\\
  228. CVTPI2PD & xmm, mm/m64 & Packed Dword Integers to Packed Double FP\\
  229. \hline
  230. \multicolumn{3}{ |c| }{Round} \\
  231. \hline
  232. ROUNDPS & xmm1, xmm2/m128, imm8 & Round Packed Single FP to Integer\\
  233. ROUNDSS & xmm1, xmm2/m32, imm8 & Round Scalar Single FP to Integer\\
  234. ROUNDPD & xmm1, xmm2/m128, imm8 & Round Packed Double FP to Integer\\
  235. ROUNDSD & xmm1, xmm2/m64, imm8 & Round Scalar Double FP to Integer\\
  236. \hline
  237. \multicolumn{3}{ |c| }{Otros} \\
  238. \hline
  239. PCMPGTW & xmm, xmm & Compara word a word y si cumple la condici\'on setea unos en el resultado, sino ceros \\
  240. \end{tabular}
  241. \end{document}