Jump to content

Coprimes: Difference between revisions

1,512 bytes added ,  3 years ago
Add 8080 assembly
(Add C++)
(Add 8080 assembly)
Line 6:
<br><br>
 
 
=={{header|8080 Assembly}}==
<lang 8080asm>puts: equ 9
org 100h
lxi h,pairs
load: mov b,m ; Load the current pair into (B,C)
inx h
mov c,m
inx h
xra a ; Load C into A and set flags
ora c
rz ; If zero, we've reached the end
push b ; Keep the current pair
call gcd ; Calculate GCD
pop b ; Restore the pair
dcr a ; If GCD = 1, then GCD-1 = 0
jnz load ; If not, then try the next pair
push h ; Keep the pair and the pointer
push b
mov a,b ; Print the first item
call pnum
pop b
mov a,c ; Then the second item
call pnum
lxi d,nl ; Then print a newline
mvi c,puts
call 5
pop h ; Restore the pointer
jmp load
;;; Let A = GCD(A,B) using the subtraction algorithm
;;; (The 8080 does not have division in hardware)
gcd: cmp b ; Compare A and B
rz ; If A == B, stop
jc gcdsw ; If A < B, then swap them
gcdsub: sub b ; Otherwise, A = A - B
jmp gcd
gcdsw: mov c,a ; Swap A and B
mov a,b
mov b,c
jmp gcdsub
;;; Print the decimal value of A
pnum: lxi d,nbuf ; End of output buffer
mvi c,10 ; Divisor
pdgt: mvi b,-1 ; Quotient
pdgdiv: inr b ; Division by trial subtraction
sub c
jnc pdgdiv
adi '0'+10 ; ASCII digit
dcx d ; Store in buffer
stax d
xra a ; Continue with quotient
ora b
jnz pdgt ; If not zero
dcr c ; CP/M syscall to print a string is 9
jmp 5
;;; Pairs to test
pairs: db 21,15 ; 2 bytes per pair
db 17,23
db 36,12
db 18,29
db 60,15
db 0,0 ; end marker
db '***' ; Number output buffer
nbuf: db ' $'
nl: db 13,10,'$'</lang>
{{out}}
<pre>17 23
18 29</pre>
 
=={{header|APL}}==
2,114

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.