{"id":567,"date":"2016-05-22T04:50:02","date_gmt":"2016-05-22T04:50:02","guid":{"rendered":"http:\/\/darthjedi.logiodice.com\/?p=567"},"modified":"2016-05-29T02:17:08","modified_gmt":"2016-05-29T02:17:08","slug":"bubble-sort-in-x86-asm","status":"publish","type":"post","link":"https:\/\/darthjedi.logiodice.com\/?p=567","title":{"rendered":"Bubble sort in x86 ASM"},"content":{"rendered":"<p>[[NOTE: For a more efficient way to implement the bubble sort, see my <a href=\"https:\/\/darthjedi.logiodice.com\/?p=576\">later post<\/a>]]<\/p>\n<p>Why? \u00a0I have no idea. \u00a0It&#8217;s funny how many times I set off my AV scanner trying to compile and run my PE. \u00a0That brings back some great memories with the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Virus_Creation_Laboratory\" target=\"_blank\">VCL<\/a>.<\/p>\n<p>I&#8217;m sure there are cleaner ways to do it &#8211; but right now, I&#8217;m just worried about making it work. \u00a0\ud83d\ude09<\/p>\n<p>NOTE: Written in <a href=\"http:\/\/flatassembler.net\/\" target=\"_blank\">FASM<\/a>, and the training.inc can be found over at <a href=\"https:\/\/github.com\/xorpd\/asm_prog_ex\" target=\"_blank\">xorpd on git<\/a>.<\/p>\n<p>; Author J. Logiodice<br \/>\n; Date: 05\/22\/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 dd ?<\/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, TOTAL_NUMS<br \/>\nmov esi, 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; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br \/>\nloopRead:<\/p>\n<p>dec 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; I am sure there is a more effecient way of doing this<br \/>\n; I will probably try and clean it up later, but for now it works.<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, TOTAL_NUMS<br \/>\nmov esi, ecx<\/p>\n<p>; set the swapped variable to false<br \/>\nmov [boolSwapped], dword 0<\/p>\n<p>loopSort:<\/p>\n<p>dec esi<br \/>\nmov eax, esi<br \/>\nsub eax,1d<\/p>\n<p>test esi, esi<br \/>\njbe loopExit<br \/>\nmov edx, dword [array_numbers + eax * 4]<br \/>\ncmp edx, dword [array_numbers + esi * 4]<br \/>\njbe noSwap<br \/>\n; if we get into this section, then swapping needs to occur<br \/>\n; set the boolSwapped to true<br \/>\nmov [boolSwapped], dword 1<\/p>\n<p>; need to swap the two numbers<br \/>\nmov [nMinus1Mem], dword edx<br \/>\nmov edx, dword [array_numbers + esi * 4]<br \/>\nmov [array_numbers + eax * 4], dword edx<br \/>\nmov edx, dword [nMinus1Mem]<br \/>\nmov [array_numbers + esi * 4], dword edx<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 edi, array_numbers<br \/>\nmov ecx, TOTAL_NUMS<br \/>\nmov esi, ecx<\/p>\n<p>loopPrint:<\/p>\n<p>dec esi<br \/>\nmov eax, [array_numbers + esi * 4]<br \/>\ncall print_eax<\/p>\n<p>loop loopPrint<\/p>\n<p>exit_prog:<br \/>\npush 0<br \/>\ncall [ExitProcess]<br \/>\ninclude &#8216;training.inc&#8217;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[[NOTE: For a more efficient way to implement the bubble sort, see my later post]] Why? \u00a0I have no idea. \u00a0It&#8217;s funny how many times I set off my AV scanner trying to compile and run my PE. \u00a0That brings back some great memories with the VCL. I&#8217;m sure there are cleaner ways to do &hellip; <a href=\"https:\/\/darthjedi.logiodice.com\/?p=567\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bubble sort in x86 ASM&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":568,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6,28],"tags":[],"class_list":["post-567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-diary","category-technology"],"jetpack_featured_media_url":"https:\/\/darthjedi.logiodice.com\/wp-content\/uploads\/2016\/05\/bubblesort.jpg","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=\/wp\/v2\/posts\/567","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=567"}],"version-history":[{"count":8,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=\/wp\/v2\/posts\/567\/revisions"}],"predecessor-version":[{"id":578,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=\/wp\/v2\/posts\/567\/revisions\/578"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=\/wp\/v2\/media\/568"}],"wp:attachment":[{"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/darthjedi.logiodice.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}