GGT-Berechnung


; Programm zur Berechnung des GGTs zweier Zahlen
; Clemens Adolphs, Immanuel-Kant-Gymnasium Heiligenhaus, Mai 2003
; rekursive Version
JMP Anfang
X1: DEF 0         ;Globale Variablen fuer Ein/Ausgabe
Y1: DEF 0
Ergebnis: DEF 0

rest:                ; int rest(int x, int y) = x % y
  schleife:
   LDAS 3            ; x - y
   SUBS 2
   JMS ende          ; if (x-y) >= 0
   STAS 3            ;   x = x-y
   JMP schleife
  ende:
  LDAS 3             ; else return x
  STAS 4
RTN

ggT:                 ; int ggT(int x, int y)
   PSH               ; lokale Variable fuer den Rest
   R    EQUAL 1      ; lokale Variable "Rest"
   x    EQUAL 4      ; Parameter x
   y    EQUAL 3      ; Parameter y
   gT   EQUAL 5      ; Rueckgabeadresse
   SPBP              ; Da ua. Rekursion auftritt, SP -> BP
   PSH               ; Platz fuer Rueckgabewert
   LDAB x
   PSH               ; Parameter x uebergeben
   LDAB y
   PSH               ; Parameter y uebergeben
   JSR Rest          ; Rest ermitteln
   POP
   POP
   POP
   STAB R            ; Rest in R speichern
   JZE fertig:       ; if (rest == 0) GoTo fertig
                     ; else return ggT(y, r)
   PSH               ; Platz fuer Rueckgabewert
   LDAB Y
   PSH               ; Parameter Y
   LDAB R
   PSH               ; Parameter R
   JSR ggT
   POP
   POP
   POP
   SPBP              ; SP -> BP (wichtig nach rekursivem Aufruf!)
   STAB gT
   POP
  RTN
  fertig:
   LDAB y
   STAB gT
   POP
  RTN

Anfang:
INM X1
INM Y1
PSH
PSHM X1
PSHM Y1
JSR ggT
POP
POP
POP
STA Ergebnis
OUT Ergebnis
END