Feb 19
OK, it's time to learn x86 Assembly - chapter 3 of Systems textbook.
Discuss assigned reading and problems 3.1 through 3.5 :
- registers
- addressing modes
- various MOV instructions
- PUSH and POP
Then work through some real assembler code
- based on code & explanation from lecture notes in CMU's 04-machine-basics.pptx
- C code, gcc compile, and sample gdb session below .
Questions :
- What exactly are the "mov" instructions in the "diass swap" doing?
- Where does the code for each function (swap, main, doit) live?
- Where is the "stack"? And what are %esp and %ebp ? (In gdb $esp and $ebp )
/* swap.c ... based on CMU system's course lecture 4
*
* $ gcc -O1 -m32 swap.c -o swap
*
* $ gdb swap
* (gdb) start
* (gdb) disass swap
* Dump of assembler code for function swap:
* 0x08048394 <+0>: push %ebp
* 0x08048395 <+1>: mov %esp,%ebp
* 0x08048397 <+3>: push %ebx
* 0x08048398 <+4>: mov 0x8(%ebp),%edx
* 0x0804839b <+7>: mov 0xc(%ebp),%eax
* 0x0804839e <+10>: mov (%edx),%ecx
* 0x080483a0 <+12>: mov (%eax),%ebx
* 0x080483a2 <+14>: mov %ebx,(%edx)
* 0x080483a4 <+16>: mov %ecx,(%eax)
* 0x080483a6 <+18>: pop %ebx
* 0x080483a7 <+19>: pop %ebp
* 0x080483a8 <+20>: ret
* (gdb) break *(swap +4)
* (gdb) continue
* (gdb) info stack
* #0 0x08048398 in swap ()
* #1 0x080483cf in doit ()
* #2 0x080483df in main ()
* (gdb) info frame
* Stack level 0, frame at 0xbffff608:
* eip = 0x8048398 in swap; saved eip 0x80483cf
* called by frame at 0xbffff628
* Arglist at 0xbffff600, args:
* Locals at 0xbffff600, Previous frame's sp is 0xbffff608
* Saved registers:
* ebx at 0xbffff5fc, ebp at 0xbffff600, eip at 0xbffff604
* (gdb) print $eip
* $1 = (void (*)()) 0x8048398 <swap+4>
* (gdb) x/32x $esp
* 0xbffff5fc: 0xb7fceff4 0xbffff620 0x080483cf 0xbffff61c
* 0xbffff60c: 0xbffff618 0xb7e9ad35 0xb7ff0a70 0x000001c8
* 0xbffff61c: 0x0000007b 0xbffff628 0x080483df 0xbffff6a8
* 0xbffff62c: 0xb7e81e37 0x00000001 0xbffff6d4 0xbffff6dc
* 0xbffff63c: 0xf57fe414 0xffffffff 0xb7ffeff4 0x08048215
* 0xbffff64c: 0x00000001 0xbffff690 0xb7fefa51 0xb7fffad0
* 0xbffff65c: 0xb7fe09d0 0x00000001 0xb7fceff4 0x00000000
* 0xbffff66c: 0x00000000 0xbffff6a8 0x58a166bb 0x7776feab
* (gdb) x/32d $esp
* 0xbffff5fc: -1208160268 -1073744352 134513615 -1073744356
* 0xbffff60c: -1073744360 -1209422539 -1208022416 456
* 0xbffff61c: 123 -1073744344 134513631 -1073744216
* 0xbffff62c: -1209524681 1 -1073744172 -1073744164
* 0xbffff63c: -176167916 -1 -1207963660 134513173
* 0xbffff64c: 1 -1073744240 -1208026543 -1207960880
* 0xbffff65c: -1208088112 1 -1208160268 0
* 0xbffff66c: 0 -1073744216 1486972603 2004287147
* (gdb) quit
*/
void swap(int *xp, int *yp){
int t0 = *xp;
int t1 = *yp;
*xp = t1;
*yp = t0;
}
int doit(){
int x = 123;
int y = 456;
swap(&x, &y);
return x + y;
}
int main(){
int z = doit();
return 0;
}
- Explain why 0x08048215 shows up in the "x/32x $esp" output.
- Explain why 456 and 123 show up in the "x/32d $esp" output ... even though this memory is far away from main, swap, or doit's address.
As time allows, continue with material from that chapter :
- unary and binary operations : add, imul, xor, ...
- program control : flags, cmp, jmp, jl, ...
- procedure calls and the stack (push, pop, frame, call, ...)
midterm in about two weeks
Bomblab ...