Lecture Notes in Assembly Language
Total Page:16
File Type:pdf, Size:1020Kb
Lecture Notes in Assembly Language Short introduction to low-level programming Piotr Fulmański Łódź, 12 czerwca 2015 Spis treści Spis treści iii 1 Before we begin1 1.1 Simple assembler.................................... 1 1.1.1 Excercise 1 ................................... 2 1.1.2 Excercise 2 ................................... 3 1.1.3 Excercise 3 ................................... 3 1.1.4 Excercise 4 ................................... 5 1.1.5 Excercise 5 ................................... 6 1.2 Improvements, part I: addressing........................... 8 1.2.1 Excercise 6 ................................... 11 1.3 Improvements, part II: indirect addressing...................... 11 1.4 Improvements, part III: labels............................. 18 1.4.1 Excercise 7: find substring in a string .................... 19 1.4.2 Excercise 8: improved polynomial....................... 21 1.5 Improvements, part IV: flag register ......................... 23 1.6 Improvements, part V: the stack ........................... 24 1.6.1 Excercise 12................................... 26 1.7 Improvements, part VI – function stack frame.................... 29 1.8 Finall excercises..................................... 34 1.8.1 Excercise 13................................... 34 1.8.2 Excercise 14................................... 34 1.8.3 Excercise 15................................... 34 1.8.4 Excercise 16................................... 34 iii iv SPIS TREŚCI 1.8.5 Excercise 17................................... 34 2 First program 37 2.1 Compiling, linking. ................................... 37 2.1.1 Compiler and compiling............................ 37 2.1.2 Linker and linking ............................... 38 2.1.3 Summary.................................... 38 2.2 32-bit basic stand alone program........................... 39 2.2.1 Code for NASM ................................ 39 2.2.2 Code for GNU AS ............................... 48 2.2.3 AT&T vs. Intel assembly syntax ....................... 51 2.3 64-bit basic stand alone program........................... 53 2.3.1 Code for NASM ................................ 53 2.3.2 Code for GNU AS ............................... 55 2.3.3 Excercise 1 ................................... 55 2.4 Multiple files ...................................... 56 3 NASM syntax 59 3.1 Layout of a NASM source line............................. 59 3.2 Pseudo-instructions................................... 60 3.2.1 Declaring initialized data ........................... 60 3.2.2 Declaring uninitialized data.......................... 60 3.2.3 Including external binary files......................... 61 3.2.4 Defining constants............................... 61 3.2.5 Repeating instructions or data ........................ 61 3.3 Effective addresses................................... 61 3.4 Constants........................................ 62 3.4.1 Numeric constants............................... 62 3.4.2 String constants ................................ 63 3.4.3 Floating-point constants............................ 63 3.4.4 Packed BCD constants............................. 64 4 Basic CPU instructions 65 SPIS TREŚCI v 4.1 Utility instructions................................... 65 4.1.1 cbw ....................................... 65 4.1.2 cwd ....................................... 65 4.2 Arithmetic instructions ................................ 65 4.2.1 div........................................ 65 4.2.2 Excercise 1 ................................... 67 4.2.3 add ....................................... 77 4.2.4 sub........................................ 77 4.2.5 mul ....................................... 77 4.2.6 idiv ....................................... 77 4.2.7 imul....................................... 77 4.2.8 cmp ....................................... 77 4.2.9 inc........................................ 77 4.2.10 dec........................................ 77 4.3 Logic instructions.................................... 77 4.3.1 and ....................................... 77 4.3.2 or ........................................ 77 4.3.3 not........................................ 77 4.3.4 xor........................................ 77 4.3.5 shl........................................ 77 4.3.6 shr........................................ 77 4.3.7 test ....................................... 77 4.4 Jump instructions ................................... 78 4.4.1 jmp ....................................... 78 4.4.2 call........................................ 78 4.4.3 JZ........................................ 78 4.4.4 JE........................................ 79 4.4.5 JNZ....................................... 79 4.4.6 JNE....................................... 79 4.4.7 JA........................................ 79 4.4.8 JNA....................................... 79 4.4.9 JB........................................ 79 vi SPIS TREŚCI 4.4.10 JNB....................................... 79 4.4.11 CMP, TEST and JE.............................. 79 4.4.12 LOOP...................................... 80 4.4.13 Jump examples................................. 80 4.5 Transfer instructions.................................. 88 4.5.1 mov ....................................... 88 4.5.2 call........................................ 88 4.5.3 push....................................... 88 4.5.4 pop ....................................... 88 4.5.5 pusha ...................................... 88 4.5.6 popa....................................... 88 4.5.7 xchg....................................... 88 5 Debugging with GDB 89 6 First program linked with a C library 91 6.1 32-bit basic program linked with a C library..................... 91 6.1.1 Code for NASM ................................ 91 6.1.2 GCC 32-bit calling conventions in brief ................... 92 6.1.3 Excercise .................................... 93 6.2 64-bit basic program linked with a C library..................... 96 6.2.1 Code for NASM ................................ 96 6.2.2 GCC 64-bit calling conventions in brief ................... 97 6.2.3 Excercise 2 ................................... 98 6.3 Excercises........................................ 104 6.3.1 Excercise .................................... 104 6.3.2 Excercise .................................... 108 6.3.3 Excercise .................................... 110 7 FPU 113 7.1 FPU internals...................................... 114 7.1.1 FPU Data Registers.............................. 114 7.1.2 FPU Addressing Modes ............................ 116 SPIS TREŚCI vii 7.1.3 FPU stack usage example........................... 116 7.2 FPU Status Register.................................. 117 7.2.1 Exception Flags ................................117 7.3 FPU Control Register ................................. 119 7.4 FPU Tag Word Register................................ 119 7.5 Examples ........................................ 119 7.5.1 Instructions related to the FPU internals ..................119 7.5.2 FPU control word usage............................ 123 7.5.3 FPU status word usage ............................124 7.5.4 FPU stack overflow............................... 126 7.6 Excercises........................................ 131 7.6.1 Excercise .................................... 131 8 File operations 135 8.1 File operations with Linux system calls........................ 135 8.1.1 Excercise .................................... 140 8.2 File operations with C functions ...........................140 8.3 Command Line Parameters .............................. 140 8.4 Auxiliary code ..................................... 143 8.5 Records ......................................... 145 8.6 Excercises........................................ 145 8.6.1 Excercise .................................... 145 9 MMX 147 9.1 Introduction....................................... 147 9.2 Single Instruction, Multiple Data (SIMD) technique . 148 9.3 Eight 64-bit wide MMX registers........................... 149 9.4 New data types..................................... 150 9.5 New instructions .................................... 151 9.5.1 Add packed integers with PADDW...................... 151 9.5.2 Multiply and Add Packed Integers with PMADDWD . 152 9.5.3 Compare packed signed integers for greater than with PCMPGTW . 152 9.5.4 Pack with signed saturation with PACKSSWB . 152 viii SPIS TREŚCI 9.6 Examples ........................................ 153 9.7 Excercise ........................................ 158 9.7.1 Solution..................................... 158 10 SSE 165 10.1 Streaming SIMD Extensions.............................. 165 10.2 Example......................................... 165 10.3 Excercise ........................................ 167 11 RDTS – measure what is unmeasurable 175 11.1 Read time-stamp counter ............................... 175 11.2 Usage of the RDTS................................... 175 11.2.1 Usage example ................................. 177 11.2.2 Excercise .................................... 185 12 Inline assembler 187 12.1 First fundamental problem ..............................188 12.2 Second fundamental problem ............................. 189 12.2.1 Global variables ................................189 12.2.2 Local variables ................................. 190 12.3 Third fundamental problem.............................. 198 13 Introduction 199 13.1 Assembly language................................... 199 13.2 Pre-x86 age – historical background .........................201 13.2.1 Intel 4004...................................