-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da1ee16
Adding x86 assembly to book.json
Gathros 1880319
Adding bubble_sort.asm
Gathros 7a836c1
Updaing bubble_sort.md
Gathros 415171a
changing x86 to x86-64
Gathros cd82b92
Adding x86-64 bubble sort
Gathros 27dafca
Adding bubble_sort.s
Gathros 35b7161
Merge branch 'master' into bubble_sort_asm
Gathros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,6 +135,10 @@ | |
{ | ||
"lang": "nim", | ||
"name": "Nim" | ||
}, | ||
{ | ||
"lang": "x86asm", | ||
"name": "X86 Assembly" | ||
} | ||
] | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 following0 +
as well.