- 1 year ago
Multicore in NASM Windows: lpParameter data are wrong on entry
In NASM, when using multi-core threading in Windows, you need to ensure that you pass the correct data to the CreateThread
function. The lpParameter
argument is a pointer to the data that you want to pass to the thread function. Here's how you can correctly pass data to the thread function:
Allocate memory for the data that you want to pass to the thread function using the
malloc
function.Populate the allocated memory with the data that you want to pass to the thread function.
Pass the pointer to the allocated memory as the
lpParameter
argument to theCreateThread
function.In the thread function, cast the
lpParameter
back to the correct data type and access the data.
Here's a simple example of how you can do this in NASM:
assemblysection .bss
; Allocate memory for the data to be passed to the thread function
data_size equ 4
data_buffer resb data_size
section .text
global main
extern ExitProcess, CreateThread
main:
; Your program logic here
; Populate the data_buffer with the data you want to pass to the thread function
mov dword [data_buffer], 1234
; Create the thread and pass the pointer to the data_buffer as lpParameter
push 0 ; dwStackSize (use default)
push 0 ; dwCreationFlags (use default)
push thread_function
push data_buffer
push 0 ; dwThreadId (output)
call CreateThread
; Wait for the thread to finish (optional)
push 0xFFFFFFFF
call WaitForSingleObject
; Exit the program
push 0
call ExitProcess
thread_function:
; In the thread function, cast the lpParameter back to the correct data type
; and access the data
mov eax, [esp + 4] ; eax = lpParameter
mov eax, [eax] ; eax = data_buffer
; Your thread logic here
ret
Note: In this example, we're passing a 32-bit integer to the thread function. You can adjust the data_size
and data type based on your specific data needs.
Remember to free the memory allocated for the data_buffer when it's no longer needed to avoid memory leaks.