News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Macros, mindless and merciless

Started by Mikl__, February 20, 2023, 06:44:44 PM

Previous topic - Next topic

Mikl__

Hey All!
Tell me how ... It is required to run 128 threads, each thread has almost the same procedure, but it's somehow reluctant to write them with pens. It seems to be very to place REPT macros k = 0
REPT 128
. . . < -- changing fragment where k occurs to be replaced by an increasing number
k=k + 1
endm
Thread procedure names ThreadProc0, ThreadProc1, ... , ThreadProc127. How to format thread procedures name using the REPT macro? ThreadProc&k ?

jj2007

Hi Mikl :thup:

k=0
REPEAT 10
thread$ CATSTR <chr$("ThreadProc>, %k, <")>
% echo thread$
push eax
invoke CreateThread, 0, 0, thread$, k+100, 0, esp
pop edx
k=k+1
ENDM


The % echo thread$ is just to check whether the string is constructed correctly (it does not create code).
Instead of k+100, you can pass any other lParam to the thread.

This works fine with the Masm32 SDK (or MasmBasic). However, I found out that the Masm64 SDK invoke macro doesn't handle the chr$() macro correctly, so you need some acrobatics:

k=0
REPEAT 10
thread$ CATSTR <chr$("ThreadProc>, %k, <")>
% echo ***************** thread$
push rax
mov rax, rsp ; rcx rdx r8 r9
push rax ; arg 6
push 0 ; arg 5
mov r9, k+100 ; arg 4
mov r8, thread$ ; arg3
xor edx, edx ; arg2
xor ecx, ecx ; arg1
call CreateThread
pop rdx
pop rdx
pop rdx
k=k+1
ENDM


Demo attached.

Mikl__


jj2007

Di niente, carissimo Mikl ;-)

Note that the demo attached above works, technically speaking. However, it does create ten threads in no man's land, where they certainly crash miserably. I am using x64dbg, which cannot look inside the threads. It's harmless, because ExitProcess kills them all, but still, don't expect anything useful from these threads.

Mikl__

Thanks everyone! Results can be seen in «Sleep Sort» and in «Spaghetti sort». Thanks again!

daydreamer

Quote from: Mikl__ on February 21, 2023, 11:44:08 PM
Thanks everyone! Results can be seen in «Sleep Sort» and in «Spaghetti sort». Thanks again!
Is spaghetti sort about sort out the right straw that leads to a kiss with a cute lady?  :biggrin:
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

Mikl__

Hi, daydreamer!
It is Spaghetti sorting
«Spaghetti sorting» ― belongs to the class of "Parallel sortings". Let's represent an array of unsorted numbers as a bunch of dry spaghetti of the appropriate length. Stand the spaghetti upright on a flat surface. A press is lowered on top of the spaghetti until it meets the longest pasta. Delete this spaghetti and insert it at the end of the (initially empty) array of sorted spaghetti. Each time the press touches the next pasta, we fix the next sorted element and remove it from the array of unsorted elements. Repeat until all spaghetti are sorted. Worst, average and best time complexity O(n)

jj2007

In practice, it might be a fast option to check simultaneously for the top and bottom:

.if eax>max
  mov destTop, eax
  inc destTop
  swap max, eax
.elseif eax<min
  mov destBottom, eax
  inc destBottom
  swap min, eax
.endif

Mikl__

Ciao jj!
Bubble sorting (+1) --> Shaker sorting (+2) --> Optimized Shaker Sort (+4)

jj2007

Quote from: Mikl__ on February 26, 2023, 09:01:36 PM
Booble sorting --> Shaker sorting --> Optimized Shaker Sort

Something went wrong when sorting the boobles :tongue:


Mikl__

#10
[delete]