cd cs31/weeklylab pwd mkdir week04 cd week04 pwd cp ~lammert/public/cs31/week04/* . ls Makefile conditions.c simpleops.c while-loop-asm.s while-loop.c
Open up simpleops.c in a vim or emacs.
We are going to look at how to use gcc to create an assembly version of this file, and how to create a object .o file, and how to examine its contents. We are also going to look at how to used gdb to see the assembly code and to step through the execution of individual instructions.
If you open up the Makefile you can see the rules for building .s, .o and executable files from simpleops.c. We will be compiling the 32-bit version of instructions, so we will use the -m32 flag, and we are using version 4.4 of gcc (version 4.4 generates easier to read IA32 code):
gcc-4.4 -m32 -S simpleops.c # just runs the compiler to create a .s text file gcc-4.4 -m32 -c simpleops.s # compiles to a relocatable object binary file (.o) gcc-4.4 -m32 -o simpleops simpleops.o # creates a 32-bit executable fileTo see the machine code and assembly code mappings in the .o file:
objdump -d simpleops.oYou can compare this to the assembly file:
gdb simpleops (gdb) break main (gdb) runIn gdb you can disassemble code using the disass command:
(gdb) disass mainYou can set a break point at a specific instruction:
(gdb) break *0x080483c1 # set breakpoint at specified address (gdb) cont (gdb) disassAnd you can step or next at the instruction level using ni or si (si steps into function calls, ni skips over them):
(gdb) ni # execute the next instruction then gdb gets control again (gdb) ni (gdb) ni (gdb) ni (gdb) ni (gdb) disassYou can print out the values of individual registers like this:
(gdb) print $eaxYou can also view all register values:
(gdb) info registersYou can also use the display command to automatically display values each time a breakpoint is reached:
(gdb) display $eax (gdb) display $edxSee my GDB Guide for more information about using gdb. (see the "using gdb to debug assembly code and examine memory and register values" section).
Also see figure 3.30 on p.255 of the textbook.
Recall the CPU condition codes we talked about in class:
ZF: If the computed result is zero. SF: If the computed result's most significant bit is set (i.e., it's negative if interpreted as signed). CF: If the computed result caused an overflow, when interpreted as an unsigned operation. OF: If the computed result caused an overflow, when interpreted as a signed operation.
Assume %eax holds the value 5, and %ecx holds 7. Suppose we executed the following instructions:
For each of these instructions, which flags should be set?
Now that think we know which flags would be set, let's see if we're correct. Open conditions.c in your text editor. We'll use this simple C file to generate the above instructions (or, at least some very similar ones).
Run make, and let's open gdb:
Let's put a breakpoint at main:
From here, we can step through individual instructions (via the ni command). Let's stop when we get to the second sub instruction (sub $0x5, %eax). We can see that at this point, %eax holds the value 5, as expected. Now, execute the instruction with ni. Are the condition codes (in the eflags register) what you expected them to be?
Now do the same with the cmp instruction that comes a few instructions after the sub we just examined.
The condition codes for your ALU should work the same way!
Next, we'll try writing a while loop in IA32 assembly. Take a look at the while-loop.c file. You'll see that it creates an array with five buckets. It sets three of them to 5, 10, and 2, and it prompts the user to fill in the other two. It then calls a sum_function. Right now, the sum function, which should return the sum of all five buckets in the array, is empty! We need to fill it in, only rather than doing it in C, let's write it IA32 assembly.
Open the file while-loop-asm.s in a text editor. There's lots of stuff in here that was generated by gcc that isn't easily human-readable, but I've added comments in the section that you need to edit. At the beginning, the base of the array is stored in the %ecx register. You want to put your result in %eax when you're done. Right now, the constant 10 is being put into that register, which is no good. Work with your partner to write a loop that sums up the five array values.
At any time, you can run make and then execute while-loop-asm to test your implementation. Don't be surprised if you see some crazy values the first few times you execute it.
After we've written a correct while loop in assembly, let's edit while-loop.c with an equivalent C implementation and use gcc -S to see what the compiler generated:
gcc-4.4 -m32 -S while-loop.c gcc-4.4 -m32 -Wall -g -o while-loop while-loop.c emacs while-loop.s &