1 / 24

중간고사 feedback

중간고사 feedback. 2011. 10. 25 최 윤 정. Point. Binary / Hexadecimal / Decimal Grammar in C language - %d %f %lf %u %x %p … - *, & Floating Point Format Stack Structure. Page 3 : Chap.2 & C 의 기본문법 ( 1/4). 1) 0x502c + 64 = 0x502c + 0x40 = 0x506c, %x 출력형태는 506c

raya-waters
Download Presentation

중간고사 feedback

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 중간고사 feedback 2011. 10. 25최윤 정

  2. Point • Binary / Hexadecimal / Decimal • Grammar in C language - %d %f %lf %u %x %p …- *, & • Floating Point Format • Stack Structure

  3. Page 3 : Chap.2 & C의 기본문법(1/4) 1) 0x502c + 64 = 0x502c + 0x40 = 0x506c, %x 출력형태는 506c 2) 0x50da + 0x502c = 0xa106 , %x 출력형태는 a106 3) x = 0xFFFFFF0, x<<3 ? • X = 00001111……11110000 , X<<3 = 00001111……….111 1000 0000, ∴ 7FFFFFF80 4) unsigned int x = 0xFFFFFFF0, x>>2 ? • Arithmetic shift vs. Logical shift • X = 1111 ………. 0000, X >> 2 = 001111……….1111 0000,∴3FFFFFFC

  4. Page 3 : Chap.2 & C의 기본문법(2/4) 5) int x = 0x15213F10 >> 4; //즉 0x015213F1 char y = (char) x // y = 0xF1 unsigned char z = (unsigned char) x; z = 0xF1 • F1 = 11110001, • signed = -x7(27) + (x6(26) + … x0(20)) = -128 +(64+32+16+1)= -15 • unsigned = x7(27) + x6(26) + … x0(20) = 128+64+32+16+1 = 241 6) float x = 31/8 ; float y = 31/8.0; • Arithmetic : 31/8 = 3.875, but, integer31 / interger8 = 3, (∵ 11111 >> 3) • (float) 31/ 8.0 = 3.875

  5. Page 3 : Chap.2 & C의 기본문법(3/4) 7) n-bit signed integer의 범위 : -2n-1 ~ 0 ~ 2n-1 - 1 n-bit unsigned integer의 범위 : 0 ~ 2n – 1 • Two’s complement에서, 즉, signed integer로 -1과 1을 표현하는 필요한 bit size • -1 : -21-0 n = 1, 1bit • 1 : 2-1 = 22-1 - 1 = 1, 2bit 8) 4 page에서 9) 15213U > -1 • 15213U >(unsigned) -1 ,15213U > FFFFFFFF ? false ∴ 0. 10) INT_MIN == TMIN • 1000 …. 0000, 32bit에서, 800000000 • -INT_MIN = INT_MAX + 1  INT_MIN 즉, 800000000

  6. Page 3 : Chap.2 & C의 기본문법(4/4) 11) , 12) : a • - n = ~ n + 1, • ~(0000101101) + 1 = 1111010010 +1 = 1111010011 13) %eax 14) sizeof(char) + sizeof(int) + sizeof(void *) + sizeof(int *) • IA32 : 1 + 4 + 4 + 4 = 13, • Intel x86_64 : 1 + 4 + 8 + 8 = 21 , sizeof(int) =4, sizeof(long int) = 8. • 그러나, linux의 버전과 compiler별 차이를 분명히명시하지 않았던 점으로 인하여 sizeof(int) = 8 로 계산한 1 + 8 + 8 + 8 = 25 의 답도 인정함. 15) Lea vs. Mov, • Lea: 0x10(%eax, %ecx, 4) , %eax %eax = 16+ %eax + 4*%ecx • Mov: 0x10(%eax, %ecx, 4) , %eax %eax = *(16 + %eax + 4*%ecx) • 16) • #include<stdio.h> • int main(){ • int x = 0; • printf("Please input an integer:"); • scanf("%d", x ); • printf("%d", (!!x)<<31); • }

  7. Page 4 : Chap.2 – Floating Point Format • 7-bit floating point representation based on the IEEE floating point • There is a sign bit in the most significant bit. • The next 3 bits are the exponent. The exponent bias is 3. • The last 3 bits are the fraction. V = (-1)s * M * 2E M is the significand and E the integer value of the exponent 0 000 000 • E = exp - Bias (norm.) • E = 1 - Bias ( denorm.) exp frac. 2 와 3/8 = 10.011 (-1) 0 * 1.0011 * 21 따라서, s = 0 E = 1 M = 1.0011, exp = 3 +1 = 4 frac. = 010 ( ∵ 소수부0011의 3bit를취할때,round-to-even 적용)

  8. Page 3. 8) ¼ - bitrepresentation in IEEE 754 • IEEEE 754 = 1 sign bit, 8 exp bits , 23 frac. bits • Bias = 28-1 – 1 = 27– 1 = 127 , sign bit = 0. • ¼ = 0.01 • (-1)0 * 1.0 * 2 -2 • exp = Bias + E = 127 + (-2) = 125, 0111 1101 • frac = 0000 …. 0000 • ∴0 01111101 0000…0000 • = 3E8 0 0 0 0 0

  9. Stack “Bottom” Increasing Addresses Stack Grows Down Stack Pointer: %esp Stack “Top” IA32 Stack • Region of memory managed with stack discipline • Grows toward lower addresses • Register %esp contains lowest stack address • address of “top” element • Push : %esp = %esp – 4 • Pop : %esp = %esp+ 4 • Call Procedure : • pushnext address(Return address) . . . . 0x110 • ex) call 0x8048b90 • (Next addr = 0x8048553) 0x10c 0x104 0x108 123 %esp 0x108 0x8048b90 0x104 0x8048553 %eip 0x804854e

  10. 9*9단 inline- assembly code 에서 void print(int x, int y); .. ".Lp3 : " "movl %1, 4(%%esp) " "movl %0, (%%esp) " "call print"  //0x804b90 "movl 4(%%esp), %1" "movl (%%esp), %0 " ... :"r" (i), "r" (j).. call 8048b90 %esp 0x108 . . . . 0x110 %ebp 0x804854e 0x10c %ebx i 0x108 %esp %edx j %eax 0x804b1204 %eip 0x804b1100

  11. 9*9단 inline- assembly code 에서 void print(int x, int y); .. ".Lp3 : " "movl %1, 4(%%esp) " "movl %0, (%%esp) " "call print"  //0x804b90 "movl 4(%%esp), %1" "movl (%%esp), %0 " ... :"r" (i), "r" (j).. call 8048b90 %esp 0x108 . . . . 0x110 %ebp 0x804854e j 0x10c %ebx i 0x108 i %esp %edx j %eax 0x804b1204 %eip 0x804b1200

  12. 9*9단 inline- assembly code 에서 void print(int x, int y); .. ".Lp3 : " "movl %1, 4(%%esp) " "movl %0, (%%esp) " "call print"  //0x804b90 "movl 4(%%esp), %1" "movl (%%esp), %0 " ... :"r" (i), "r" (j).. call 8048b90 %esp 0x104 . . . . 0x110 %ebp 0x804854e j 0x10c %ebx i 0x108 i 0x104 0x804b1204 %esp %edx j %eax 0x804b1204 %eip 0x804b90

  13. 9*9단 inline- assembly code 에서 void print(int x, int y); .. ".Lp3 : " "movl %1, 4(%%esp) " "movl %0, (%%esp) " "call print"  //0x804b90 "movl 4(%%esp), %1" "movl (%%esp), %0 " ... :"r" (i), "r" (j).. call 8048b90 %esp 0x100 . . . . 0x110 %ebp 0x804854e j 0x10c %ebx i 0x108 i 0x104 0x804b1204 %edx j 0x804854e %esp 0x100 %eax ... //setupcode in func. print pushl %ebp movl %esp, %ebp … %eip 0x804b90+a

  14. 9*9단 inline- assembly code 에서 void print(int x, int y); .. ".Lp3 : " "movl %1, 4(%%esp) " "movl %0, (%%esp) " "call print"  //0x804b90 "movl 4(%%esp), %1" "movl (%%esp), %0 " ... :"r" (i), "r" (j).. call 8048b90 %esp 0x100 . . . . 0x110 %ebp 0x100 j 0x10c %ebx i 0x108 i 0x104 0x804b1204 %edx j %ebp 0x804854e 0x100 %esp %eax ... //setupcode in func. print pushl %ebp movl %esp, %ebp … pop %ebp %eip 0x804b90+b

  15. 9*9단 inline- assembly code 에서 void print(int x, int y); .. ".Lp3 : " "movl %1, 4(%%esp) " "movl %0, (%%esp) " "call print"  //0x804b90 "movl 4(%%esp), %1" "movl (%%esp), %0 " ... :"r" (i), "r" (j).. call 8048b90 %esp 0x104 . . . . 0x110 %ebp 0x804854e j 0x10c %ebx i 0x108 i 0x104 0x804b1204 %esp %edx j %eax ... //return from func. print … pop %ebp %eip 0x804b90+c

  16. 9*9단 inline- assembly code 에서 void print(int x, int y); .. ".Lp3 : " "movl %1, 4(%%esp) " "movl %0, (%%esp) " "call print"  //0x804b90 "movl 4(%%esp), %1" "movl (%%esp), %0 " ... :"r" (i), "r" (j).. call 8048b90 %esp 0x108 . . . . 0x110 %ebp 0x804854e j 0x10c %ebx i 0x108 i %esp %edx j %eax ... %eip 0x804b1204

  17. voidgugu_no_loop(){ • int i=0, j=0; • __asm__ __volatile__( • "movl $0, %0 \n\t" • "movl $0, %1 \n\t" • ".Lp1 : ""cmpl $9, %0 \n\t" • "jge .End \n\t" • "incl %0 \n\t" • ".Lp2 : ""cmpl $9, %1 \n\t" • "jl .Lp4 \n\t" • "movl $0, %1 \n\t" • "movl %1, 4(%%esp) \n\t" • "movl %0, (%%esp) \n\t" • "call println \n\t" • "movl 4(%%esp), %1 \n\t" • "movl (%%esp), %0 \n\t" • "jmp .Lp1 \n\t" • ".Lp3 : " "movl %1, 4(%%esp) \n\t" • "movl %0, (%%esp) \n\t" • "call print \n\t" • "movl 4(%%esp), %1 \n\t" • "movl (%%esp), %0 \n\t" • "jmp .Lp2 \n\t" • ".Lp4 : ""incl %1 \n\t" • "jmp .Lp3 \n\t" • ".End : ""movl $0, %1 \n\t" • : • :"r" (i), "r" (j) • ); • } #include<stdio.h> voidgugu_for(); voidgugu_no_loop(); voidprintln(){ printf("\n"); } void print(int x, int y){ printf("%2d x %2d = %2d \n", x, y, x*y); } voidgugu_for(){ inta, sum, i, j; a = 1; for( i=1; i<=9; i++){ for(j=1; j<=9; j++){ __asm__ __volatile__( "movl %1, %0 \n\t" "imull %2, %0 \n\t" "movl %2, 4(%%esp) \n\t" "movl %1, (%%esp) \n\t" "call print" : "=g" (sum) :"r" (i), "r" (j) ); // printf("%d x %d = sum=%d \n", i, j, sum); } printf("\n"); } }

  18. %ebp %ebp %ebp %ebp %esp %esp %esp %esp Page 4 : Chap. 3 – Stack&Assembly코드의 이해 0x300 Stack 1 • pushl %ebp • movl %esp,%ebp • movl 8(%ebp),%edx • movl 12(%ebp),%eax • movl %ebp,%esp • movl (%edx),%edx • addl %edx,(%eax) • movl %edx,%eax • popl %ebp • ret … 0x10c 2 0x200 %esp 0x108 0x100 func1 0x104 %ebp 0x100 func2 func3 0x100 %eax 300 200 %edx Rtnaddr Old ebp %ebp %esp

  19. Page 4 : Chap. 3 – Stack&Assembly코드의 이해 0x300 1 • pushl %ebp • movl %esp,%ebp • movl 8(%ebp),%edx • movl 12(%ebp),%eax • movl %ebp,%esp • movl (%edx),%edx • addl %edx,(%eax) • movl %edx,%eax • popl %ebp • ret 0x10c 2 0x200 %esp 0x108 0x100 0x104 %ebp 0x100 0x100 %eax *(0x100+c) 300 300 200 %edx *(0x100+8) 200 Rtnaddr Old ebp %ebp %esp

  20. 0x300 Page 4 : Chap. 3 – Stack&Assembly코드의 이해 0x200 • pushl %ebp • movl %esp,%ebp • movl8(%ebp),%edx • movl 12(%ebp),%eax • movl %ebp,%esp • movl (%edx),%edx • addl %edx,(%eax) • movl %edx,%eax • popl %ebp • ret 1 0x10c 2 %esp 0x108 0x100 0x104 %ebp 0x100 0x100 %eax *(0x100+c) 300 300 200 %edx *(0x100+8) 200 Rtnaddr Old ebp %ebp %esp

  21. 0x300 Page 4 : Chap. 3 – Stack&Assembly코드의 이해 0x200 • pushl %ebp • movl %esp,%ebp • movl8(%ebp),%edx • movl 12(%ebp),%eax • movl %ebp,%esp • movl (%edx),%edx • addl %edx,(%eax) • movl %edx,%eax • popl %ebp • ret 1 0x10c 2 %esp 0x108 0x100 0x104 %ebp 0x100 0x100 %eax *(0x100+c) 300 300 200 %edx 2 Rtnaddr Old ebp %ebp %esp

  22. 0x300 Page 4 : Chap. 3 – Stack&Assembly코드의 이해 0x200 • pushl %ebp • movl %esp,%ebp • movl8(%ebp),%edx • movl 12(%ebp),%eax • movl %ebp,%esp • movl (%edx),%edx • addl %edx,(%eax) • movl %edx,%eax • popl %ebp • ret 1+2 0x10c 2 %esp 0x108 0x100 0x104 %ebp 0x100 0x100 %eax *(0x100+c) 300 300 200 %edx 2 Rtnaddr Old ebp %ebp %esp

  23. 0x300 Page 4 : Chap. 3 – Stack&Assembly코드의 이해 0x200 • pushl %ebp • movl %esp,%ebp • movl8(%ebp),%edx • movl 12(%ebp),%eax • movl %ebp,%esp • movl (%edx),%edx • addl %edx,(%eax) • movl %edx,%eax • popl %ebp • ret 1+2 0x10c 2 %esp 0x108 0x100 0x104 %ebp 0x100 0x100 %eax 2 300 200 %edx 2 Rtnaddr Old ebp %ebp %esp

  24. Page 4 : Chap. 3 – Stack&Assembly코드의 이해 • pushl %ebp • movl %esp,%ebp • movl8(%ebp),%edx • movl 12(%ebp),%eax • movl %ebp,%esp • movl (%edx),%edx • addl %edx,(%eax) • movl %edx,%eax • popl %ebp • ret 0x300 1 0x10c 2 0x200 %esp 0x108 0x104 0x104 %ebp Old ebp %eax *(0x100+c) 300 300 200 %edx *(0x100+8) 200 Rtnaddr Old ebp %esp

More Related