Coprimes: Difference between revisions

1,312 bytes added ,  3 years ago
Add 8086 Assembly
(Add 8080 assembly)
(Add 8086 Assembly)
Line 75:
18 29</pre>
 
=={{header|8086 Assembly}}==
<lang asm>puts: equ 9 ; MS-DOS syscall to print a string
cpu 8086
org 100h
section .text
mov si,pairs
load: lodsw ; Load pair into AH,AL
test ax,ax ; Stop on reaching 0
jz .done
mov cx,ax ; Keep a copy out of harm's way
call gcd ; Calculate GCD
dec al ; If GCD=1 then GCD-1=0
jnz load ; If that is not the case, try next pair
mov al,cl ; Otherwise, print the fist item
call pnum
mov al,ch ; Then the second item
call pnum
mov dx,nl ; Then a newline
call pstr
jmp load ; Then try the next pair
.done: ret
;;; AL = gcd(AH,AL)
gcd: cmp al,ah ; Compare AL and AH
je .done ; If AL == AH, stop
jg .sub ; If AL > AH, AL -= AH
xchg al,ah ; Otherwise, swap them first
.sub: sub al,ah
jmp gcd
.done: ret
;;; Print the decimal value of AL
pnum: mov bx,nbuf ; Pointer to output buffer
.dgt: aam ; AH = AL/10, AL = AL mod 10
add al,'0' ; Add ASCII 0 to digit
dec bx ; Store digit in buffer
mov [bx],al
mov al,ah ; Continue with rest of number
test al,al ; If not zero
jnz .dgt
mov dx,bx
pstr: mov ah,puts ; Print the buffer using MS-DOS
int 21h
ret
section .data
db '***' ; Number output buffer
nbuf: db ' $'
nl: db 13,10,'$' ; Newline
pairs: db 21,15
db 17,23
db 36,12
db 18,29
db 60,15
dw 0</lang>
{{out}}
<pre>17 23
18 29</pre>
=={{header|APL}}==
{{works with|Dyalog APL}}
2,093

edits