반응형

참조 : http://msdn.microsoft.com/ko-kr/library/9yky46tz.aspx


[source]

// crt_fflush.c #include <stdio.h> int main( void ) { int integer; char string[81]; // Read each word as a string. printf( "Enter a sentence of four words with scanf: " ); for( integer = 0; integer < 4; integer++ ) { scanf( "%s", string, sizeof(string) ); printf( "%s\n", string ); } // You must flush the input buffer before using gets. // fflush on input stream is an extension to the C standard fflush( stdin ); printf( "Enter the same sentence with gets: " ); gets( string); printf( "%s\n", string ); }


//MS에서 제공한 test source를 사용햇는데 conio 헤더가 레뎃6.2버전에서는 

존재하지 않아서 약간수정하고 gets_s와 scanf_s를 각각 gets 와 scanf로 

수정해준후 진행하였다


[디버깅]

(gdb) disas main Dump of assembler code for function main: 0x8048490 <main>: push %ebp 0x8048491 <main+1>: mov %esp,%ebp // SFP 

// | SFP | RET    ↑esp,ebp

0x8048493 <main+3>: sub $0x58,%esp //

// | stack [88] | SFP | RET

↑esp,ebp

스택은 보통 4의배수로 할당받기 때문에 이렇게 85byte 가아닌 88byte를

할당된다

0x8048496 <main+6>: push $0x80485a0 //printf()'s string 0x804849b <main+11>: call 0x80483c8 <printf>// printf() call 0x80484a0 <main+16>: add $0x4,%esp // calling convention 0x80484a3 <main+19>: movl $0x0,0xfffffffc(%ebp)//ebp -4에 0 을

| stack [81]|integer= 0 | SFP | RET 

↑ebp,esp

0x80484aa <main+26>: lea 0x0(%esi),%esi// NOP 0x80484b0 <main+32>: cmpl $0x3,0xfffffffc(%ebp)//ebp -4 부분과

// | stack [81]| integer=0 | SFP | RET 

↑ebp,esp

0x80484b4 <main+36>: jle 0x80484b8 <main+40>// 같거나 작으면 

//main+40으로 jmp 0x80484b6 <main+38>: jmp 0x80484e1 <main+81>//크면 main+81로

// jmp 0x80484b8 <main+40>: push $0x51 //81바이트 push 0x80484ba <main+42>: lea 0xffffffa8(%ebp),%eax//ebp-88을eax로

0x80484bd <main+45>: push %eax // eax에 인자를 집어넣고 0x80484be <main+46>: push $0x80485cc // 형식포멧 집어넣어주고 0x80484c3 <main+51>: call 0x8048398 <scanf> // scanf() call 0x80484c8 <main+56>: add $0xc,%esp//calling conevntion 0x80484cb <main+59>: lea 0xffffffa8(%ebp),%eax // input값 0x80484ce <main+62>: push %eax // push 0x80484cf <main+63>: push $0x80485cf //string 0x80484d4 <main+68>: call 0x80483c8 <printf> // printf() call 0x80484d9 <main+73>: add $0x8,%esp // calling convention ---Type <return> to continue, or q <return> to quit--- 0x80484dc <main+76>: incl 0xfffffffc(%ebp)//long형으로

//???? 0x80484df <main+79>: jmp 0x80484b0 <main+32>// for()로 jmp 0x80484e1 <main+81>: mov 0x80496f0,%eax// &stdin 0x80484e6 <main+86>: push %eax // push 0x80484e7 <main+87>: call 0x8048378 <fflush>//fflush() call 0x80484ec <main+92>: add $0x4,%esp//calling convention 0x80484ef <main+95>: push $0x80485e0 //printf()'s string 0x80484f4 <main+100>: call 0x80483c8 <printf>// printf() call 0x80484f9 <main+105>: add $0x4,%esp//calling convention 0x80484fc <main+108>: lea 0xffffffa8(%ebp),%eax //string을 입력받음 0x80484ff <main+111>: push %eax //push 0x8048500 <main+112>: call 0x8048388 <gets> //gets() call 0x8048505 <main+117>: add $0x4,%esp // calling convention 0x8048508 <main+120>: lea 0xffffffa8(%ebp),%eax//string 0x804850b <main+123>: push %eax// push 0x804850c <main+124>: push $0x80485cf //printf()'s string 0x8048511 <main+129>: call 0x80483c8 <printf>// printf() call 0x8048516 <main+134>: add $0x8,%esp //calling convention 0x8048519 <main+137>: leave //에필로그 0x804851a <main+138>: ret


반응형

'과거의 컴퓨터 공부 > C 디버깅' 카테고리의 다른 글

exec() v.s fork ()  (0) 2014.09.22
gets() v.s scanf()  (0) 2014.09.20
if else  (0) 2014.09.19
for()  (0) 2014.09.19
main함수 옆에 파라메터 넣어보기  (0) 2014.09.18
,