#include "HelloWorld.h"
#include <stdio.h>
#include <jni.h>

JNIEXPORT jint JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
	char hstring[] = "Hello World";

	__asm
	{
		lea eax, hstring
		push eax

		call printf

		add esp, 4
	}

	printf("Hello World!\n");
	return 0;
}



JNIEXPORT jint JNICALL Java_HelloWorld_ZeichneTwoDiagonals(JNIEnv *env, jobject obj)
{
	__asm
	{
		mov bl, 0
		

OuterLoop:
		mov bh, 0
		inc bl
		cmp bl, 23
		jge Ende

		mov eax, 10
		push eax
		call putchar
		add esp, 4

		mov eax, 13
		push eax
		call putchar
		add esp, 4

InnerLoop:
		inc bh
		cmp bh, 23
		jge OuterLoop

		cmp bl, bh
		je Cross

		mov cl, 23
		sub cl, bl
		cmp cl, bh
		je Cross

		mov eax, 176
		jmp EndIf
Cross:
		mov eax, 219
EndIf:
		push eax
		call putchar
		add esp, 4
		jmp InnerLoop


Ende:
		nop
	}
	return 0;
}



JNIEXPORT jint JNICALL Java_HelloWorld_BubbleSort(JNIEnv *env, jobject obj, jintArray arr, jint asize)
{
	int i = 0;
	jint *array = (*env)->GetIntArrayElements(env, arr, 0);
	unsigned char sorted = 0;
	asize--;
	
	__asm
	{
		mov esi, array ; Adresse des Arrays in esi laden
Anf:
		cmp sorted, 1 ; äussere while-Bedingung
		je Ende
		mov sorted, 1
		mov ecx, 0 ; Laufvariable zurücksetzen

Forlo:	cmp ecx, asize ; har der Array überhaupt eine Grösse?
		jge Anf
		mov eax, [esi] [ecx * 4] ; Laden der zwei Zahlen aus dem Array
		inc ecx
		mov ebx, [esi] [ecx * 4]
		cmp eax, ebx
		jle Forlo ; muss getauscht werden?

		mov sorted, 0
		mov edx, eax ; austauschen der zwei Zahlen
		mov eax, ebx
		mov ebx, edx
		dec ecx
		mov [esi] [ecx * 4], eax
		inc ecx
		mov [esi] [ecx * 4], ebx
		jmp Forlo

Ende:

	}



	return 0;
}
