2011-10-18 11:17:11 +02:00

111 lines
1.8 KiB
NASM

.data
b: .long 5
e: .long 7
m: .long 77
intout:
.string "Ergebnis: %d\n"
.text
.globl main
main:
movl b, %eax
movl e, %ebx
movl m, %ecx
call .agy_pot
jmp .end
######################################
# %eax = b
# %ebx = e
# %ecx = m
.agy_pot:
cmp $0, %ebx # exponent = 0?
je .agy_pot_ancor # jump to ancor
movl %ebx, %edx # copy e to %edx
and $0b000001, %edx # mask last bit of %edx
cmp $0, %edx # %edx uneven?
je .agy_pot_irrelevant # even = irrelevant
jmp .agy_pot_relevant # uneven = relevant
.agy_pot_relevant:
pushl %eax # save basis
pushl %eax # save basis again
movl %ebx, %eax # %eax = exponent
movl $2, %ebx # %ebx = 2
cdq
idivl %ebx # exponent / 2
movl %eax, %ebx # %ebx = exponent / 2
popl %eax # restore basis
imull %eax, %eax # basis^2
cdq # Basis^2 MOD m
idivl %ecx
movl %edx, %eax
call .agy_pot # call agy_pot for the rest
popl %edx # get relevant basis
imull %edx, %eax # relvant basis * result from agy_pot
cdq
idivl %ecx # (relvant basis * result from agy_pot) mod m
movl %edx, %eax
ret
.agy_pot_irrelevant:
pushl %eax # save basis
movl %ebx, %eax # %eax = exponent
movl $2, %ebx # %ebx = 2
cdq
idivl %ebx # exponent / 2
movl %eax, %ebx # %ebx = exponent / 2
popl %eax # restore basis
imull %eax, %eax # basis^2
cdq # Basis^2 MOD m
idivl %ecx
movl %edx, %eax
call .agy_pot # call agy_pot for the rest
ret
.agy_pot_ancor:
movl $1, %eax # result = 1
ret
##AUSGABE####################################################################
.end:
# Wert im %eax ausgeben
pushl %eax
pushl $intout
call printf
# Exit
movl $1, %eax
int $0x80