Hi,
I am writing a (microsoft Intel P4) assembly program that copies a few floating point values from x to y and then bubble sorts the values (ascending) in y by using the FPU commands.

For some reason, one value is out of place. The value that should be in the first location is in the middle and the value that should be in the middle is in the first location. I have tried everything I can think of including sorting the values counting down from the last data location.

Here is the code: (also attached "sort.txt")

;;;;;;;;;;;;;;;;;;;;;;;;;


.model small
.586

.stack 100h

.data

x DD 23.5,99.5,44.5,67.25,11.25,78.5
y DD 6 DUP(?)
swap db ?
greater db ?

.code

begin:

.startup

CALL copy
MOV cx,5

nextR:
MOV byte ptr swap,0
PUSH cx
LEA Esi,y

nextC:
CALL compare
CMP byte ptr greater,1
JE doswap
JMP continue

doswap:
CALL exchange

continue:
ADD Esi,4

Loop nextC

CMP byte ptr swap,0
JE done
POP cx

Loop nextR

jmp done

copy proc

MOV Cx,6
LEA Esi,x
LEA Edi,y

small:

MOV Eax,
MOV , Eax

ADD Esi,4
ADD Edi,4

Loop small

RET
copy ENDP


compare proc

FLD dword ptr

FCOM dword ptr

FSTSW Ax

AND Ax,4500h
XOR Ax,0100h

MOV greater,Ah

RET
compare ENDP


exchange proc

MOV byte ptr swap,1

MOV Eax,
PUSH Eax
MOV Eax,
MOV ,Eax
POP Eax
MOV ,Eax

RET
exchange ENDP

done:
;;;;;;;;NEEDED IN ORDER TO GET THE SORTING FIXED;;;;;;;;;;;

LEA Esi,y
MOV Eax,
PUSH Eax
MOV Eax,
MOV ,Eax
POP Eax
MOV ,Eax

LEA Esi,y
MOV Eax,
PUSH Eax
MOV Eax,
MOV ,Eax
POP Eax
MOV ,Eax

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

.exit

end begin

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Can anyone help and tell me how to fix this?

Thanks,
E.D.1870
Posted on 2002-04-28 20:47:15 by Edwin_Drood_1870
Make sure you can understand this - your
instructor might want you to explain it. ;)
.data

fArray REAL4 2.5,1.4,1.7,-2.5,8.0,9.2,0.6,0.0
.code
mov ecx, LENGTHOF fArray
mov edi, OFFSET fArray

; ecx = count to sort
; edi = array address
outerloop:
mov edx,ecx
fld REAL4 PTR [edi]
jmp order_ok
innerloop:
fcom REAL4 PTR [edi+edx*4]
fstsw ax
test ax,4100h
jne order_ok
fld REAL4 PTR [edi+edx*4]
fxch
fstp REAL4 PTR [edi+edx*4]
order_ok:
dec edx
jne innerloop
fstp REAL4 PTR [edi]
add edi,4
dec ecx
jne outerloop
...the inner loop finds the smallest number in array. Then the number is put in the front and the array size is decreased. We do it again until the array is unit size. The inner loop counter is used as an index into the array - starting at the end - small numbers migrate forward. ;)
Posted on 2002-04-28 23:02:59 by bitRAKE
Thanks alot.

E.D.1870
Posted on 2002-04-30 11:25:07 by Edwin_Drood_1870