Author Topic: Macro goodies  (Read 5673 times)

jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Macro goodies
« 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.

Code: [Select]
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

  • Member
  • *****
  • Posts: 1454
  • The base type of a type is the type itself
    • SmplMath macros
Re: Macro goodies
« Reply #1 on: May 24, 2012, 01:12:53 AM »
Code: [Select]
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

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: Macro goodies
« Reply #2 on: May 24, 2012, 06:02:10 PM »
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:
Code: [Select]
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:
Code: [Select]
#define RGB(r,g,b)          ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))

An example:
Code: [Select]
invoke CreateSolidBrush, rgb(255,255,255)
Well Microsoft, here’s another nice mess you’ve gotten us into.

dedndave

  • Member
  • *****
  • Posts: 8734
  • Still using Abacus 2.0
    • DednDave
Re: Macro goodies
« Reply #3 on: May 24, 2012, 11:07:12 PM »
Code: [Select]
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:

Code: [Select]
      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

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: Macro goodies
« Reply #4 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.


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

jj2007

  • Member
  • *****
  • Posts: 7558
  • Assembler is fun ;-)
    • MasmBasic
Re: Macro goodies
« Reply #5 on: May 25, 2012, 02:30:19 AM »
I like it a bit flexible :biggrin:
Code: [Select]
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