sorting - Is there an easier way to write a bubble sort algorithm in masm modular style? -


i wrote bubble sort algorithm in assembly, i'm proud of myself, @ same time think bubble sort wrong.

can let me know if it's right? , how make program more modular can reuse later?

 .386 .model flat,stdcall .stack 100h  printf proto c arg1:ptr byte, printlist:vararg  .data  array dword 8,9,10,40,80,0 fmtmsg2 db 0dh,0ah,0 fmtmsg1 db "%d ",0  .code   public main  main proc        mov ecx,0       mov edx,0       mov esi,offset array innerloop:        inc ecx                   cmp ecx,5                 je outerloop              mov eax,[esi]             cmp eax,[esi + 4]         jge noexchange                ;exchange values          xchg eax,[esi+4]          mov [esi],eax         noexchange:       add esi,4                  jmp innerloop            outerloop:      mov esi,offset array       ;inner loop counter      mov ecx,0        ;outer loop counter      inc edx            cmp edx,5      jne innerloop       ;loop 3 counter      mov edx,0       ;load array offset      mov esi,offset array  loop3:       mov eax,[esi]      push edx      invoke printf,addr fmtmsg1,eax      pop edx       add esi,4      inc edx      cmp edx,5      jne loop3       invoke printf,addr fmtmsg2       ret main endp  end main 

your original algorithm works great (congratulations). sorts array in descending order, example, if array [1,2,3,4,5] result [5,4,3,2,1]. if want in ascending order, change 1 instruction. used visual studio 2010, code same (my changes pointed arrows, need 1 change: "jbe") :

void death_reverse () { int array[5] = { 5,4,3,2,1 };        // <===================== __asm {          mov ecx,0                   mov edx,0                   lea esi, array        // <=====================             innerloop:                    inc ecx                               cmp ecx,5                             je outerloop                          mov eax,[esi]                         cmp eax,[esi + 4]                     jbe noexchange         // <=============== ascending order.                    ;exchange values                      xchg eax,[esi+4]                      mov [esi],eax                     noexchange:                   add esi,4                              jmp innerloop                        outerloop:                  lea esi, array        // <=====================                   ;inner loop counter                  mov ecx,0                    ;outer loop counter                  inc edx                        cmp edx,5                  jne innerloop                   ;loop 3 counter                  mov edx,0                   ;load array offset                  lea esi, array        // <=====================              loop3:                   mov eax,[esi]                  push edx                  invoke printf,addr fmtmsg1,eax                  pop edx                   add esi,4                  inc edx                  cmp edx,5                  jne loop3                   invoke printf,addr fmtmsg2 } } 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -