2013-12-19 22:22:57 +01:00

61 lines
1.5 KiB
NASM

.data
##ACHTUNG: Programm kann maximal 2^31 berechnen.
##Die Ausgabe kann fehlerhaft sein, nur unter Windows getestet
##Das Ergebniss (%eax) muss als unsigned long betrachtet werden!
# Eingaben:
x: .long 2
n: .long 31
# Zu berechnen: x^n
intout: .string "Wert: %ul\n"
.text
######################################################################
.globl _main
.global _mainCRTStartup # debugging 4 msvs/windows
######################################################################
_mainCRTStartup: # do nothing
jmp _main # jump to _main
######################################################################
_main:
movl $1, %eax # Lade 1 in %eax
movl x, %ebx # Lade x in %ebx
movl n, %ecx # Lade n in %ecx
call .x_hoch_n_iterativ # call subrutine x_hoch_n_iterativ
jmp .end # finish
######################################################################
# %eax = current result
# %ebx = x
# %ecx = n
.x_hoch_n_iterativ:
cmp $0, %ecx # n = 0 ?
je .x_hoch_n_iterativ_ancor
decl %ecx # n -= 1
mull %ebx # result *= x
#Hier wird unsigned multipliziert um für n einen Wert bis zu 31 zu ermöglichen.
call .x_hoch_n_iterativ # call x_hoch_n_iterativ to calculate the rest
ret # finish
.x_hoch_n_iterativ_ancor: # ancor-marker (%eax * 1)
ret # jump out
######################################################################
.end:
# Wert im %eax ausgeben
pushl %eax
pushl $intout
call _printf
# Exit
movl $1, %eax
#int $0x80