The MASM Forum Archive 2004 to 2012

General Forums => The Laboratory => Topic started by: ollydbg on June 07, 2006, 11:26:52 AM

Title: My useful macros
Post by: ollydbg on June 07, 2006, 11:26:52 AM
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 ...
Title: Re: My useful macros
Post by: Mark Jones on June 07, 2006, 06:40:29 PM
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...
Title: Re: My useful macros
Post by: ollydbg on June 08, 2006, 12:12:24 AM
Why don't you use printf,it's better than StdOut ?
Title: Re: My useful macros
Post by: Mark Jones on June 08, 2006, 11:43:38 PM
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
Title: Re: My useful macros
Post by: MichaelW on June 09, 2006, 01:25:12 PM
Windows on an 8088 ??


Title: Re: My useful macros
Post by: hutch-- on June 09, 2006, 01:27:54 PM
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.
Title: Re: My useful macros
Post by: Mark Jones on June 10, 2006, 12:25:16 AM
Quote from: MichaelW on June 09, 2006, 01:25:12 PM
Windows on an 8088 ??

Well, look at the bottom of the page which Mnemonic found:
http://www.masm32.com/board/index.php?topic=4975.0
Title: Re: My useful macros
Post by: ollydbg on June 10, 2006, 02:00:03 PM
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>

(http://photo2.hexun.com/p/2006/0610/26247/m_0FE1F3010A845F80.jpg%3Cbr%20/%3E)
Title: Re: My useful macros
Post by: Max_Power on June 10, 2006, 06:55:41 PM
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.
Title: Re: My useful macros
Post by: ollydbg on June 11, 2006, 12:48:12 AM
Example:

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

But StdOut can not print numbers  ...
Title: Re: My useful macros
Post by: Ossa on June 11, 2006, 01:09:41 AM
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
Title: Re: My useful macros
Post by: ollydbg on June 11, 2006, 02:32:17 AM
Yes ,you are right.
Title: Re: My useful macros
Post by: Casper on June 11, 2006, 10:18:08 PM
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
Title: Re: My useful macros
Post by: Ossa on June 11, 2006, 11:41:04 PM
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
Title: Re: My useful macros
Post by: Rockphorr on June 13, 2006, 11:34:05 AM
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
Title: Re: My useful macros
Post by: Roger on June 13, 2006, 02:04:20 PM
Hi All,

Windows 2.10 needed a 80286 CPU.

I have the original floppies in front of me and the wording on the disks reads:-


MicrosoftR Windows/286
--------------------------------------------------------------------------------------------
Presentation Manger
For Personal Computers Using an IntelR 80826 Processor

     AV 2.10 / 024373
Copyright 1985-1988. Microsoft Corporation. All rights reserved.


I don't remember getting it to run on my XT clone but this was because I could not strip it down enough to run on a twin floppy machine and have any space on the disks for an application program.
I didn't actually use it on my AT either because as an API it was only extra work to run the same DOS programmes and as for the graphics, well they streched the capabilities of a CGA screen - just.

Regards Roger