/* * Need to reference specific instructions? * http://felixcloutier.com/x86/ * * Can't remember how a specific calling convention works? * https://en.wikipedia.org/wiki/X86_calling_conventions#List_of_x86_calling_conventions */ /* * Note that, by default, godbolt compiles for x86-64 with gcc, so you'll be seeing * the System V AMD64 ABI. */ /* * blank_example - The function prolog and epilog * * Things to note: * 1. How do these instructions relate to the stack frame shown in the slides? * 2. Which instruction does the compiler place between the two, and what does it do? * */ void blank_example() { } /* * return_example - The `return` statement * * Things to note: * 1. Where is the return value stored? * */ int return_example() { return 1; } /* * return_param - Using parameters (callee perspective) * * Things to note: * 1. How is the parameter accessed? * */ int return_param(int param) { return param; } /* * add_example - Adding integers * * Things to note: * 1. What instruction is used? * 2. Which operand is the source, and which is the destination? * */ int add_example(int arg1, int arg2) { return arg1 + arg2; } /* * call_function - Calling functions and using their return values (caller perspective) * * Things to note: * 1. How does this function pass an argument to the one it calls? * 2. How does this function access the return value? * */ int call_function(int param) { return return_param(param + 5); } /* * local_vars - Local variables on the stack * * Things to note: * 1. Where does this function store its local variables? * 2. How does it access them again? * 3. Has the storage location of the parameter changed? * 4. What does the answer to #3 tell you about the consistency of stack frame layouts? * */ int local_vars(int param) { int copy = param; return copy; } /* * global_array_value - Getting values from global arrays and strings * * Things to note: * 1. How is the reference to the global array loaded? * 2. How does the compiler retrieve a specific index? * */ int global_array_value_thing[] = {0x5, 0x6, 0x7, 0x8, 0x9}; int global_array_value(long index) { return global_array_value_thing[index]; } /* * global_array_ref - Getting pointers to global arrays and strings * * Things to note: * 1. How is the reference to the global array loaded? * 2. How does the compiler retrieve a specific index reference? * */ int global_array_ref_thing[] = {0x5, 0x6, 0x7, 0x8, 0x9}; int *global_array_ref(long index) { return &global_array_ref_thing[index]; } /* * if_example - What do if statements look like? * * Things to note: * 1. Which instruction is used to check the condition? * 2. How does this instruction perform its comparison? * 3. Which instructions are used to perform the jumps? * 4. What's the difference between the two? * Tip: jump instructions start with a "j" * */ int if_example(int condition) { if (condition > 5) { return 1; } else { return 2; } } /* * for_example - What do for loops look like? * * Things to note: * 1. How are the four components (initializer, condition, end-action, body) arranged? * 2. How can you determine which local variable is which? * */ int for_example(int limit) { int counter = 0; for (int i = 0; i < limit; i++) { counter += 2; } return counter; } /* * sub_example - Subtracting integers * * Things to note: * 1. Which instruction is used? * 2. Which operand is the source, and which is the destination? * 3. Which is the subtrahend and which is the minuend? * */ int sub_example(int arg1, int arg2) { return arg1 - arg2; } /* * xor_example - Xoring integers * * Things to note: * 1. Which instruction is used? * 2. What are the mathematical properties of the exclusive-or operation? * (see https://en.wikipedia.org/wiki/Exclusive_or#Properties ) * 3. How can these properties be applied to create a simple encryrption scheme? * */ int xor_example(int data, int key) { return data ^ key; } /* * short_example - Smaller datatypes * * Things to note: * 1. How is the 32-bit parameter converted to a 16-bit value? * 2. How can register sub-parts be accessed? * 3. Which special instruction assists in loading smaller values into larger registers? * 4. What exactly does that instruction do, and are there other varients of it? * */ short short_example(int param) { short alt = param; return alt + 5; }