Assembly language experiments
This is an in-class lab. Nothing to hand in.
Setup and experimentation
- Go to the Compiler Explorer
- Set it up like this:
- For each experiment in this lab:
- write a little C code in Compiler Explorer
- see what the corresponding assembly language output looks like
- walk through the assembly language code by hand on paper, making suitable adjustments to the registers and EFLAGS bits as you go; get clarity about what every single instruction is doing
- is the assembly code bizarrely short and baffling? what do you get if you change "-Og" to "-O0"?
- write down questions and instructions/notations that confuse you; let's get those questions answered for you!
Experiment 1
Write a function that computes the maximum value of two int parameters
Experiment 2
Write a function that contains a loop. For example, write the function
// Returns the sum 1 + 2 + 3 + ... + n
int triangular_number(int n)
(The nth triangular number is also n * (n - 1) / 2, which is faster to
compute, but write the loop anyway so you can see how x86_64 does loops.)
Experiment 3
Write a function that calls a second function. For example, you could take your maximum-value function from experiment 1 and call it twice to implement max(int a, int b, int c).
Experiment 4
Go wild. Grab one of your recent assignments (bits.c, queue.c, etc.) and stick it into Compiler Explorer. Can you make sense of the result?