15213 Recitation Section CAssembly Review: Machine ModelAssembly FormatMemory Addressing ModeExercise: What do the ASM mean?Procedure Related InstructionsCASMCompiling and Debugging C CodeWhat if compiling without “-g”?Example: func1ASM of func1Slide 12Using GDB to run the programExample 2ASM of func2Slide 16Example 3ASM of func3Slide 19Slide 20Slide 21Example 4ASM of func4ASMCASMC: write C code for ASM15213 Recitation Section C• Assembly Review• C ASM using GDB• ASM CShimin ChenSept. 16, 2002Outline215213 Recitation C Shimin ChenAssembly Review: Machine ModelCPUMemoryAddressesDataInstructionsObject CodeProgram DataStackEIPRegistersConditionCodes315213 Recitation C Shimin ChenAssembly Format•Op Src, Dest–add %eax, %ebx # %ebx += %eax–sub %eax, %ebx # %ebx -= %eax•Op Arg–jmp 0x87654321 # unconditional branch–jge 0x87654321 # branch if >= in signed# comparison415213 Recitation C Shimin ChenMemory Addressing Mode•Generic form:–D(R1, R2, S)–Address: Reg[R1] + Reg[R2]*S + D–e.g. 0x8(%eax, %ebx, 0x4)–the address is %eax + %ebx * 0x4 + 0x8•Special forms: –omit D, R1, R2, or S–(R1), D(R1), (R1, R2), D(R1, R2)515213 Recitation C Shimin ChenExercise: What do the ASM mean?1) sub %ecx, %edx2) cmp %ecx, 0x4jge 0x123456783) mov (%ebx), %eax4) mov (%ebx, %esi, 0x4), %edi5) lea (%ebx, %esi, 0x4), %edi6) xor %ecx, %ecx615213 Recitation C Shimin ChenProcedure Related Instructionsint a_func (int arg1, int arg2, int arg3)•Get arguments:–arg1: mov 8(%ebp),%ecx–arg2: mov 12(%ebp),%ecx–arg3?•Set return value:–mov 0x1, %eax # return 1;mov 16(%ebp),%ecx715213 Recitation C Shimin ChenCASM•Compilation and GDB basics•CASM Examples815213 Recitation C Shimin ChenCompiling and Debugging C Code•Generating ASM with gcc–gcc –O –S -Wall example.c–generate example.s•Debugging C code–gcc –O –g –o example -Wall example.c–gdb example915213 Recitation C Shimin ChenWhat if compiling without “-g”?•gcc –O –o example -Wall example.c •gdb will not know the C code for assembly•the same as in L2 “bomb lab”•use gdb to examine the object code–Other tools (objdump etc.) see L2 description1015213 Recitation C Shimin ChenExample: func1int func1(int a, int b){ int x, y; x = a + b; y = 2*x - b; return x*y;}1115213 Recitation C Shimin ChenASM of func1Dump of assembler code for function func1:0x8048420 <func1>: push %ebp0x8048421 <func1+1>: mov %esp,%ebp0x8048423 <func1+3>: mov 0xc(%ebp),%eax0x8048426 <func1+6>: mov 0x8(%ebp),%ecx0x8048429 <func1+9>: add %eax,%ecx0x804842b <func1+11>: lea (%ecx,%ecx,1),%edx 0x804842e <func1+14>: sub %eax,%edx0x8048430 <func1+16>: mov %ecx,%eax0x8048432 <func1+18>: imul %edx,%eax0x8048435 <func1+21>: mov %ebp,%esp0x8048437 <func1+23>: pop %ebp0x8048438 <func1+24>: ret 0x8048439 <func1+25>: lea 0x0(%esi),%esiEnd of assembler dump.1215213 Recitation C Shimin ChenASM of func1Dump of assembler code for function func1:0x8048420 <func1>: push %ebp0x8048421 <func1+1>: mov %esp,%ebp0x8048423 <func1+3>: mov 0xc(%ebp),%eax #%eax=b0x8048426 <func1+6>: mov 0x8(%ebp),%ecx #%ecx=a0x8048429 <func1+9>: add %eax,%ecx #%ecx=a+b0x804842b <func1+11>: lea (%ecx,%ecx,1),%edx #%edx=2*%ecx0x804842e <func1+14>: sub %eax,%edx #%edx-=b0x8048430 <func1+16>: mov %ecx,%eax #%eax=x0x8048432 <func1+18>: imul %edx,%eax #return x*y0x8048435 <func1+21>: mov %ebp,%esp0x8048437 <func1+23>: pop %ebp0x8048438 <func1+24>: ret 0x8048439 <func1+25>: lea 0x0(%esi),%esiEnd of assembler dump.1315213 Recitation C Shimin ChenUsing GDB to run the program•Let’s use gdb to run the program and examine registers and memory locations•break func1•run•p/x $ebp•x/2wx $ebp+81415213 Recitation C Shimin ChenExample 2int func2(int a, int b){ if(a>b) return a; else return b;}1515213 Recitation C Shimin ChenASM of func2Dump of assembler code for function func2:0x804843c <func2>: push %ebp0x804843d <func2+1>: mov %esp,%ebp0x804843f <func2+3>: mov 0x8(%ebp),%edx0x8048442 <func2+6>: mov 0xc(%ebp),%eax0x8048445 <func2+9>: cmp %eax,%edx0x8048447 <func2+11>: jle 0x804844b <func2+15>0x8048449 <func2+13>: mov %edx,%eax0x804844b <func2+15>: mov %ebp,%esp0x804844d <func2+17>: pop %ebp0x804844e <func2+18>: ret 0x804844f <func2+19>: nop End of assembler dump.1615213 Recitation C Shimin ChenASM of func2Dump of assembler code for function func2:0x804843c <func2>: push %ebp0x804843d <func2+1>: mov %esp,%ebp0x804843f <func2+3>: mov 0x8(%ebp),%edx #%edx=a0x8048442 <func2+6>: mov 0xc(%ebp),%eax #%eax=b0x8048445 <func2+9>: cmp %eax,%edx #%edx<=%eax?0x8048447 <func2+11>: jle 0x804844b <func2+15> # 0x8048449 <func2+13>: mov %edx,%eax #%eax=a0x804844b <func2+15>: mov %ebp,%esp0x804844d <func2+17>: pop %ebp0x804844e <func2+18>: ret 0x804844f <func2+19>: nop End of assembler dump.1715213 Recitation C Shimin ChenExample 3int func3(int a, int b){ int r = 0xDEADBEEF; switch(a) { case 0: case 1: r = b; break; case 2: r = a+b; break; case 3: r = a-b; break; case 4: r = a*b; break; default:; } return r;}1815213 Recitation C Shimin ChenASM of func3Dump of assembler code for function func3:0x8048450 <func3>: push %ebp0x8048451 <func3+1>: mov %esp,%ebp0x8048453 <func3+3>: mov 0x8(%ebp),%edx0x8048456 <func3+6>: mov 0xc(%ebp),%ecx0x8048459 <func3+9>: mov $0xdeadbeef,%eax0x804845e <func3+14>: cmp $0x4,%edx0x8048461 <func3+17>: ja 0x804848b <func3+59>0x8048463 <func3+19>: jmp *0x8048598(,%edx,4)0x804846a <func3+26>: lea 0x0(%esi),%esi0x8048470 <func3+32>: mov %ecx,%eax0x8048472 <func3+34>: jmp 0x804848b <func3+59>0x8048474 <func3+36>: lea (%ecx,%edx,1),%eax0x8048477 <func3+39>: jmp 0x804848b <func3+59>1915213 Recitation C Shimin ChenASM of func30x8048479 <func3+41>: lea 0x0(%esi,1),%esi0x8048480 <func3+48>: mov %edx,%eax0x8048482 <func3+50>: sub %ecx,%eax0x8048484 <func3+52>: jmp 0x804848b <func3+59>0x8048486 <func3+54>: mov %edx,%eax0x8048488 <func3+56>: imul %ecx,%eax0x804848b <func3+59>: mov %ebp,%esp0x804848d <func3+61>: pop %ebp0x804848e <func3+62>: ret (gdb) x/5wx 0x80485980x8048598 <_IO_stdin_used+4>: 0x08048470
View Full Document