The MASM Forum

General => The Campus => Topic started by: jj2007 on May 24, 2012, 12:23:02 AM

Title: Macro goodies
Post by: jj2007 on May 24, 2012, 12:23:02 AM
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 (http://www.ollydbg.de/version2.html).
(no, really, you need Olly - not only for this problem 8))
Title: Re: Macro goodies
Post by: qWord on May 24, 2012, 01:12:53 AM
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
Title: Re: Macro goodies
Post by: MichaelW on May 24, 2012, 06:02:10 PM
This is an RGB macro function that returns a  COLORREF (http://msdn.microsoft.com/en-us/library/dd183449%28v=VS.85%29.aspx) 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)

Title: Re: Macro goodies
Post by: dedndave on May 24, 2012, 11:07:12 PM
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
Title: Re: Macro goodies
Post by: MichaelW on May 25, 2012, 01:59:11 AM
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.


Title: Re: Macro goodies
Post by: jj2007 on May 25, 2012, 02:30:19 AM
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