.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
15 replies to this topic
#1
Posted 26 December 2010 - 08:57 AM
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 :(
|
|
|
#2
Posted 26 December 2010 - 03:54 PM
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
Posted 27 December 2010 - 12:00 PM
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.
sudo rm -rf /
#4
Posted 27 December 2010 - 02:39 PM
How would that be useful, since you can't use it on two memory addresses?
Latinamne loqueris?
#5
Posted 27 December 2010 - 03:01 PM
Mebob suggested using registers, and you could use XCHG to easily swap the registers and then write them back.
sudo rm -rf /
#6
Posted 27 December 2010 - 03:06 PM
#7
Posted 27 December 2010 - 03:08 PM
#8
Posted 27 December 2010 - 03:17 PM
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):
The first variation:
The variation that uses XCHG:
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.
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
Posted 27 December 2010 - 03:29 PM
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:
Variation 1:
Variation 2 (with XCHG instruction):
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
Posted 28 December 2010 - 04:58 PM
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], ebxIt was a stupid moment
Latinamne loqueris?
#11
Posted 28 December 2010 - 05:06 PM
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
Posted 30 December 2010 - 09:26 AM
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


Sign In
Create Account


Back to top









