demo - readme =============== demo.c is a demonstration C program to explore machine level code including a recursive function and a binary data structure on the heap. See ./Makefile for the rules to compile everything with just $ make compiling and running ----------------------- Running it looks like this : $ gcc -Og demo.c -o demo // optimize for debugging $ ./demo demo.c !! -- part 0 -- What is your favorite color? blue You said 'blue'. -- part1 -- before: n1 = 12, n2 = 34, n3 = 5, n4 = 32767 after: n1 = 34, n2 = 12, n3 = 5, n4 = 483 -- part2 -- fibbo(5) is 8 -- part3 -- (a (b) (c (e) (f))) ... are we having fun yet? strings ------- We can use the "strings" utility to find all the strings in the binary, which may be helpful for learning what the input and output formats are, among other things. $ strings c_demo > c_demo_strings.txt objdump ------- Another useful utility is objdump, which can dissassemble the executable into machine code. $ objdump -d c_demo > c_demo_objdump_d.txt Here's part of that file, the disassmbled swap routine. 0000000000000916 : 916: 8b 07 mov (%rdi),%eax 918: 8b 16 mov (%rsi),%edx 91a: 89 17 mov %edx,(%rdi) 91c: 89 06 mov %eax,(%rsi) 91e: c3 retq address machine_code assembly_language gdb --- But the most useful tool is gdb (the gnu debugger), which lets you examine and interact with the running program. TO DO : * Use gdb to map out where in memory the different parts of the program are when it runs : - the code (main, swap, ...) - the stack (the local storage space for running functions) - the heap (dynamically allocated memory, malloc stuff) * Trace the execution of the recursive function calls, seeing how multiple instances of local variables for the same function can be resident in memory (in the stack). * Understand what the different registers are for. * Understand the different machine codes and their behavior - address modes - data movement between memory & registers - jumps and conditional jumps - logic and arithmetic operations