Jump to content

Bubble sort help

- - - - -

  • Please log in to reply
15 replies to this topic

#1
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Well i have been working on this one from 3 hours and i still can't get it to work . I simply used the bubble sort algorithm and translated into assembly on my own . Now i don't know where the problem is from my point it should sort but its not happening :(

.model small

    data_seg segment 'Data'
    
        myArray db 1,2,4,3,5
        cI dw 0
        cJ dw 0
        temp db 0
        count dw 0
    data_seg ends
    
    code_seg segment 'Code'
        assume cs:code_seg,ds:data_seg
            
            
    main proc far
                            mov Ax,data_seg
                            mov Ds,Ax
                                                        
                mov Bx,cI
                mov Di,cJ
                    OuterLoop:
                                    cmp cI,5D
                                        je Exit
                                    mov cJ,0
                                    mov Di,cJ
                                    
                            InnerLoop:                        
                                    cmp cJ,6D
                                        je gotoHell
                                        
                                    mov DL,myArray[Bx]
                                    mov CL,myArray[Di+1]
                                        
                                        
                                        cmp CL,DL
                                            jl    goSwap
                                            jmp tt
                                    
                                    tt:
                                        inc cJ
                                        mov Di,cJ
                                        jmp innerLoop
                                                
                                    goSwap:
                                                mov CH,myArray[Bx]
                                                mov temp,CH
                                                ;mov temp,myArray[Bx]
                                                
                                                mov CH,myArray[Di]
                                                mov myArray[Bx],CH
                                                ;mov myArray[Bx],myArray[Di]
                                                
                                                mov CH,temp
                                                mov myArray[Di],CH
                                                ;mov myArray[Di],temp
                                                
                                                inc cJ
                                                mov Di,cJ

                                                jmp InnerLoop
                                    gotoHell:
                                        inc cI
                                        mov Bx,cI
                                        je OuterLoop
                                    
        
            Exit:
            pLoop:        cmp count,5D
                    je ee
                    mov Bx,count
                    mov CL,myArray[Bx]
                    add CL,30H
                    mov DL,CL
                    mov AH,02
                    int 21H
                    inc count
                    jmp pLoop
                    ee:
                                    
                    mov AH,4CH
                    int 21H    
        main endp
    
    code_seg ends
    
end main


#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Your code looks way more complicated than it needs to be. All you have to do is load two consecutive terms in the array in to registers and load them back in to the memory in opposite spots e.g. load term 0 in to ax and term 1 in to bx, and if you need to swap them, load bx in to term 0 and ax in to term 1. There is no need for a temp variable in memory for this in assembly.
Latinamne loqueris?

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript

mebob said:

load term 0 in to ax and term 1 in to bx, and if you need to swap them, load bx in to term 0 and ax in to term 1.
You can use the XCHG instruction for this to make your life easier.
sudo rm -rf /

#4
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
How would that be useful, since you can't use it on two memory addresses?
Latinamne loqueris?

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Mebob suggested using registers, and you could use XCHG to easily swap the registers and then write them back.
sudo rm -rf /

#6
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
To make the code more understandable?
Latinamne loqueris?

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
To avoid unnecessary jumping and stuff.
sudo rm -rf /

#8
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,251 posts
  • Location:C:\Countries\US
I think the XCHG instruction could help, by shortening the program by a little bit.

Let's say the variables are 'i1' and 'i2' (integer 1 and integer 2):
i1                   DWORD ? 
i2                   DWORD ? 

The first variation:
mov eax, dword ptr [i1] 
mov ebx, dword ptr [i2] 
mov dword ptr [i1], ebx 
mov dword ptr [i2], eax 

The variation that uses XCHG:
mov eax, dword ptr [i1] 
xchg eax, dword ptr [i2] 
mov dword ptr [i1], eax 

The second variation is shorter by a whole instruction (well, there can't be a half-instruction (can there?), but anyway).

I checked both pieces of code and they worked.

#9
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,251 posts
  • Location:C:\Countries\US
Are you working with 16-bit?

I kind of forgot that this might not be 32-bit (as I'm used to 32-bit).

Anyway, same kind of idea:
i1                             resw  1 
i2                             resw  1 

Variation 1:
mov ax, word [i1] 
mov bx, word [i2] 
mov word [i1], bx 
mov word [i2], ax 

Variation 2 (with XCHG instruction):
mov ax, word [i1] 
xchg ax, word [i2] 
mov word [i1], ax 


#10
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Oh, I didn't think of using XCHG that way LOL. I was thinking like this:
mov eax, dword ptr [i1] 

mov ebx, dword ptr [i2]

xchg eax,ebx 

mov dword ptr [i1], eax 

mov dword ptr [i2], ebx
It was a stupid moment
Latinamne loqueris?

#11
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,251 posts
  • Location:C:\Countries\US
I don't think there's anything wrong with any of these three methods. It's probably just a matter of which way the person who's making the program wants the program to run. Of course, there are faster ways or smaller ways or maybe other ways, but I think it's fine, as long as the end result is right.

#12
peterandrew

peterandrew

    Newbie

  • Members
  • Pip
  • 2 posts
Hello...Bubble sort helps in many way ..makes the searching very easy and saves the time...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users