{"id":576,"date":"2016-05-29T02:15:43","date_gmt":"2016-05-29T02:15:43","guid":{"rendered":"http:\/\/darthjedi.logiodice.com\/?p=576"},"modified":"2016-05-29T02:18:53","modified_gmt":"2016-05-29T02:18:53","slug":"updated-bubble-sort-in-x86-asm","status":"publish","type":"post","link":"https:\/\/darthjedi.logiodice.com\/?p=576","title":{"rendered":"Updated Bubble Sort in x86 ASM"},"content":{"rendered":"<p>So, after another week of studying assembly, I went back and rewrote my bubble sort algorithm. \u00a0I&#8217;m feeling a lot better about this implementation. \u00a0I&#8217;m sure it could could be even better, but I&#8217;m pretty pleased with it this time.<\/p>\n<p>Here is the code:<\/p>\n<p>&nbsp;<\/p>\n<p>; Author J. Logiodice<br \/>\n; Date: 05\/28\/2016<br \/>\n; Purpose: Bubble SOrt<br \/>\n; This method will read in a series of TOTAL_NUMS numbers<br \/>\n; And bubble sort them, then print them out in sorted order to the screen<br \/>\nformat PE console<br \/>\nentry start<\/p>\n<p>include &#8216;win32a.inc&#8217;<\/p>\n<p>TOTAL_NUMS = 10 ;10<\/p>\n<p>section &#8216;.bss&#8217; data readable writeable<\/p>\n<p>array_numbers dd TOTAL_NUMS dup (?)<br \/>\nnMinus1Mem dd ?<br \/>\nboolSwapped db ?<\/p>\n<p>section &#8216;.text&#8217; code readable executable<\/p>\n<p>start:<\/p>\n<p>; Set up the loop variables<br \/>\nmov ecx, dword TOTAL_NUMS<br \/>\nmov esi, dword TOTAL_NUMS ;ecx<br \/>\n; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br \/>\n; Read in one number at a time, for TOTAL_NUMS numbers, store them in the bytes that<br \/>\n; start with array_numbers<br \/>\n; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\/p>\n<p>loopRead:<\/p>\n<p>; have to use esi because read_hex uses edi<br \/>\ndec esi<\/p>\n<p>; read input into eax but first clear out eax<br \/>\nxor eax, eax<br \/>\ncall read_hex<\/p>\n<p>; move value into memory offset (reverse order)<br \/>\nmov dword [array_numbers + esi * 4], eax<\/p>\n<p>loop loopRead<\/p>\n<p>; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br \/>\n; loop iteratively over the array until no more swapping occurs,<br \/>\n; and the highest number ends up in the lowest part of the array (lowest to highest)<br \/>\n; start with array_numbers<br \/>\n; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br \/>\n; Set back up the loop variables<br \/>\nrestartLoop:<\/p>\n<p>mov ecx, dword TOTAL_NUMS &#8211; 1<br \/>\n; set the swapped variable to false<br \/>\nmov [boolSwapped], byte 0 ;dword 0<\/p>\n<p>lea esi, [array_numbers]<\/p>\n<p>loopSort:<\/p>\n<p>; jump to the loop exit if ecx is 0<br \/>\n; this will occur if we are at the end of the counter, or if the<br \/>\n; initial array was of size 0 bytes<br \/>\njecxz loopExit<br \/>\n; load the first number in array_numbers into eax<br \/>\nlodsd<br \/>\n; after lodsd, edi is pointing to edi &#8211; dword<br \/>\n; if we compare the value at edi at this point it is effectively the value below eax<br \/>\n; compare eax to the number below it in the array<br \/>\ncmp eax, [esi] ;, eax<br \/>\n; if [esi] &gt; eax, then no ZF or CF set, so jump below or equal<br \/>\njbe noSwap<br \/>\n; if here then eax is lower, swap eax and the higher number in memory<br \/>\nmov ebx, dword [esi]<br \/>\nmov [esi], dword eax<br \/>\nmov dword [esi-4], ebx<\/p>\n<p>; if a swap occured, set boolSwapped = 1<\/p>\n<p>mov [boolSwapped], 1b<br \/>\nnoSwap:<br \/>\n; jump here if no swapping needs to occur, but we&#8217;re still in the loop<br \/>\nloop loopSort<\/p>\n<p>loopExit:<\/p>\n<p>; if boolSwapped isn&#8217;t false, then we&#8217;ve swapped at least one<br \/>\n; during the iteration, let&#8217;s go through it one more time to make sure<br \/>\n; that we don&#8217;t have any more to swap<br \/>\ncmp [boolSwapped], 1b<br \/>\njae restartLoop<br \/>\n; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br \/>\n; Print each number back out to the screen for unmodified numbers<br \/>\n; we will loop from the lowest to highest part of the array &#8211; which is the largest to smallest number<br \/>\n; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<\/p>\n<p>mov ecx, TOTAL_NUMS<br \/>\nlea esi, [array_numbers]<\/p>\n<p>loopPrint:<\/p>\n<p>jecxz loopExit<br \/>\nlodsd<br \/>\ncall print_eax<\/p>\n<p>loop loopPrint<br \/>\nexit_prog:<br \/>\npush 0<br \/>\ncall [ExitProcess]<br \/>\ninclude &#8216;training.inc&#8217;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, after another week of studying assembly, I went back and rewrote my bubble sort algorithm. \u00a0I&#8217;m feeling a lot better about this implementation. \u00a0I&#8217;m sure it could could be even better, but I&#8217;m pretty pleased with it this time. Here is the code: &nbsp; ; Author J. Logiodice ; Date: 05\/28\/2016 ; Purpose: Bubble &hellip; <a href=\"https:\/\/darthjedi.logiodice.com\/?p=576\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Updated Bubble Sort in x86 ASM&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":579,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-576","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"jetpack_featured_media_url":"https:\/\/darthjedi.logiodice.com\/wp-content\/uploads\/2016\/05\/assembly2.jpg","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=\/wp\/v2\/posts\/576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=576"}],"version-history":[{"count":1,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=\/wp\/v2\/posts\/576\/revisions"}],"predecessor-version":[{"id":577,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=\/wp\/v2\/posts\/576\/revisions\/577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=\/wp\/v2\/media\/579"}],"wp:attachment":[{"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}