Skip to content

Bubble sort in X86-64 Assembly #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@
{
"lang": "nim",
"name": "Nim"
},
{
"lang": "x86asm",
"name": "X86 Assembly"
}
]
}
Expand Down
4 changes: 4 additions & 0 deletions contents/bubble_sort/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import:3-28, lang:"lisp"](code/lisp/bubble_sort.lisp)
{% sample lang="nim" %}
[import:5-9, lang:"nim"](code/nim/bubble_sort.nim)
{% sample lang="x86asm" %}
[import:44-110, lang:"x86asm"](code/x86asm/bubble_sort.asm)
{% endmethod %}

... And that's it for the simplest bubble sort method.
Expand Down Expand Up @@ -101,6 +103,8 @@ Trust me, there are plenty of more complicated algorithms that do precisely the
[import, lang:"lisp"](code/lisp/bubble_sort.lisp)
{% sample lang="nim" %}
[import, lang:"nim"](code/nim/bubble_sort.nim)
{% sample lang="x86asm" %}
[import, lang:"x86asm"](code/x86asm/bubble_sort.asm)
{% endmethod %}

<script>
Expand Down
147 changes: 147 additions & 0 deletions contents/bubble_sort/code/x86asm/bubble_sort.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
section .text
global main
extern printf
extern putchar

section .data
unsorted_str db "Unsorted array:", 10,0
fmt db "%d", 10,0
sorted_str db "Sorted array:", 10, 0

print_range:
push ebp
mov ebp, esp
push esi
push edi
push ebx
sub esp, 4
mov DWORD [ebp - 16], 0
jmp .for_check
.for_loop:
mov eax, DWORD [ebp - 16]
lea edx, [0 + eax * 4]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the 0 + since it is not really needed. This goes for all the following 0 + as well.

mov eax, DWORD [ebp + 8]
add eax, edx
push DWORD [eax]
push fmt
call printf
add esp, 8
add DWORD [ebp - 16], 1
.for_check:
mov eax, DWORD [ebp - 16]
cmp eax, DWORD [ebp + 12]
jb .for_loop
push 10
call putchar
add esp, 4
pop ebx
pop edi
pop esi
leave
ret

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as a style thing i would remove one of the newlines here.


bubble_sort:
push ebp
mov ebp, esp
push esi
push edi
push ebx
sub esp, 12
mov DWORD [ebp - 16], 0
jmp .for_a_check
.for_a_loop:
mov DWORD [ebp - 20], 0
jmp .for_b_check
.for_b_loop:
mov eax, DWORD [ebp - 20]
lea edx, [0 + eax * 4]
mov eax, DWORD [ebp + 8]
add eax, edx
mov edx, DWORD [eax]
mov eax, DWORD [ebp - 20]
add eax, 1
lea ecx, [0 + eax * 4]
mov eax, DWORD [ebp + 8]
add eax, ecx
mov eax, DWORD [eax]
cmp edx, eax
jle .if_false
mov eax, DWORD [ebp - 20]
lea edx, [0 + eax * 4]
mov eax, DWORD [ebp + 8]
add eax, edx
mov eax, DWORD [eax]
mov DWORD [ebp - 24], eax
mov eax, DWORD [ebp - 20]
add eax, 1
lea edx, [0 + eax * 4]
mov eax, DWORD [ebp+8]
add eax, edx
mov edx, DWORD [ebp - 20]
lea ecx, [0 + edx * 4]
mov edx, DWORD [ebp + 8]
add edx, ecx
mov eax, DWORD [eax]
mov DWORD [edx], eax
mov eax, DWORD [ebp - 20]
add eax, 1
lea edx, [0 + eax * 4]
mov eax, DWORD [ebp + 8]
add edx, eax
mov eax, DWORD [ebp-24]
mov DWORD [edx], eax
.if_false:
add DWORD [ebp - 20], 1
.for_b_check:
mov eax, DWORD [ebp + 12]
sub eax, 1
cmp DWORD [ebp - 20], eax
jb .for_b_loop
add DWORD [ebp - 16], 1
.for_a_check:
mov eax, DWORD [ebp - 16]
cmp eax, DWORD [ebp + 12]
jb .for_a_loop
pop ebx
pop edi
pop esi
leave
ret

main:
push ebp
mov ebp, esp
push esi
push edi
push ebx
sub esp, 44
mov DWORD [ebp - 56], 10
mov DWORD [ebp - 52], 1
mov DWORD [ebp - 48], 45
mov DWORD [ebp - 44], 756
mov DWORD [ebp - 40], 4569
mov DWORD [ebp - 36], 56
mov DWORD [ebp - 32], 3
mov DWORD [ebp - 28], 8
mov DWORD [ebp - 24], 5
mov DWORD [ebp - 20], -10
mov DWORD [ebp - 16], -4
push unsorted_str
call printf
add esp, 4
push DWORD [ebp - 56]
lea eax, [ebp - 52]
push eax
call print_range
call bubble_sort
push sorted_str
call printf
add esp, 4
call print_range
add esp, 52
pop ebx
pop edi
pop esi
leave
ret