Modulo 3 bez dzielenia nie działa

0

Napisałem wg. http://compilers.iecc.com/comparch/article/99-10-056, ale nie działa dobrze. Wyświetla 0 dla np. 5.
Pierwsza część kodu to reszta z dzielenia przez 4,

var
        liczba : integer;

begin
        readln(liczba);

        asm
                mov ax, liczba
                and ax, 0000000000000011b
                mov liczba, ax
        end;
		
        writeln(liczba);
		
		readln(liczba);
		
		asm
			xor eax, eax
			xor ebx, ebx
			xor ecx, ecx
			xor edx, edx

			mov ax, liczba
			and ax, 33333333h
			mov bx, liczba
			and bx, 0cccccccch
			shr bx, 2

			mov ecx, eax //wynik
			add ecx, ebx
			mov edx, ecx //przesuniete
			shr edx, 2


			add ecx, edx
			and ecx, 33333333h
			mov ecx, edx
			shr edx, 4

			add ecx, edx
			mov edx, ecx
			shr edx, 2

			add ecx, edx
			and ecx, 33333333h

                        mov edx, ecx
                        shr edx, 8

                        add ecx, edx
                        mov edx, ecx
                        shr edx, 2
                        add ecx, edx

                        and ecx, 33333333h
                        mov edx, ecx
                        shr edx, 16
                        add ecx, edx

                        mov edx, ecx
                        shr edx, 2
                        add ecx, edx
                        and ecx, 3h

			mov liczba, cx
		end;

		writeln(liczba);
		readln();
end.
0

Zamień

add ecx, edx
and ecx, 33333333h
mov ecx, edx
shr edx, 4

na

add ecx, edx
and ecx, 33333333h
shr edx, 4
0

Nie pomogło, ale zauważyłem, że zamiast mov edx, ecx było mov ecx, edx. Poprawiłem, ale dalej nie działa dobrze. (edx-suma przesunięta, ecx-suma)

Przy wielokrotnościach 3 podaje wynik 3, a w innych przypadkach-dobry.

0

Do modulo w procesorach x86 jest odpowiednia instrukcja div.
Wystarczy zrobić tak:

mov eax, 10  ; liczb a
mov edx, 0 ; starsza czesc liczby a
mov ebx, 3  ; liczba b
div ebx
; w wyniku otrzymujemy edx = a mod b

I jest to szybsze od tego co ty tu wklejasz

1 użytkowników online, w tym zalogowanych: 0, gości: 1