The MASM Forum

General => The Campus => Topic started by: Mikl__ on February 20, 2023, 06:44:44 PM

Title: Macros, mindless and merciless
Post by: Mikl__ on February 20, 2023, 06:44:44 PM
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 ?
Title: Re: Macros, mindless and merciless
Post by: jj2007 on February 20, 2023, 08:25:12 PM
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.
Title: Re: Macros, mindless and merciless
Post by: Mikl__ on February 20, 2023, 08:54:11 PM
Ciao jj2007!
Mille grazie!
Title: Re: Macros, mindless and merciless
Post by: jj2007 on February 20, 2023, 09:03:52 PM
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.
Title: Re: Macros, mindless and merciless
Post by: Mikl__ on February 21, 2023, 11:44:08 PM
Thanks everyone! (https://wasm.in/styles/smiles_s/thank_you.gif) Results can be seen in «Sleep Sort» (http://wasm.in/threads/sortirovki-s-animaciej.34674/page-5#post-436533) and in «Spaghetti sort» (http://wasm.in/threads/sortirovki-s-animaciej.34674/page-5#post-436534). Thanks again!
Title: Re: Macros, mindless and merciless
Post by: daydreamer on February 26, 2023, 06:03:15 PM
Quote from: Mikl__ on February 21, 2023, 11:44:08 PM
Thanks everyone! (https://wasm.in/styles/smiles_s/thank_you.gif) Results can be seen in «Sleep Sort» (http://wasm.in/threads/sortirovki-s-animaciej.34674/page-5#post-436533) and in «Spaghetti sort» (http://wasm.in/threads/sortirovki-s-animaciej.34674/page-5#post-436534). Thanks again!
Is spaghetti sort about sort out the right straw that leads to a kiss with a cute lady?  :biggrin:
Title: Re: Macros, mindless and merciless
Post by: Mikl__ on February 26, 2023, 08:47:41 PM
Hi, daydreamer!
It is Spaghetti sorting (https://wasm.in/threads/sortirovki-s-animaciej.34674/page-6#post-436561) (https://wasm.in/styles/smiles_s/mosking.gif)
«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)
Title: Re: Macros, mindless and merciless
Post by: jj2007 on February 26, 2023, 08:57:32 PM
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
Title: Re: Macros, mindless and merciless
Post by: Mikl__ on February 26, 2023, 09:01:36 PM
Ciao jj!
Bubble sorting (+1) --> Shaker sorting (+2) --> Optimized Shaker Sort (+4)
Title: Re: Macros, mindless and merciless
Post by: jj2007 on February 26, 2023, 09:03:56 PM
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:

(https://akns-images.eonline.com/eol_images/Entire_Site/2014822/rs_600x600-140922141755-600.3boobs.cm.92214_copy.jpg?fit=around%7C1200:1200&output-quality=90&crop=1200:1200;center,top)
Title: Re: Macros, mindless and merciless
Post by: Mikl__ on February 26, 2023, 09:09:52 PM
[delete]