Skip to content

Commit b5bc757

Browse files
authored
Merge pull request #367 from Gathros/euclid_asm
Adding Euclidean Algorithms in X86-64 Assembly
2 parents 81021d7 + f2e7c06 commit b5bc757

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
.intel_syntax noprefix
2+
3+
.section .rodata
4+
fmt: .string "%d\n"
5+
6+
.section .text
7+
.global main
8+
.extern printf
9+
10+
# rdi - a
11+
# rsi - b
12+
# RET rax - gcd of a and b
13+
euclid_mod:
14+
mov rax, rdi # Get abs of a
15+
sar rax, 31
16+
xor rdi, rax
17+
sub rdi, rax
18+
mov rax, rsi # Get abs of b
19+
sar rax, 31
20+
xor rsi, rax
21+
sub rsi, rax
22+
jmp mod_check
23+
mod_loop:
24+
xor rdx, rdx # Take the mod of a and b
25+
mov rax, rdi
26+
div rsi
27+
mov rdi, rsi # Set b to the mod of a and b
28+
mov rsi, rdx # Set a to b
29+
mod_check:
30+
cmp rsi, 0 # Check if b is non-zero
31+
jne mod_loop
32+
mov rax, rdi # Return the result
33+
ret
34+
35+
euclid_sub:
36+
mov rax, rdi # Get abs of a
37+
sar rax, 31
38+
xor rdi, rax
39+
sub rdi, rax
40+
mov rax, rsi # Get abs of b
41+
sar rax, 31
42+
xor rsi, rax
43+
sub rsi, rax
44+
jmp check
45+
loop:
46+
cmp rdi, rsi # Find which is bigger
47+
jle if_true
48+
sub rdi, rsi # If a is bigger then a -= b
49+
jmp check
50+
if_true:
51+
sub rsi, rdi # Else b -= a
52+
check:
53+
cmp rsi, rdi # Check if a and b are not equal
54+
jne loop
55+
mov rax, rdi # Return results
56+
ret
57+
58+
main:
59+
mov rdi, 4288 # Call euclid_mod
60+
mov rsi, 5184
61+
call euclid_mod
62+
mov rdi, OFFSET fmt # Print output
63+
mov rsi, rax
64+
xor rax, rax
65+
call printf
66+
mov rdi, 1536 # Call euclid_sub
67+
mov rsi, 9856
68+
call euclid_sub
69+
mov rdi, OFFSET fmt # Print output
70+
mov rsi, rax
71+
xor rax, rax
72+
call printf
73+
xor rax, rax # Return 0
74+
ret

contents/euclidean_algorithm/euclidean_algorithm.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
3939
[import:12-25, lang="julia"](code/julia/euclidean.jl)
4040
{% sample lang="nim" %}
4141
[import:13-24, lang="nim"](code/nim/euclid_algorithm.nim)
42+
{% sample lang="asm-x64" %}
43+
[import:43-78, lang="asm-x64"](code/asm-x64/euclidean_example.s)
4244
{% sample lang="f90" %}
4345
[import:1-19, lang="fortran"](code/fortran/euclidean.f90)
4446
{% sample lang="php" %}
@@ -106,6 +108,8 @@ Modern implementations, though, often use the modulus operator (%) like so
106108
[import:1-10, lang="julia"](code/julia/euclidean.jl)
107109
{% sample lang="nim" %}
108110
[import:1-11, lang="nim"](code/nim/euclid_algorithm.nim)
111+
{% sample lang="asm-x64" %}
112+
[import:8-41, lang="asm-x64"](code/asm-x64/euclidean_example.s)
109113
{% sample lang="f90" %}
110114
[import:21-34, lang="fortran"](code/fortran/euclidean.f90)
111115
{% sample lang="php" %}
@@ -178,6 +182,8 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout
178182
[import, lang="julia"](code/julia/euclidean.jl)
179183
{% sample lang="nim" %}
180184
[import, lang="nim" %](code/nim/euclid_algorithm.nim)
185+
{% sample lang="asm-x64" %}
186+
[import, lang="asm-x64"](code/asm-x64/euclidean_example.s)
181187
{% sample lang="f90" %}
182188
[import, lang="fortran"](code/fortran/euclidean.f90)
183189
{% sample lang="php" %}

0 commit comments

Comments
 (0)