Last active
June 17, 2022 11:14
-
-
Save etemesi254/76bb35d6fb5275b6bcbabe526fffb635 to your computer and use it in GitHub Desktop.
First attempt to do bubble sort in 8086 assembly
This file contains 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
; Smallest number in an array of data | |
; Elements are stored from 0xf00 going forward so we ideally start there | |
; The layout is as follows | |
; ┌──────────┬────────────────────────────┐ | |
; │ │ │ | |
; │length │items │ | |
; │ │ │ | |
; └──────────┴────────────────────────────┘ | |
; | |
;The length is the first memory location, i.e where register pair LH points to | |
; currently thats 00f0H(Hexadecimal) bec | |
; 1.) Load the address of the first element of the array into HL Pair | |
lxi h,00f0H ;Point HL to the memory address at 4200H | |
; 2.) Move the count to C-reg | |
mov c,m | |
step3:nop | |
; 3. ) Copy it in D -register(For bubble sort (N-1) times required) | |
mov d,c | |
; 4.) Get the first value in A register | |
inr l; Increment L to point to the first element | |
mov a, m ; Move the first element to A | |
; 5.) Compare with the value at the next location | |
step5:nop | |
inr l | |
cmp m | |
; 6.) If they are out of order, exhange contents of A register and memory. | |
; So we are checking for ascending order. if we do the compare and A is less than memory | |
; the carry flag is set, if the carry flag is set it means they are in order | |
; so we don't need to swap, but if the flag is set we exhcange and continue | |
jc next; | |
; Here exchange contents of register with memory | |
mov b,m ; move memory to b( | |
mov m,a ; Move a to memory | |
mov a,b ; Move element that was in memory which we stored to b to a | |
dcr l ; Reduce L | |
mov m,b ; Exchange elements in memory. this essentially does a swap | |
inr l ; Increment L | |
next:nop | |
; 7.) Decrement D register by 1 | |
dcr d | |
; 8.) Repeat step 5 and 7 till the value in D becomes zero | |
jnz step5 | |
; 9.) Decrement C content by 1. | |
dcr c | |
; 10.) Repeat steps 3 to 9 till the value in C register becomes zero. | |
jnz step3 | |
hlt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment