Masm32 SDK description, downloads and other helpful links

Main Menu

Macros, mindless and merciless

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

Previous topic - Next topic


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
Thread procedure names ThreadProc0, ThreadProc1, ... , ThreadProc127. How to format thread procedures name using the REPT macro? ThreadProc&k ?


Hi Mikl :thup:

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

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:

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

Demo attached.



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.


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


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
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding


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)


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


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


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: