News:

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

Main Menu

Macro goodies

Started by jj2007, May 24, 2012, 12:23:02 AM

Previous topic - Next topic

jj2007

It would be nice to have a thread on macros for newbies, so here is my attempt to start one.

include \masm32\include\masm32rt.inc
.code ; console assembly and link
start: ; so what do you expect...? Remember Ctrl C stops the printing ;-)
.While rv(GetTickCount) ; rv=retval macro from \Masm32\macros\macros.asm; GetTickCount yields #of ticks since boot time
print "*"
xor eax, eax
.Endw
inkey " ... and bye"
exit
end start


To understand the problem, you need Olly.
(no, really, you need Olly - not only for this problem 8))

qWord

a = 1
b = 2
IF a AND b              ; c-equivalent: a&b != 0
ELSE
    echo AND,OR and XOR are bit operators
ENDIF

IF a NE 0 AND b NE 0    ; c-eqvilaent: a && b
ENDIF
MREAL macros - when you need floating point arithmetic while assembling!

MichaelW

This is an RGB macro function that returns a COLORREF value calculated from the red, green, and blue components, entirely by the preprocessor so it gets encoded as a 32-bit constant:

rgb MACRO red, green, blue
    EXITM % blue SHL 16 + green SHL 8 + red
ENDM


It works like the RGB macro defined in the Microsoft WinGDI.h:

#define RGB(r,g,b)          ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))


An example:

invoke CreateSolidBrush, rgb(255,255,255)

Well Microsoft, here's another nice mess you've gotten us into.

dedndave

RGB2CR  MACRO   red,green,blue
        EXITM   % (blue SHL 16) OR (green SHL 8) OR red
        ENDM


i had written one, as well, Michael - lol
guess i could have AND'ed each argument with 255 - but i assume the user knows the limits

the one in the masm32 macros.asm file generates a bunch of code   :biggrin:

      RGB MACRO red, green, blue
        xor eax, eax
        mov ah, blue    ; blue
        mov al, green   ; green
        rol eax, 8
        mov al, red     ; red
      ENDM

MichaelW

I did a macro function version because the MASM32 macro procedure cannot be used directly in an invoke, and the lack of generated code was just a bonus.

Even though I have not found any problems with my version, I think your use of OR was probably a better choice than my use of +. If nothing else, it's easier to understand.


Well Microsoft, here's another nice mess you've gotten us into.

jj2007

I like it a bit flexible :biggrin:
include \masm32\MasmBasic\MasmBasic.inc ; http://masmforum.com/~masm32/board/index.php?topic=94
RgbCol MACRO red, green, blue ; like RGB but function
  if (opattr red)+(opattr green)+(opattr blue) eq 108
EXITM <red+green shl 8+blue shl 16> ; three immediates
  else
ifdifi <red>, <eax>
mov eax, red
endif
movzx eax, byte ptr al
mov edx, green
or ah, dl
mov edx, blue
movzx edx, dl
shl edx, 16
or eax, edx
EXITM <eax>
  endif
ENDM ; will be implement in the next release of MasmBasic

.data
memRed dd 0AAh
memGreen dd 0CCh
Init
RGB 0aah, 0bbh, 0cch ; Masm32
PrintLine Hex$(eax) ; output 00CCBBAA
PrintLine Hex$(RgbCol(0aah, 0bbh, 0cch))
mov eax, 0aah
mov ebx, 0bbh
mov ecx, 0cch
PrintLine Hex$(RgbCol(eax, ebx, ecx))
PrintLine Hex$(RgbCol(memRed, ebx, memGreen))
Inkey
Exit
end start


Output:
00CCBBAA
00CCBBAA
00CCBBAA
00CCBBAA