News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

I needs your help

Started by shawn, April 09, 2010, 06:53:11 PM

Previous topic - Next topic

Slugsnack

well i was not building with masm32, but vc++

clive

Fixing the sort, there are a couple of things that could work more efficiently. The xchg is VERY expensive, and the loop could exit quicker if it determined the list was in order.
-Clive

goSort          PROC NEAR32

                push    esi
                push    edi

                dec     ecx             ; do not count last 0 input
                jz      lp4             ; Empty list
                dec     ecx             ; decrement outer loop counter by 1 (arraysize -1)
                jz      lp4             ; List of 1 already sorted

lp1:            mov     edx, ecx        ; save outer loop count
                lea     esi, nbrArray   ; point to first value

lp2:            mov     eax, [esi]
                cmp     [esi+4], eax
                jge     lp3

                xchg    eax, [esi+4]
                mov     [esi], eax

lp3:            add     esi, 4          ; move both pointers forward
                loop    lp2             ; inner loop

                mov     ecx, edx        ; retrieve outer loop count
                loop    lp1             ; else repeat outer loop

lp4:            pop     edi
                pop     esi
                ret

goSort          ENDP

It could be a random act of randomness. Those happen a lot as well.

clive

Here with some optimization. Still a bubblesort, but easy enough to understand.
-Clive

goSort          PROC NEAR32

                push    ebx
                push    esi
                push    edi

                dec     ecx             ; do not count last 0 input
                jz      lp4             ; Empty list
                dec     ecx             ; decrement outer loop counter by 1 (arraysize -1)
                jz      lp4             ; List of 1 already sorted

lp1:            mov     edx, ecx        ; save outer loop count
                lea     esi, nbrArray   ; point to first value
                xor     edi, edi        ; clear exchange flag

                mov     eax, [esi+0]    ; Hold current element in eax

lp2:            mov     ebx, [esi+4]    ; Hold next element in ebx
                cmp     ebx, eax
                jge     lp3

                xchg    eax, ebx        ; register to register cheap
                mov     [esi+0], eax    ; echo change back to memory
                mov     [esi+4], ebx    ;  will use write buffers

                add     edi, 1          ; flag an exchange

lp3:            add     esi, 4          ; move both pointers forward

                mov     eax, ebx        ; move element next element to current

                sub     ecx, 1
                jnz     lp2             ; inner loop

                or      edi, edi        ; list sorted if no exchanges
                jz      lp4

                mov     ecx, edx        ; retrieve outer loop count
                sub     ecx, 1
                jnz     lp1             ; else repeat outer loop

lp4:            pop     edi
                pop     esi
                pop     ebx

                ret

goSort          ENDP
It could be a random act of randomness. Those happen a lot as well.

lingo

#18
shawn,
just take a look :wink
OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE
align 16
BblSrt proc  lenArea:dword, lpArea:dword
pop eax   ; return addres
pop edx   ; edx->lenArea in DWords
mov [esp-1*4], ebx           ; saving register ebx
pop ebx   ; ebx->lpArea
mov [esp-1*4], esi          ; saving register esi
sub edx, 1
mov esi, esp   ; saving register esp
jle lExit
jne NoExchangesArea
align 16
ExchangesArea:
pop [esp-2*4]
sub ecx, 1
mov     [esp-1*4], eax
je NoExchangesArea
@@:
cmp eax, [esp]
jae ExchangesArea
sub ecx, 1
pop eax
jne @b

NoExchangesArea:
mov esp, ebx             ; ebx->lpArea
mov ecx, edx         ; edx->length of area /4 ->in Dwords
pop eax
@@:
cmp [esp], eax
jb ExchangesArea
sub ecx, 1
pop eax
jne @b
lExit:
mov ebx, [esi-2*4]   ; restoring ebx register
mov esp, esi ; restoring esp register
mov esi,  [esi-1*4] ; restoring esi register 
        jmp dword ptr [esp-3*4]     ; jmp to return address
BblSrt endp
OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef


clive

Quote from: lingo
   xchg   eax, [esp]

This instruction will absolutely murder performance, it is locked and atomic. You can measure the RAM access speed with this construct.

-Clive
It could be a random act of randomness. Those happen a lot as well.

jj2007

Quote from: clive on April 10, 2010, 07:17:52 PM
Quote from: lingo
   xchg   eax, [esp]

This instruction will absolutely murder performance, it is locked and atomic. You can measure the RAM access speed with this construct.

-Clive

Clive is right...

Intel(R) Celeron(R) M CPU        420  @ 1.60GHz (SSE3)
555     cycles for mov
2010    cycles for xchg


lingo

"This instruction will absolutely murder performance..."

Ok, corrected... :wink

clive

That has to win a prize for the nastiest, stack unsafe code I've seen in a while. :cheekygreen:
-Clive
It could be a random act of randomness. Those happen a lot as well.

lingo

"..for the nastiest, stack unsafe code.."

Don't talk about things you don't understand, and don't tell God how to run the world and how to write safety code in assembly... :naughty:

jj2007

Quote from: clive on April 10, 2010, 09:49:34 PM
That has to win a prize for the nastiest, stack unsafe code I've seen in a while. :cheekygreen:
-Clive

Can you explain why you consider it unsafe? The stack is your property, isn't it?
(I hate to defend lingo, I am just curious :bg)

clive

Anything below the stack pointer is fair game for the processor to use as it pleases. The operating system, or parts thereof, might also reasonably expect the user space stack to be usable.

-Clive
It could be a random act of randomness. Those happen a lot as well.

jj2007

Rumours (in this forum) say that was correct for old versions of Windows but nowadays the OS won't touch [esp-4] any more.

dedndave

i prefer not to use area under [esp] - i think it's a matter of time before something bad happens
this is a discussion we have had a few times before, but we shouldn't confuse poor Shawn any more than he already is   :bg

qWord

Quote from: dedndave on April 10, 2010, 11:19:11 PM
i prefer not to use area under [esp] - i think it's a matter of time before something bad happens
I've never problems when using this "dubious" technique . Also I don't think, that the OS (or whatever) will extract ESP from threads context to use memory below esp...
FPU in a trice: SmplMath
It's that simple!

GregL

In case we get more questions about this book "Introduction to 80x86 Assembly Language and Computer Architecture" Second Edition, the libraries and source code for it are here under Samples. The book is intended to be used with Visual Studio 2008 as the development environment. Also, even though the include files are .h files they are in MASM format.