News:

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

Main Menu

Using _beginthreadex

Started by TBRANSO1, January 11, 2019, 06:05:16 AM

Previous topic - Next topic

hutch--

It just means that the argument is required and if the arg is omitted you get an error.

jj2007

REQ forces an error if the argument is not present. Here is a test piece showing some macro tricks.

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

Quote from: jj2007 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.

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


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


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--

 :biggrin:

> What are your thoughts?

Looks like a C compiler.  :P

jj2007

> and param, 000FFh

Are you sure it shouldn't be 0FFFFh?

TBRANSO1

Quote from: jj2007 on January 20, 2019, 11:08:44 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.


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

In C MAKEINTRESOURCE macros don't generate code, only casts WORD value to LPSTR / LPWSTR.#define MAKEINTRESOURCEA(i) ((LPSTR)((ULONG_PTR)((WORD)(i))))
#define MAKEINTRESOURCEW(i) ((LPWSTR)((ULONG_PTR)((WORD)(i))))

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

Thanks Timo

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