How to resolve the algorithm Greatest common divisor step by step in the Z80 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Greatest common divisor step by step in the Z80 Assembly programming language

Table of Contents

Problem Statement

Find the greatest common divisor   (GCD)   of two integers.

Greatest common divisor   is also known as   greatest common factor (gcf)   and   greatest common measure.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Greatest common divisor step by step in the Z80 Assembly programming language

Source code in the z80 programming language

; Inputs: a, b
; Outputs: a = gcd(a, b)
; Destroys: c
; Assumes: a and b are positive one-byte integers
gcd:
    cp b
    ret z                   ; while a != b

    jr c, else              ; if a > b

    sub b                   ; a = a - b

    jr gcd

else:
    ld c, a                 ; Save a
    ld a, b                 ; Swap b into a so we can do the subtraction
    sub c                   ; b = b - a
    ld b, a                 ; Put a and b back where they belong
    ld a, c

    jr gcd

  

You may also check:How to resolve the algorithm MD5/Implementation step by step in the Seed7 programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Horizontal sundial calculations step by step in the Objeck programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the jq programming language
You may also check:How to resolve the algorithm The sieve of Sundaram step by step in the ALGOL 68 programming language