// Addem Main Program      (AddMain.cpp) 

#include <iostream>
using namespace std;

extern "C" int addem(int p1, int p2, int p3);
extern "C" int fun(int x, int y, int z);
extern "C" int fun2(char *strx, int *Slen);

int main()
{
	const int BUFSIZE = 200;
	char buffer[BUFSIZE];
	char buffer1[BUFSIZE] = "abcdefghijkabcdefghijkabcdefghijkddd\0";
	int buffer2[BUFSIZE] = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 };
	int count;				// loop count
	int p1, p2, p3;
	int i;
	int strlen;
	
	//*****************************************************
	
	int total = addem(10, 15, 25);
	cout << "Total = " << total << endl << endl;

	//*****************************************************

	p1 = 20;  p2 = 15;  p3 = -2;
	int wynik = fun(p1, p2, p3);
	cout << "Wartosc funkcji w = 20*p3^2 - 100*p2 + p1" << endl;
	cout << "dla parametrow : p1 = "  << p1 << ", p2 = " << p2 << ", p3 = " << p3 << endl;
	cout << "wynosi ->   w = " << wynik << endl << endl;

	//*****************************************************

	for (count = 0; count < BUFSIZE; count++)  {
		if (buffer1[count] == '\0')  break;
	}
	cout << "Dlugosc lancucha znakow w tablicy buffer1 = " << count << endl;
	cout << "male litery \"PRZED\"  = " << buffer1 << endl;

	__asm {
		lea esi, buffer1  // Load Effective Address str. 68, 85
			lea edi, buffer
			mov ecx, count
			inc ecx
		L0 : mov dl, [esi]	// kopiowanie tablicy buffer1 do buffer
			 mov[edi], dl	// łącznie ze znakiem końca linii -> '\0'
			 inc esi
			 inc edi
			 loop L0

		;********

			lea esi, buffer1  // główna petla -> początek
			lea edi, buffer
			mov ecx, count
			dec ecx
		LA : mov dl, [esi]
			 cmp dl, 'd' //jesli rowne 'd' podmiana dwoch kol. znakow LB
			 jne LB
			 mov[edi], '*'
			 mov[edi + 1], '#'
		LB : inc esi
			 inc edi
			 Loop LA			// główna pętla -> koniec
	}	// asm

	cout << "male litery \"PO   \"  = " << buffer << endl << endl;

	//*****************************************************

	fun2(buffer, &strlen);
	cout << "Wielkie litery          = " << buffer << endl;
	cout << "Dlugosc lancucha znakow = " << strlen << endl << endl;

	//*****************************************************

	count = 10 - 1;

	__asm {
			lea esi, buffer2  // Load Effective Address str. 68, 85
			mov ecx, count
		L1 : mov edx, dword PTR[esi] //inty 32 bitowe
			cmp dl, 50;
			je L2
			inc  esi
			inc  esi
			inc  esi
			inc  esi
			Loop L1
		L2 : mov dword PTR[esi], 1000
			 mov dword PTR[esi + 4], 1001
	}	// asm

	cout << "Liczby = ";
	for (i = 0; i < 10; i++) { cout << buffer2[i] << ", "; }
	cout << endl;



	return 0;
}


