Скачиваний:
34
Добавлен:
15.09.2014
Размер:
6.33 Кб
Скачать
// mmx.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream>
#include <xmmintrin.h>

#define	MAXSIZE	8

using namespace std;

int add(unsigned char*, unsigned char*, unsigned char*, unsigned char*);
int sub(unsigned char*, unsigned char*, unsigned char*, unsigned char*);
int mul(unsigned char*, unsigned char*, unsigned char*, unsigned char*);
int div(unsigned char*, unsigned char*, unsigned char*, unsigned char*);
int and(unsigned char*, unsigned char*, unsigned char*, unsigned char*);
int or(unsigned char*, unsigned char*, unsigned char*, unsigned char*);
int not_and(unsigned char*, unsigned char*, unsigned char*, unsigned char*);
int not_or(unsigned char*, unsigned char*, unsigned char*, unsigned char*);
int get_line(unsigned char*, int);

HANDLE HStdin;

int main(int argc, char* argv[])
{
	int i;
	unsigned char name[MAXSIZE];
	unsigned char surname[MAXSIZE];
	unsigned char firstname[MAXSIZE];
	unsigned char res[MAXSIZE];

	HStdin = GetStdHandle(STD_INPUT_HANDLE);

	ZeroMemory(name, MAXSIZE);
	ZeroMemory(surname, MAXSIZE);
	ZeroMemory(firstname, MAXSIZE);

	cout << "Enter name <= 8 symbols:" << endl;
	get_line(name, MAXSIZE);
	cout << "Enter surname <= 8 symbols:" << endl;
	get_line(surname, MAXSIZE);
	//cout << "Enter firstname <= 8 symbols:" << endl;
	//get_line(firstname, MAXSIZE);

	for(i = 0; i < MAXSIZE; i++) printf("%4x", name[i]);
	cout << endl;
	for(i = 0; i < MAXSIZE; i++) printf("%4x", surname[i]);
	//cout << endl;
	//for(i = 0; i < MAXSIZE; i++) printf("%4x", firstname[i]);
	cout << endl << endl;

	add(name, surname, firstname, res);
	sub(name, surname, firstname, res);
	/*mul(name, surname, firstname, res);
	and(name, surname, firstname, res);
	or(name, surname, firstname, res);
	not_and(name, surname, firstname, res);
	not_or(name, surname, firstname, res);*/
	getch();
	CloseHandle(HStdin);

	return 0;
}

int get_line(unsigned char *str, int max_len)
{
	DWORD is_read, is_read2;
	char trash[2];

	ReadFile(HStdin, str, max_len, &is_read, NULL);

	if(is_read < max_len)
	{
		str[is_read-2] = 0;
		str[is_read-1] = 0;
		is_read -= 2;
	}
	else
	{
		if(str[max_len-1] == 13)
		{
			str[max_len-1] = 0;
			is_read -= 1;
			ReadFile(HStdin, trash, 1, &is_read2, NULL);
		}
		else if(str[max_len-1] == 10)
		{
			str[max_len-2] = 0;
			str[max_len-1] = 0;
			is_read -= 2;
		}
		else 
		{
			ReadFile(HStdin, trash, 2, &is_read2, NULL);
		}
	}

	return is_read;
}

int add(unsigned char *name, unsigned char *surname, unsigned char *firstname, unsigned char *res)
{
	__asm	
	{
		mov		eax, name
		movq 	mm0, [eax]
		mov		eax, surname
		movq	mm1, [eax]
		//mov		eax, firstname
		//movq	mm2, [eax]

		paddb	mm0, mm1
		//paddb	mm0, mm2

		mov		eax, res
		movq	[eax], mm0
		emms
	}

	for(int i = 0; i < MAXSIZE; i++) printf("%4x", res[i]);
	cout << "\t - addition" << endl << endl;

	return 0;
}

int sub(unsigned char *name, unsigned char *surname, unsigned char *firstname, unsigned char *res)
{
	__asm	
	{
		mov		eax, name
		movq 	mm0, [eax]
		mov		eax, surname
		movq	mm1, [eax]
		//mov		eax, firstname
		//movq	mm2, [eax]

		psubb	mm0, mm1
		//psubb	mm0, mm2

		mov		eax, res
		movq	[eax], mm0
		emms
	}

	for(int i = 0; i < MAXSIZE; i++) printf("%4x", res[i]);
	cout << "\t - substraction" << endl << endl;

	return 0;
}

int mul(unsigned char *name, unsigned char *surname, unsigned char *firstname, unsigned char *res)
{
	unsigned char b[MAXSIZE];
	ZeroMemory(b, MAXSIZE);

	__asm	
	{
		mov		eax, name
		movq 	mm0, [eax]
		mov		eax, surname
		movq	mm1, [eax]
		//mov		eax, firstname
		//movq	mm2, [eax]

		pmullw	mm0, mm1
		movq	b,	 mm0

		mov		eax, name
		movq 	mm0, [eax]

		pmulhw	mm0, mm1
		mov		eax, res
		movq	[eax], mm0
		emms
	}

	for(int i = 0; i < MAXSIZE; i += 2)
	{
		printf("%4x", res[i+1]);
		printf("%4x ", res[i]);
		printf("%4x", b[i+1]);
		printf("%4x ", b[i]);
		printf("\n");
	}
	cout << " multiplying" << endl << endl;

	return 0;
}

int div(unsigned char *name, unsigned char *surname, unsigned char *firstname, unsigned char *res)
{
	cout << "does not work!";
	return 0;
}

int and(unsigned char *name, unsigned char *surname, unsigned char *firstname, unsigned char *res)
{
	__asm	
	{
		mov		eax, name
		movq 	mm0, [eax]
		mov		eax, surname
		movq	mm1, [eax]
		//mov		eax, firstname
		//movq	mm2, [eax]

		pand	mm0, mm1

		mov		eax, res
		movq	[eax], mm0
		emms
	}

	for(int i = 0; i < MAXSIZE; i++) printf("%4x", res[i]);
	cout << "\t - operation 'and'" << endl << endl;

	return 0;
}

int or(unsigned char *name, unsigned char *surname, unsigned char *firstname, unsigned char *res)
{
	__asm	
	{
		mov		eax, name
		movq 	mm0, [eax]
		mov		eax, surname
		movq	mm1, [eax]
		//mov		eax, firstname
		//movq	mm2, [eax]

		por	mm0, mm1

		mov		eax, res
		movq	[eax], mm0
		emms
	}

	for(int i = 0; i < MAXSIZE; i++) printf("%4x", res[i]);
	cout << "\t - operation 'or'" << endl << endl;

	return 0;
}

int not_and(unsigned char *name, unsigned char *surname, unsigned char *firstname, unsigned char *res)
{
	unsigned char b[MAXSIZE];

	memset((void*)b, 0xff, MAXSIZE);
	
	__asm	
	{
		mov		eax, name
		movq 	mm0, [eax]
		mov		eax, surname
		movq	mm1, [eax]
		movq	mm2, b
		//mov		eax, firstname
		//movq	mm2, [eax]

		pand	mm0, mm1
		pxor	mm0, mm2

		mov		eax, res
		movq	[eax], mm0
		emms
	}

	for(int i = 0; i < MAXSIZE; i++) printf("%4x", res[i]);
	cout << "\t - operation 'not and'" << endl << endl;

	return 0;
}

int not_or(unsigned char *name, unsigned char *surname, unsigned char *firstname, unsigned char *res)
{
	unsigned char b[MAXSIZE];

	memset((void*)b, 0xff, MAXSIZE);
	
	__asm	
	{
		mov		eax, name
		movq 	mm0, [eax]
		mov		eax, surname
		movq	mm1, [eax]
		movq	mm2, b
		//mov		eax, firstname
		//movq	mm2, [eax]

		por	mm0, mm1
		pxor	mm0, mm2

		mov		eax, res
		movq	[eax], mm0
		emms
	}

	for(int i = 0; i < MAXSIZE; i++) printf("%4x", res[i]);
	cout << "\t - operation 'not or'" << endl << endl;

	return 0;
}