본문 바로가기
IT/프로그래밍

SIMD 프로그래밍 사칙연산과 제어문

by nutrient 2021. 5. 30.
728x90
728x170

 

 

// add.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int A = 5;
	int B = 7;
	int C = 0;
	
	__asm
	{
		pushad
		mov eax, A	//eax = A 값 대입
		mov ebx, B	//ebx = B 값 대입
		add eax, ebx	//eax = eax + ebx;
		mov C, eax	//C = eax 대입
		popad
	}
	
	printf( "5 + 7 ADD Result : %d\n", C );
	return 0;
}


 

 

// div.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int A = 5;
	int B = 0;
	int C = 0;
	
	__asm
	{
		pushad
		mov eax, 17	//eax = 15;
		cdq		//32bit를 64bit로 확장
		//convert double word to quad word
		mov ebx, A	//ebx = A;
		div ebx		//eax = eax / ebx;
		mov B, eax	//B = eax;
		// edx = eax % eax;
		mov C, edx	//C = edx;
		popad
	}
	
	printf( "17/3 DIV Result : %d, %d\n", B, C );
	return 0;
}


 

 

// mov.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int a = 1;
	int nValue = 0;	//메모리 변수
	__asm
	{
		mov eax, 1	//레지스터 변수
		mov nValue, 2
	}
	printf("nValue : %d\n",nValue);
	return 0;
}


 

 

// mul.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int A = 5;
	int C = 0;
	
	__asm
	{
		pushad
		mov ebx, 15	//ebx = 15;
		mov eax, A	//eax = A; 
		mul ebx		//eax = ebx*eax;
		mov C, eax
		popad
	}
	
	printf( "15 * 5 MUL Result : %d\n", C );
	return 0;
}


 

 

// shift.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int nValue = 8;

	__asm
	{
		pushad
		mov eax, nValue		// eax = nValue (8);
		shl	eax, 1		// eax << 1; 8x2 = 16
		mov nValue, eax		// nValue = eax;
		popad
	}
	printf("SHL Result : %d\n", nValue);
	
	__asm
	{
		pushad
		mov eax, nValue		// eax = nValue; (16)
		shr	eax, 2		// eax >> 2; 16 / 4 = 4
		mov nValue, eax		// nValue = eax
		popad
	}
	printf("SHR Result : %d\n", nValue);
	return 0;
}


 

 

// sub.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int A = 5;
	int B = 7;
	int C = 0;
	
	__asm
	{
		pushad
		mov ebx, A 	//ebx = A;
		sub ebx, B	//ebx = ebx - B;
		mov C, ebx	//C = ebx;
		popad
	}
	
	printf( "5 - 7 SUB Result : %d\n", C );
	return 0;
}


 

 

// Loop.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int nValue = 0;
	__asm
	{
		pushad
			
		mov eax, 0		//eax = 0;
SIMDLOOP:					//반복 수행을 할 라벨 goto와 같다
		//실행할 연산 입력
			
		inc eax			//eax++
		cmp eax, 1000		//if(eax != 0) 
		//compare eax, 1000
		jne SIMDLOOP		//goto LOOP 
		//jump not equal LOOP
		
		mov nValue, eax		//nValue = eax;
		popad
	}
	printf("Result : %d\n", nValue);
	return 0;
}

 

 

// Loop2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int nValue = 1000;
	__asm
	{
		mov eax, 1000	//eax = 1000;
SIMDLOOP:
		
		dec eax		//eax--;
		jnz SIMDLOOP	//if( 0 != eax ) goto LOOP;
		//jump not zero, eax, to LOOP
		mov nValue, eax	//nValue = eax;	
	}
	printf("Result : %d\n",nValue);
	return 0;
}

 

 

// Loop3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int nValue = 1000;
	__asm
	{
		mov eax, 1000	//eax = 1000;
SIMDLOOP:
		//반복 연산 코드 작성


		dec eax		//eax--;
		cmp eax, 0	// eax 가 0과 같은 값인지 비교
		jne SIMDLOOP	//jump not equal goto LOOP;
		mov nValue, eax	//nValue = eax;
			
	}
	printf("Result : %d\n",nValue);
	return 0;
}

728x90
그리드형

댓글