중간고사 feedback
DESCRIPTION
중간고사 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 - PowerPoint PPT PresentationTRANSCRIPT
중간고사 feedback
2011. 10. 25최 윤 정
2
PointBinary / Hexadecimal / DecimalGrammar in C language
- %d %f %lf %u %x %p …
- *, &
Floating Point FormatStack 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 , integer 31 / interger 8 = 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 inte-
ger:");
scanf("%d", x );
printf("%d", (!!x)<<31);
}
7
Page 4 : Chap.2 – Floating Point Format
Description Binary exp M E V
Fraction number 0 010 010 2 5/4 -1 5/8
2 3/8 0 100 010 4 5/4 1 5/2
-infinity 1 111 000 7 - - -Largest Positive
Norm. 0 110 111 6 15/8 3 15Smallest Negative
Norm. 1 110 111 6 15/8 3 -15Largest Negative
Norm. 1 001 000 1 1 -2 -1/4
1 0 011 000 3 1 0 1Smallest Positive
Norm. 0 001 000 1 1 -2 1/4Largest Positive
DeNorm. 0 000 001 0 7/8 -2 7/32Smallest Positive
DeNorm. 0 000 111 0 1/8 -2 1/32
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
E = exp - Bias (norm.)E = 1 - Bias ( denorm.)
2 와 3/8 = 10.011 (-1) 0 * 1.0011 * 21
따라서 , s = 0E = 1M = 1.0011, exp = 3 +1 = 4frac. = 010
( ∵ 소수부 0011 의 3bit 를 취할 때 , round-to-even 적용 )
exp frac.
0 000 000
8
Page 3. 8) ¼ - bit representa-tion in IEEE 754 IEEEE 754 = 1 sign bit, 8 exp bits , 23
frac. bits Bias = 2 8-1 – 1 = 2 7 – 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 = 3 E 8 0 0 0 0 0
9
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 :
push next address(Return address)
Stack Pointer: %esp
Stack GrowsDown
IncreasingAddresses
Stack “Top”
Stack “Bottom”
%esp
%eip 0x804854e
0x108
0x10c
0x110
123
.
.
.
.
0x108
ex) call 0x8048b90(Next addr = 0x8048553)
0x80485530x104
0x104
0x8048b90
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)..
%esp
%ebp
%eip 0x804b1100
0x108
0x10c
0x1100x804854e
.
.
.
.
0x108
call 8048b90
%ebx
%edx j
i
%eax 0x804b1204
%esp
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)..
%esp
%ebp
%eip 0x804b1200
0x108
0x10c
0x1100x804854e
.
.
.
.
0x108
call 8048b90
%ebx
%edx j
i
%eax 0x804b1204
i
j
%esp
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)..
0x804b1204
0x108
0x10c
0x110
0x104
i
.
.
.
.
call 8048b90
j
%esp
%esp
%ebp
%eip 0x804b90
0x804854e
0x104
%ebx
%edx j
i
%eax 0x804b1204
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)..
0x804b1204
0x108
0x10c
0x110
0x104
i
.
.
.
.
call 8048b90
j
%esp0x804854e0x100
%esp
%ebp
%eip 0x804b90+a
0x100
%ebx
%edx j
i
%eax ...
0x804854e
//setup code in func. print
pushl %ebpmovl %esp, %ebp…
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)..
0x804b1204
0x108
0x10c
0x110
0x104
i
.
.
.
.
call 8048b90
j
%ebp0x100 %esp
%esp
%ebp
%eip 0x804b90+b
0x100
%ebx
%edx j
i
%eax ...
0x100
0x804854e
//setup code in func. print
pushl %ebpmovl %esp, %ebp…
pop %ebp
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)..
0x804b1204
0x108
0x10c
0x110
0x104
i
.
.
.
.
call 8048b90
j
%esp
%esp
%ebp
%eip 0x804b90+c
0x104
%ebx
%edx j
i
%eax ...
0x804854e
//return from func. print
…
pop %ebp
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)..
0x108
0x10c
0x110
i
.
.
.
.
call 8048b90
j
%esp
%esp
%ebp
%eip 0x804b1204
0x108
%ebx
%edx j
i
%eax ...
0x804854e
17
#include <stdio.h>void gugu_for();void gugu_no_loop();void println(){ printf("\n");}void print(int x, int y){ printf("%2d x %2d = %2d \n", x, y, x*y);}void gugu_for(){ int a, 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"); }
}
void gugu_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));
}
18
Page 4 : Chap. 3 – Stack&Assembly코드의 이해
…
Stack%ebp
%espfunc1
%ebp
%espfunc2
%ebp
%espfunc3
%ebp
%esp
%esp
%ebp 0x100
0x100
%eax
%edx
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
%esp%ebp
200
300
Old ebp
Rtn addr0x104
0x1080x10c
0x100
1
20x200
0x300
19
Page 4 : Chap. 3 – Stack&Assembly코드의 이해
%esp
%ebp 0x100
0x100
%eax
%edx
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
%esp%ebp
200
300
Old ebp
Rtn addr0x104
0x1080x10c
0x100
*(0x100+8)200
*(0x100+c)
300
1
20x200
0x300
20
Page 4 : Chap. 3 – Stack&Assembly코드의 이해
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
%esp
%ebp 0x100
0x100
%eax
%edx *(0x100+8)200
*(0x100+c)
300
%esp%ebp
200
1
Old ebp
Rtn addr0x104
0x1080x10c
0x100
0x200
0x300
2
300
21
Page 4 : Chap. 3 – Stack&Assembly코드의 이해
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
%esp
%ebp 0x100
0x100
%eax
%edx 2
*(0x100+c)
300
%esp%ebp
200
1
Old ebp
Rtn addr0x104
0x1080x10c
0x100
0x200
0x300
2
300
22
Page 4 : Chap. 3 – Stack&Assembly코드의 이해
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
%esp
%ebp 0x100
0x100
%eax
%edx 2
*(0x100+c)
300
%esp%ebp
200
1+2
Old ebp
Rtn addr0x104
0x1080x10c
0x100
0x200
0x300
2
300
23
Page 4 : Chap. 3 – Stack&Assembly코드의 이해
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
%esp
%ebp 0x100
0x100
%eax
%edx 2
2
%esp%ebp
200
1+2
Old ebp
Rtn addr0x104
0x1080x10c
0x100
0x200
0x300
2
300
24
Page 4 : Chap. 3 – Stack&Assembly코드의 이해
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
%esp
%ebp Old ebp
0x104
%eax
%edx *(0x100+8)200
*(0x100+c)
300
%esp
200
300
Rtn addr0x104
0x1080x10c
Old ebp
1
20x200
0x300