게임프로그래밍/게임 프로그래머 입문 올인원

[게임 프로그래머 입문 올인원] 산술 연산, 비교 연산, 논리 연산

shine94 2024. 12. 16. 19:56

* 지금 테스트 코드는 전부 전역변수 > Data 영역에서 메모리 할당

 

* Alt + 커서 : 세로로 블록 설정

 

* 대입 연산자

   =

 

* 산술 연산자

   +, -, *, /, %

   +=, -=, *=, /=, %=

#include <iostream>
using namespace std;

int a, b, result;

int main(void)
{
00DC1000  push        ebp  
00DC1001  mov         ebp,esp  
	// 대입 연산
	a = 10;
00DC1003  mov         dword ptr [a (0DC33D0h)],0Ah  
	b = 3;
00DC100D  mov         dword ptr [b (0DC33C8h)],3  

	// + - * / %
	result = a + b;
00DC1017  mov         eax,dword ptr [a (0DC33D0h)]  
00DC101C  add         eax,dword ptr [b (0DC33C8h)]  
00DC1022  mov         dword ptr [result (0DC33CCh)],eax  
	result = a - b;
00DC1027  mov         ecx,dword ptr [a (0DC33D0h)]  
00DC102D  sub         ecx,dword ptr [b (0DC33C8h)]  
00DC1033  mov         dword ptr [result (0DC33CCh)],ecx  
	result = a * b;
00DC1039  mov         edx,dword ptr [a (0DC33D0h)]  
00DC103F  imul        edx,dword ptr [b (0DC33C8h)]  
00DC1046  mov         dword ptr [result (0DC33CCh)],edx  
	result = a / b;
00DC104C  mov         eax,dword ptr [a (0DC33D0h)]  
00DC1051  cdq  
00DC1052  idiv        eax,dword ptr [b (0DC33C8h)]  
00DC1058  mov         dword ptr [result (0DC33CCh)],eax  
	result = a % b;
00DC105D  mov         eax,dword ptr [a (0DC33D0h)]  
00DC1062  cdq  
00DC1063  idiv        eax,dword ptr [b (0DC33C8h)]  
00DC1069  mov         dword ptr [result (0DC33CCh)],edx  

	return 0;
00DC106F  xor         eax,eax  
}
00DC1071  pop         ebp  
00DC1072  ret

 

* 증감 연산자

   ++, --

#include <iostream>
using namespace std;

int hp;

int main(void)
{
002E1000  push        ebp  
002E1001  mov         ebp,esp  
	hp = 100;
002E1003  mov         dword ptr [hp (02E33C8h)],64h  

	// ++, --
	hp = hp + 1;
002E100D  mov         eax,dword ptr [hp (02E33C8h)]  
002E1012  add         eax,1  
002E1015  mov         dword ptr [hp (02E33C8h)],eax  
	hp += 1;
002E101A  mov         ecx,dword ptr [hp (02E33C8h)]  
002E1020  add         ecx,1  
002E1023  mov         dword ptr [hp (02E33C8h)],ecx  

	hp++;
002E1029  mov         edx,dword ptr [hp (02E33C8h)]  
002E102F  add         edx,1  
002E1032  mov         dword ptr [hp (02E33C8h)],edx  
	++hp;
002E1038  mov         eax,dword ptr [hp (02E33C8h)]  
002E103D  add         eax,1  
002E1040  mov         dword ptr [hp (02E33C8h)],eax  

	hp--;
002E1045  mov         ecx,dword ptr [hp (02E33C8h)]  
002E104B  sub         ecx,1  
002E104E  mov         dword ptr [hp (02E33C8h)],ecx  
	--hp;
002E1054  mov         edx,dword ptr [hp (02E33C8h)]  
002E105A  sub         edx,1  
002E105D  mov         dword ptr [hp (02E33C8h)],edx  

	return 0;
002E1063  xor         eax,eax  
}
002E1065  pop         ebp  
002E1066  ret

 

* 시프트 연산자

 : 왼쪽 혹은 오른쪽으로 밀어주는 역할

   <<, >>

#include <iostream>
using namespace std;

int hp;

int main(void)
{
006E1000  push        ebp  
006E1001  mov         ebp,esp  
	hp = 0b100;
006E1003  mov         dword ptr [hp (06E33C8h)],4  

	hp >>= 1;
006E100D  mov         eax,dword ptr [hp (06E33C8h)]  
006E1012  sar         eax,1  
006E1014  mov         dword ptr [hp (06E33C8h)],eax  

	return 0;
006E1019  xor         eax,eax  
}
006E101B  pop         ebp  
006E101C  ret

 

* 최상단 비트가 부호 비트이기 때문에 아래와 같은 예상치 못한 상황이 발생할 수 있음
   [해결] unsigned 처리

 

* 비교 연산자

   <, >, <=, >=, ==,  !=

#include <iostream>
using namespace std;

int a, b;
bool result;

int main(void)
{
00EC1000  push        ebp  
00EC1001  mov         ebp,esp  
00EC1003  sub         esp,18h  
	a = 100;
00EC1006  mov         dword ptr [a (0EC33D0h)],64h  
	b = 50;
00EC1010  mov         dword ptr [b (0EC33C8h)],32h  

	result = (a < b);
00EC101A  mov         eax,dword ptr [a (0EC33D0h)]  
00EC101F  cmp         eax,dword ptr [b (0EC33C8h)]  
00EC1025  jge         main+30h (0EC1030h)  
00EC1027  mov         dword ptr [ebp-4],1  
00EC102E  jmp         main+37h (0EC1037h)  
00EC1030  mov         dword ptr [ebp-4],0  
00EC1037  mov         cl,byte ptr [ebp-4]  
00EC103A  mov         byte ptr [result (0EC33CCh)],cl  
	result = (a > b);
00EC1040  mov         edx,dword ptr [a (0EC33D0h)]  
00EC1046  cmp         edx,dword ptr [b (0EC33C8h)]  
00EC104C  jle         main+57h (0EC1057h)  
00EC104E  mov         dword ptr [ebp-8],1  
00EC1055  jmp         main+5Eh (0EC105Eh)  
00EC1057  mov         dword ptr [ebp-8],0  
00EC105E  mov         al,byte ptr [ebp-8]  
00EC1061  mov         byte ptr [result (0EC33CCh)],al  
	result = (a <= b);
00EC1066  mov         ecx,dword ptr [a (0EC33D0h)]  
00EC106C  cmp         ecx,dword ptr [b (0EC33C8h)]  
00EC1072  jg          main+7Dh (0EC107Dh)  
00EC1074  mov         dword ptr [ebp-0Ch],1  
00EC107B  jmp         main+84h (0EC1084h)  
00EC107D  mov         dword ptr [ebp-0Ch],0  
00EC1084  mov         dl,byte ptr [ebp-0Ch]  
00EC1087  mov         byte ptr [result (0EC33CCh)],dl  
	result = (a >= b);
00EC108D  mov         eax,dword ptr [a (0EC33D0h)]  
00EC1092  cmp         eax,dword ptr [b (0EC33C8h)]  
00EC1098  jl          main+0A3h (0EC10A3h)  
00EC109A  mov         dword ptr [ebp-10h],1  
00EC10A1  jmp         main+0AAh (0EC10AAh)  
00EC10A3  mov         dword ptr [ebp-10h],0  
00EC10AA  mov         cl,byte ptr [ebp-10h]  
00EC10AD  mov         byte ptr [result (0EC33CCh)],cl  
	result = (a == b);
00EC10B3  mov         edx,dword ptr [a (0EC33D0h)]  
00EC10B9  cmp         edx,dword ptr [b (0EC33C8h)]  
00EC10BF  jne         main+0CAh (0EC10CAh)  
00EC10C1  mov         dword ptr [ebp-14h],1  
00EC10C8  jmp         main+0D1h (0EC10D1h)  
00EC10CA  mov         dword ptr [ebp-14h],0  
00EC10D1  mov         al,byte ptr [ebp-14h]  
00EC10D4  mov         byte ptr [result (0EC33CCh)],al  
	result = (a != b);
00EC10D9  mov         ecx,dword ptr [a (0EC33D0h)]  
00EC10DF  cmp         ecx,dword ptr [b (0EC33C8h)]  
00EC10E5  je          main+0F0h (0EC10F0h)  
00EC10E7  mov         dword ptr [ebp-18h],1  
00EC10EE  jmp         main+0F7h (0EC10F7h)  
00EC10F0  mov         dword ptr [ebp-18h],0  
00EC10F7  mov         dl,byte ptr [ebp-18h]  
00EC10FA  mov         byte ptr [result (0EC33CCh)],dl  

	return 0;
00EC1100  xor         eax,eax  
}
00EC1102  mov         esp,ebp  
00EC1104  pop         ebp  
00EC1105  ret

 

* 논리 연산자

   !, &&, ||

#include <iostream>
using namespace std;

bool a, b, result;

int main(void)
{
00161000  push        ebp  
00161001  mov         ebp,esp  
00161003  sub         esp,0Ch  
	a = true;
00161006  mov         byte ptr [a (01633CAh)],1  
	b = false;
0016100D  mov         byte ptr [b (01633C8h)],0  

	result = !a;
00161014  movzx       eax,byte ptr [a (01633CAh)]  
0016101B  test        eax,eax  
0016101D  jne         main+28h (0161028h)  
0016101F  mov         dword ptr [ebp-4],1  
00161026  jmp         main+2Fh (016102Fh)  
00161028  mov         dword ptr [ebp-4],0  
0016102F  mov         cl,byte ptr [ebp-4]  
00161032  mov         byte ptr [result (01633C9h)],cl  
	result = (a || b);
00161038  movzx       edx,byte ptr [a (01633CAh)]  
0016103F  test        edx,edx  
00161041  jne         main+57h (0161057h)  
00161043  movzx       eax,byte ptr [b (01633C8h)]  
0016104A  test        eax,eax  
0016104C  jne         main+57h (0161057h)  
0016104E  mov         dword ptr [ebp-8],0  
00161055  jmp         main+5Eh (016105Eh)  
00161057  mov         dword ptr [ebp-8],1  
0016105E  mov         cl,byte ptr [ebp-8]  
00161061  mov         byte ptr [result (01633C9h)],cl  
	result = (a && b);
00161067  movzx       edx,byte ptr [a (01633CAh)]  
0016106E  test        edx,edx  
00161070  je          main+86h (0161086h)  
00161072  movzx       eax,byte ptr [b (01633C8h)]  
00161079  test        eax,eax  
0016107B  je          main+86h (0161086h)  
0016107D  mov         dword ptr [ebp-0Ch],1  
00161084  jmp         main+8Dh (016108Dh)  
00161086  mov         dword ptr [ebp-0Ch],0  
0016108D  mov         cl,byte ptr [ebp-0Ch]  
00161090  mov         byte ptr [result (01633C9h)],cl  

	return 0;
00161096  xor         eax,eax  
}
00161098  mov         esp,ebp  
0016109A  pop         ebp  
0016109B  ret

 


movsx

상위 비트는 값의 부호 비트(최상단 비트)로 채워진다

 

movzx

상위 비트는 항상 0으로 채워진다

 

test

비트 단위 AND 연산 후 플래그 설정

ㄴ je는    ZF = 1일 때, 점프

ㄴ jne는  ZF = 0일 때, 점프


00161014  movzx       eax,byte ptr [a (01633CAh)]
▶ 00000000 00000000 00000000 00000001

0016101B  test        eax,eax 
▶  값 1, ZF는 0

0016101D  jne         main+28h (0161028h)  
▶  ZF가 0임
▶  00161028  mov         dword ptr [ebp-4],0  


 

 

 

 

* 해당 글은 게임 프로그래머 입문 올인원 강의를 보고 정리한 글입니다.

   강의_바로가기

 

[게임 프로그래머 입문 올인원] C++ & 자료구조/알고리즘 & STL & 게임 수학 & Windows API & 게임 서버

Rookiss | 어디부터 시작할지 막막한 게임 프로그래밍 입문자를 위한 All-In-One 커리큘럼입니다. C++, 자료구조/알고리즘, STL, 게임 수학, Windows API, 게임 서버 입문으로 이어지는 알찬 커리큘럼으로

www.inflearn.com