News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

My useful macros

Started by ollydbg, June 07, 2006, 11:26:52 AM

Previous topic - Next topic

ollydbg

printLastError macro
   LOCAL fmt
   .CONST
   fmt db "%d %XH",10,0
   .CODE
   PUSHAD
   invoke GetLastError
   invoke printf,offset fmt,eax,eax
   POPAD
endm

ple equ printLastError

You can use :
ple
to see an API's error.
There are more ...

Mark Jones

Hello Ollydbg. Doumo Arigatou! Here's a macro for printing console text in color. It could be changed to accept any number or combinations of parameters but I'll leave that as a learning excercise. :bg


printcolor macro fg:REQ, bg:REQ, arg1:REQ, any_text:VARARG
    mov eax,bg                          ; color values are 4-bit IRGB
    shl eax,4                           ; bit3=int, 2=R, 1=G, 0=B
    or  eax,fg                          ; 1001b == bright blue
    invoke SetConsoleTextAttribute,hStdOut,eax
    invoke StdOut,reparg(arg1)          ; print dword offset
      ifnb <any_text>                   ; if literal text,
        invoke StdOut,chr$(any_text)    ; display that too
      endif                             ;
    mov eax,7                           ; color back to white
    invoke SetConsoleTextAttribute,hStdOut,eax
endm



.data
    szRed   db 'Red',0
    szWhite db 'White',0
    szBlue  db 'Blue',0
.code
    print chr$(" Flag of America: ")
    printcolor 0100y,0y,addr szRed,", "  ; "y" signifies Binary Notation
    printcolor 1111y,0y,addr szWhite,", and "
    printcolor 0001y,0y,addr szBlue,"..."


Quote from: AMD XP 2500+
  Flag of America: Red, White, and Blue...
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

ollydbg

Why don't you use printf,it's better than StdOut ?

Mark Jones

Hi, I don't use printf primarily because it uses MSVCRT.DLL, and older systems may not have that library. (Some members here are running code on 386's and even 8088's.) :U
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

MichaelW

eschew obfuscation

hutch--

I used to know this young guy from New Zealand who ran win95 OEM on a 33 meg 486sx and could get masm32 to run on it but I gather it was hard work just to get it to boot and it was endlessly swapping to the swap file.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Mark Jones

"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

ollydbg

A little improvement:

colorprintf macro foreground_color,background_color,text
    invoke GetStdHandle,STD_OUTPUT_HANDLE
    push eax
    ;-----
    mov edx,background_color            ; color values are 4-bit IRGB
    shl edx,4                           ; bit3=int, 2=R, 1=G, 0=B
    or  edx,foreground_color            ; 1001b == bright blue
    invoke SetConsoleTextAttribute,eax,edx
    invoke printf,text         ; print dword offset
    ;------
    pop eax   
    invoke SetConsoleTextAttribute,eax,7
endm


Example:

colorprintf 1110y,11y,<"num:%d\n",10>


Max_Power

ollydbg, can you explain how it is any better? What is the benefit of printf over StdOut? I don't see needing MSVCRT.DLL as a benefit.

ollydbg

#9
Example:

printf("The number is:%d",20);

But StdOut can not print numbers  ...

Ossa

Quote from: ollydbg on June 11, 2006, 12:48:12 AM
But StdOut can not print numbers and \n ...

I always thought that "\n" was converted by a C compiler, not by printf - have I been wrong on this?

Ossa
Website (very old): ossa.the-wot.co.uk

ollydbg


Casper

How do you explain this then, it seems to be able to do any characters...

QuoteThe ReadConsoleOutput function copies a rectangular block of character and color attribute data from a console screen buffer into a destination buffer. The function treats the destination buffer as a two-dimensional array of CHAR_INFO structures. Similarly, the WriteConsoleOutput function copies a rectangular block of character and color attribute data from a source buffer to a console screen buffer. For more information about reading from or writing to rectangular blocks of screen buffer cells, see Input and Output Methods.

The following example uses the CreateConsoleScreenBuffer function to create a new screen buffer. After the SetConsoleActiveScreenBuffer function makes this the active screen buffer, a block of characters and color attributes is copied from the top two rows of the SDTOUT screen buffer into a temporary buffer. The data is then copied from the temporary buffer into the new active screen buffer. When the application is finished using the new screen buffer, it calls SetConsoleActiveScreenBuffer to restore the original STDOUT screen buffer.

#include <windows.h> 

VOID main(void) {
    HANDLE hStdout, hNewScreenBuffer;
    SMALL_RECT srctReadRect;
    SMALL_RECT srctWriteRect;
    CHAR_INFO chiBuffer[160]; // [2][80];
    COORD coordBufSize;
    COORD coordBufCoord;
    BOOL fSuccess;

    /*
     * Get a handle of the STDOUT screen buffer to copy from and
     * create a new screen buffer to copy to.
     */

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    hNewScreenBuffer = CreateConsoleScreenBuffer(

       GENERIC_READ |           /* read-write access      */
       GENERIC_WRITE,
       0,                       /* not shared             */
       NULL,                    /* no security attributes */
       CONSOLE_TEXTMODE_BUFFER, /* must be TEXTMODE       */
       NULL);                   /* reserved; must be NULL */
    if (hStdout == INVALID_HANDLE_VALUE ||
            hNewScreenBuffer == INVALID_HANDLE_VALUE)
        MyErrorExit("CreateConsoleScreenBuffer");


    /* Make the new screen buffer the active screen buffer. */

    if (! SetConsoleActiveScreenBuffer(hNewScreenBuffer) )
        MyErrorExit("SetConsoleActiveScreenBuffer");

    /* Set the source rectangle. */

    srctReadRect.Top = 0;    /* top left: row 0, col 0  */
    srctReadRect.Left = 0;
    srctReadRect.Bottom = 1; /* bot. right: row 1, col 79 */
    srctReadRect.Right = 79;

    /* The temporary buffer size is 2 rows x 80 columns. */


    coordBufSize.Y = 2;
    coordBufSize.X = 80;

    /*
     * The top left destination cell of the temporary buffer is
     * row 0, col 0.
     */

    coordBufCoord.X = 0;
    coordBufCoord.Y = 0;

    /* Copy the block from the screen buffer to the temp. buffer. */

    fSuccess = ReadConsoleOutput(
       hStdout,        /* screen buffer to read from       */
       chiBuffer,      /* buffer to copy into              */
       coordBufSize,   /* col-row size of chiBuffer        */

       coordBufCoord,  /* top left dest. cell in chiBuffer */
       &srctReadRect); /* screen buffer source rectangle   */
    if (! fSuccess)
        MyErrorExit("ReadConsoleOutput");

    /* Set the destination rectangle. */

    srctWriteRect.Top = 10;    /* top lt: row 10, col 0 */
    srctWriteRect.Left = 0;
    srctWriteRect.Bottom = 11; /* bot. rt: row 11, col 79 */
    srctWriteRect.Right = 79;

    /* Copy from the temporary buffer to the new screen buffer. */


    fSuccess = WriteConsoleOutput(
        hNewScreenBuffer, /* screen buffer to write to    */
        chiBuffer,        /* buffer to copy from          */
        coordBufSize,     /* col-row size of chiBuffer    */
        coordBufCoord, /* top left src cell in chiBuffer  */
        &srctWriteRect); /* dest. screen buffer rectangle */
    if (! fSuccess)
        MyErrorExit("WriteConsoleOutput");
    Sleep(10000);

    /* Restore the original active screen buffer. */


    if (! SetConsoleActiveScreenBuffer(hStdout))
        MyErrorExit("SetConsoleActiveScreenBuffer");

}

Casper

Ossa

#13
QuoteHow do you explain this then, it seems to be able to do any characters...

I'm not too sure that I understand, could you perhaps explain further Casper?

Ossa
Website (very old): ossa.the-wot.co.uk

Rockphorr

Quote from: MichaelW on June 09, 2006, 01:25:12 PM
Windows on an 8088 ??

Yes, Windows 1.0 or Windows 2.1 on IBM PC XT
Strike while the iron is hot - Бей утюгом, пока он горячий