Author Topic: Using _beginthreadex  (Read 2412 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6424
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Using _beginthreadex
« Reply #45 on: January 20, 2019, 08:11:47 AM »
It just means that the argument is required and if the arg is omitted you get an error.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 9523
  • Assembler is fun ;-)
    • MasmBasic
Re: Using _beginthreadex
« Reply #46 on: January 20, 2019, 08:28:19 AM »
REQ forces an error if the argument is not present. Here is a test piece showing some macro tricks.

Code: [Select]
include \masm32\include\masm32rt.inc

hi macro arg ; now add :req and see what it does
  print "hi ", arg
endm

hiwithdef macro arg:=<"folks"> ; you can use it without passing an argument but...
  print "hi ", arg
endm

test1 macro arg ; assemble time: generates code only for the relevant branch
  ifidn <arg>, <eax>
print "you passed eax", 13, 10
  else
print "you passed &arg", 13, 10
  endif
endm

test2 macro arg ; runtime: generates code for both branches
  .if arg==eax
print "you passed eax", 13, 10
  .else
print "you passed &arg", 13, 10
  .endif
endm

.code
start:
  hi "TBRANSO1"
  print chr$(13, 10)
  hi
  print chr$(13, 10)

  hiwithdef "TBRANSO1"
  print chr$(13, 10)
  hiwithdef
  print chr$(13, 10)


  test1 eax
  test1 something else

  test2 eax
  test2 ecx

  inkey "ok?"
  exit

end start

TBRANSO1

  • Member
  • **
  • Posts: 82
Re: Using _beginthreadex
« Reply #47 on: January 20, 2019, 08:49:49 AM »
REQ forces an error if the argument is not present. Here is a test piece showing some macro tricks.

Cool examples.

I am continually amazed at what MASM can do for lowly assembly that resembles stuff that I only thought was available in OOP languages.  I've placed C in Python or Ruby and it's a bitch and time consuming to do it, working with the original C-code bases and the FFI library. I may be prematurely saying this since I am new to this, but when folks say that it's not possible to do things in assembly except some number crunching here and there, obviously aren't aware of the capabilities you guys have designed along with the assemblers.  I've always said the smartest programmers aren't the guys doing Front End JS stuff, but the compiler, assembler writers.

I have another example that I made into a MACRO, and wondering what you thought of my implementation:
from MSDN there is

Code: [Select]
MAKEINTRESOURCEA(i) (LPSTR)((DWORD)((WORD)(i)))

I realize that this make be absolutely unnecessary in assembly but I made this:
take the lower word of the parameter, cast it to double word, then cast it to LPSTR

Code: [Select]
MAKEINTRESOURCE MACRO param:REQ
LOCAL lpcStr
.data
lpcStr LPCSTR ?
.code
and param, 000FFh
push param
pop lpcStr
EXITM <lpcStr>
ENDM

What are your thoughts?


hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6424
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Using _beginthreadex
« Reply #48 on: January 20, 2019, 09:37:18 AM »
 :biggrin:

> What are your thoughts?

Looks like a C compiler.  :P
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 9523
  • Assembler is fun ;-)
    • MasmBasic
Re: Using _beginthreadex
« Reply #49 on: January 20, 2019, 11:08:44 AM »
> and param, 000FFh

Are you sure it shouldn't be 0FFFFh?

TBRANSO1

  • Member
  • **
  • Posts: 82
Re: Using _beginthreadex
« Reply #50 on: January 20, 2019, 11:57:14 AM »
> and param, 000FFh

Are you sure it shouldn't be 0FFFFh?

You're right, I caught that afterward.  I had changed it in my base code already.

I had to change it a bit, since the assembler complained.

Code: [Select]
MAKEINTRESOURCE MACRO param:REQ
LOCAL lpStr
.data?
lpStr LPSTR ?
.code
xor eax, eax
mov eax, param
and eax, 0FFFFh
mov lpStr, eax
EXITM <lpStr>
ENDM

TimoVJL

  • Member
  • ***
  • Posts: 337
Re: Using _beginthreadex
« Reply #51 on: January 20, 2019, 07:24:21 PM »
In C MAKEINTRESOURCE macros don't generate code, only casts WORD value to LPSTR / LPWSTR.
Code: [Select]
#define MAKEINTRESOURCEA(i) ((LPSTR)((ULONG_PTR)((WORD)(i))))
#define MAKEINTRESOURCEW(i) ((LPWSTR)((ULONG_PTR)((WORD)(i))))
Code: [Select]
7: LPSTR lpStr = MAKEINTRESOURCEA(8001);
  [00000006] C745FC411F0000         mov               dword ptr [ebp-4],1F41h
8: LPWSTR lpWStr = MAKEINTRESOURCEW(8001);
  [0000000D] C745F8411F0000         mov               dword ptr [ebp-8],1F41h
UNREFERENCED_PARAMETER() macro works only with optimizing compilers, as they can remove an unused parameter.
May the source be with you

TBRANSO1

  • Member
  • **
  • Posts: 82
Re: Using _beginthreadex
« Reply #52 on: January 22, 2019, 06:54:17 AM »
Thanks Timo

Yes, I know it's most likely unnecessary but it was an exercise and and excuse to practice macros.