IT공부/IT서적

[윤성우 열혈 C프로그래밍] 디버깅 빌드, 어셈블리 코드 - Chapter5

shine94 2025. 1. 12. 22:46

 

#include <stdio.h>

int main(void)
{
006C1870  push        ebp  
006C1871  mov         ebp,esp  
006C1873  sub         esp,0E8h  
006C1879  push        ebx  
006C187A  push        esi  
006C187B  push        edi  
006C187C  lea         edi,[ebp-28h]  
006C187F  mov         ecx,0Ah  
006C1884  mov         eax,0CCCCCCCCh  
006C1889  rep stos    dword ptr es:[edi]  
006C188B  mov         ecx,offset _4B8E694C_SizeOfOperator@c (06CC008h)  
006C1890  call        @__CheckForDebuggerJustMyCode@4 (06C132Ah)  
006C1895  nop  
	char ch = 9;
006C1896  mov         byte ptr [ch],9  
	int inum = 1052;
006C189A  mov         dword ptr [inum],41Ch  
	double dnum = 3.14;
006C18A1  movsd       xmm0,mmword ptr [__real@40091eb851eb851f (06C7C18h)]  
006C18A9  movsd       mmword ptr [dnum],xmm0  

	printf("변수 ch의 크기 : %d\n", sizeof(ch));
006C18AE  push        1  
006C18B0  push        offset string "\xba\xaf\xbc\xf6 ch\xc0\xc7 \xc5\xa9\xb1\xe2 : %d\n" (06C7B30h)  
006C18B5  call        _printf (06C10D2h)  
006C18BA  add         esp,8  
	printf("변수 inum의 크기 : %d\n", sizeof(inum));
006C18BD  push        4  
006C18BF  push        offset string "\xba\xaf\xbc\xf6 inum\xc0\xc7 \xc5\xa9\xb1\xe2 : %d\n" (06C7B4Ch)  
006C18C4  call        _printf (06C10D2h)  
006C18C9  add         esp,8  
	printf("변수 dnum의 크기 : %d\n\n", sizeof(dnum));
006C18CC  push        8  
006C18CE  push        offset string "\xba\xaf\xbc\xf6 dnum\xc0\xc7 \xc5\xa9\xb1\xe2 : %d\n\n" (06C7B68h)  
006C18D3  call        _printf (06C10D2h)  
006C18D8  add         esp,8  
	
	printf("char의 크기 : %d\n", sizeof(char));
006C18DB  push        1  
006C18DD  push        offset string "char\xc0\xc7 \xc5\xa9\xb1\xe2 : %d\n" (06C7B84h)  
006C18E2  call        _printf (06C10D2h)  
006C18E7  add         esp,8  
	printf("int의 크기 : %d\n", sizeof(int));
006C18EA  push        4  
006C18EC  push        offset string "int\xc0\xc7 \xc5\xa9\xb1\xe2 : %d\n" (06C7B9Ch)  
006C18F1  call        _printf (06C10D2h)  
006C18F6  add         esp,8  
	printf("long의 크기 : %d\n", sizeof(long));
006C18F9  push        4  
006C18FB  push        offset string "long\xc0\xc7 \xc5\xa9\xb1\xe2 : %d\n" (06C7BB0h)  
006C1900  call        _printf (06C10D2h)  
006C1905  add         esp,8  
	printf("long long의 크기 : %d\n\n", sizeof(long long));
006C1908  push        8  
006C190A  push        offset string "long long\xc0\xc7 \xc5\xa9\xb1\xe2 : %d\n\n" (06C7BC8h)  
006C190F  call        _printf (06C10D2h)  
006C1914  add         esp,8  

	printf("float의 크기 : %d\n", sizeof(float));
006C1917  push        4  
006C1919  push        offset string "float\xc0\xc7 \xc5\xa9\xb1\xe2 : %d\n" (06C7BE4h)  
006C191E  call        _printf (06C10D2h)  
006C1923  add         esp,8  
	printf("double의 크기 : %d\n", sizeof(double));
006C1926  push        8  
006C1928  push        offset string "double\xc0\xc7 \xc5\xa9\xb1\xe2 : %d\n" (06C7BFCh)  
006C192D  call        _printf (06C10D2h)  
006C1932  add         esp,8  

	return 0;
006C1935  xor         eax,eax  
}
006C1937  pop         edi  
006C1938  pop         esi  
006C1939  pop         ebx  
006C193A  add         esp,0E8h  
006C1940  cmp         ebp,esp  
006C1942  call        __RTC_CheckEsp (06C124Eh)  
006C1947  mov         esp,ebp  
006C1949  pop         ebp  
006C194A  ret

 

* xmm0 레지스터

   128비트 크기의 레지스터

   SIMD(Single Instruction, Multiple Data) 연산에 사용 : 한 번에 여러 데이터를 병렬 처리

   사용범위

   - 64비트 모드 : xmm0부터 xmm15까지,

   - 32비트 모드 : xmm0 ~ xmm7까지 사용 가능

   주 용도

   - 부동소수점 연산

 

#include <stdio.h>

int main(void)
{
00F244F0  push        ebp  
00F244F1  mov         ebp,esp  
00F244F3  sub         esp,108h  
00F244F9  push        ebx  
00F244FA  push        esi  
00F244FB  push        edi  
00F244FC  lea         edi,[ebp-48h]  
00F244FF  mov         ecx,12h  
00F24504  mov         eax,0CCCCCCCCh  
00F24509  rep stos    dword ptr es:[edi]  
00F2450B  mov         ecx,offset _1E7E2136_CharShortBaseAdd@c (0F2C008h)  
00F24510  call        @__CheckForDebuggerJustMyCode@4 (0F2131Bh)  
00F24515  nop  
	char num1 = 1, num2 = 2, result1 = 0;
00F24516  mov         byte ptr [num1],1  
00F2451A  mov         byte ptr [num2],2  
00F2451E  mov         byte ptr [result1],0  
	short num3 = 300, num4 = 400, result2 = 0;
00F24522  mov         eax,12Ch  
00F24527  mov         word ptr [num3],ax  
00F2452B  mov         eax,190h  
00F24530  mov         word ptr [num4],ax  
00F24534  xor         eax,eax  
00F24536  mov         word ptr [result2],ax  

	printf("size of num1 & num2: %d, %d\n", sizeof(num1), sizeof(num2));
00F2453A  push        1  
00F2453C  push        1  
00F2453E  push        offset string "size of num1 & num2: %d, %d\n" (0F27BCCh)  
00F24543  call        _printf (0F213B1h)  
00F24548  add         esp,0Ch  
	printf("size of num3 & num4: %d, %d\n\n", sizeof(num3), sizeof(num4));
00F2454B  push        2  
00F2454D  push        2  
00F2454F  push        offset string "size of num3 & num4: %d, %d\n\n" (0F27CD0h)  
00F24554  call        _printf (0F213B1h)  
00F24559  add         esp,0Ch  

	printf("size of char add: %d\n", sizeof(num1 + num2));
00F2455C  push        4  
00F2455E  push        offset string "size of char add: %d\n" (0F27E20h)  
00F24563  call        _printf (0F213B1h)  
00F24568  add         esp,8  
	printf("size of char add: %d\n\n", sizeof(num3 + num4));
00F2456B  push        4  
00F2456D  push        offset string "size of char add: %d\n\n" (0F285A8h)  
00F24572  call        _printf (0F213B1h)  
00F24577  add         esp,8  

	result1 = num1 + num2;
00F2457A  movsx       eax,byte ptr [num1]  
00F2457E  movsx       ecx,byte ptr [num2]  
00F24582  add         eax,ecx  
00F24584  mov         byte ptr [result1],al  
	result2 = num3 + num4;
00F24587  movsx       eax,word ptr [num3]  
00F2458B  movsx       ecx,word ptr [num4]  
00F2458F  add         eax,ecx  
00F24591  mov         word ptr [result2],ax  

	printf("size of result1 & result2: %d %d\n", sizeof(result1), sizeof(result2));
00F24595  push        2  
00F24597  push        1  
00F24599  push        offset string "size of result1 & result2: %d %@"... (0F28608h)  
00F2459E  call        _printf (0F213B1h)  
00F245A3  add         esp,0Ch  

	return 0;
00F245A6  xor         eax,eax  
}
00F245A8  pop         edi  
00F245A9  pop         esi  
00F245AA  pop         ebx  
00F245AB  add         esp,108h  
00F245B1  cmp         ebp,esp  
00F245B3  call        __RTC_CheckEsp (0F2123Fh)  
00F245B8  mov         esp,ebp  
00F245BA  pop         ebp  
00F245BB  ret

 

 

* 일반적으로 CPU가 처리하기에 가장 적합한 크기의 정수 자료형은 int로 정의한다

   따라서 int형 연산 속도가 다른 자료형의 연산 속도에 비해서 동일하거나 더 빠르다

 

#include <stdio.h>

int main(void)
{
00291960  push        ebp  
00291961  mov         ebp,esp  
00291963  sub         esp,0E4h  
00291969  push        ebx  
0029196A  push        esi  
0029196B  push        edi  
0029196C  lea         edi,[ebp-24h]  
0029196F  mov         ecx,9  
00291974  mov         eax,0CCCCCCCCh  
00291979  rep stos    dword ptr es:[edi]  
0029197B  mov         eax,dword ptr [__security_cookie (029A000h)]  
00291980  xor         eax,ebp  
00291982  mov         dword ptr [ebp-4],eax  
00291985  mov         ecx,offset _BB2611C7_CircleArea@c (029C008h)  
0029198A  call        @__CheckForDebuggerJustMyCode@4 (0291339h)  
0029198F  nop  
	double rad;
	double area;
	printf("원의 반지름 입력: ");
00291990  push        offset string "\xbf\xf8\xc0\xc7 \xb9\xdd\xc1\xf6\xb8\xa7 \xc0\xd4\xb7\xc2: " (0297B30h)  
00291995  call        _printf (02910DCh)  
0029199A  add         esp,4  
	scanf_s("%lf", &rad);
0029199D  lea         eax,[rad]  
002919A0  push        eax  
002919A1  push        offset string "%lf" (0297B48h)  
002919A6  call        _scanf_s (0291294h)  
002919AB  add         esp,8  

	area = rad * 3.14195;
002919AE  movsd       xmm0,mmword ptr [rad]  
002919B3  mulsd       xmm0,mmword ptr [__real@400922b6ae7d566d (0297B60h)]  
002919BB  movsd       mmword ptr [area],xmm0  
	printf("원의 넓이: %f\n", area);
002919C0  sub         esp,8  
002919C3  movsd       xmm0,mmword ptr [area]  
002919C8  movsd       mmword ptr [esp],xmm0  
002919CD  push        offset string "\xbf\xf8\xc0\xc7 \xb3\xd0\xc0\xcc: %f\n" (0297B4Ch)  
002919D2  call        _printf (02910DCh)  
002919D7  add         esp,0Ch  

	return 0;
002919DA  xor         eax,eax  
}
002919DC  push        edx  
002919DD  mov         ecx,ebp  
002919DF  push        eax  
002919E0  lea         edx,ds:[291A0Ch]  
002919E6  call        @_RTC_CheckStackVars@8 (02911F4h)  
002919EB  pop         eax  
002919EC  pop         edx  
002919ED  pop         edi  
002919EE  pop         esi  
002919EF  pop         ebx  
002919F0  mov         ecx,dword ptr [ebp-4]  
002919F3  xor         ecx,ebp  
002919F5  call        @__security_check_cookie@4 (0291159h)  
002919FA  add         esp,0E4h  
00291A00  cmp         ebp,esp  
00291A02  call        __RTC_CheckEsp (0291258h)  
00291A07  mov         esp,ebp  
00291A09  pop         ebp  
00291A0A  ret

 

#include <stdio.h>

int main(void)
{
00881960  push        ebp  
00881961  mov         ebp,esp  
00881963  sub         esp,0DCh  
00881969  push        ebx  
0088196A  push        esi  
0088196B  push        edi  
0088196C  lea         edi,[ebp-1Ch]  
0088196F  mov         ecx,7  
00881974  mov         eax,0CCCCCCCCh  
00881979  rep stos    dword ptr es:[edi]  
0088197B  mov         eax,dword ptr [__security_cookie (088A000h)]  
00881980  xor         eax,ebp  
00881982  mov         dword ptr [ebp-4],eax  
00881985  mov         ecx,offset _BB2611C7_CircleArea@c (088C008h)  
0088198A  call        @__CheckForDebuggerJustMyCode@4 (0881339h)  
0088198F  nop  
	float rad;
	float area;
	printf("원의 반지름 입력: ");
00881990  push        offset string "\xbf\xf8\xc0\xc7 \xb9\xdd\xc1\xf6\xb8\xa7 \xc0\xd4\xb7\xc2: " (0887B30h)  
00881995  call        _printf (08810DCh)  
0088199A  add         esp,4  
	scanf_s("%f", &rad);
0088199D  lea         eax,[rad]  
008819A0  push        eax  
008819A1  push        offset string "%f" (0887B48h)  
008819A6  call        _scanf_s (0881294h)  
008819AB  add         esp,8  

	area = rad * 3.14195f;
008819AE  movss       xmm0,dword ptr [rad]  
008819B3  mulss       xmm0,dword ptr [__real@404915b5 (0887B44h)]  
008819BB  movss       dword ptr [area],xmm0  
	printf("원의 넓이: %f\n", area);
008819C0  cvtss2sd    xmm0,dword ptr [area]  
008819C5  sub         esp,8  
008819C8  movsd       mmword ptr [esp],xmm0  
008819CD  push        offset string "\xbf\xf8\xc0\xc7 \xb3\xd0\xc0\xcc: %f\n" (0887B4Ch)  
008819D2  call        _printf (08810DCh)  
008819D7  add         esp,0Ch  

	return 0;
008819DA  xor         eax,eax  
}
008819DC  push        edx  
008819DD  mov         ecx,ebp  
008819DF  push        eax  
008819E0  lea         edx,ds:[881A0Ch]  
008819E6  call        @_RTC_CheckStackVars@8 (08811F4h)  
008819EB  pop         eax  
008819EC  pop         edx  
008819ED  pop         edi  
008819EE  pop         esi  
008819EF  pop         ebx  
008819F0  mov         ecx,dword ptr [ebp-4]  
008819F3  xor         ecx,ebp  
008819F5  call        @__security_check_cookie@4 (0881159h)  
008819FA  add         esp,0DCh  
00881A00  cmp         ebp,esp  
00881A02  call        __RTC_CheckEsp (0881258h)  
00881A07  mov         esp,ebp  
00881A09  pop         ebp  
00881A0A  ret

 

* 메모리 오퍼랜드 키워드

   byte ptr: 8비트(1바이트) 데이터

   word ptr: 16비트(2바이트) 데이터

   dword ptr: 32비트(4바이트) 데이터

   qword ptr: 64비트(8바이트) 데이터

   mmword ptr: 64비트(8바이트) 데이터

   xmmword ptr: 128비트(16바이트) 데이터

 

* CPU는 메모리 정렬(alignment)과 효율성을 위해 더 큰 레지스터를 사용