May 07, 2003

Clint Pierce is my hero

Clint Pierce is my hero. Below, hopefully not included inline here, is an example of how to clear the screen, under Windows, in Parrot assembly. And yes, I know, printing an escape sequence would be easier, but hitting the Win32 API directly, with no extensions or windows-specific code, is just, well, damn cool.

I shall have to do something with OS X and Cocoa, I expect, though that will await objects...

        new P25, .PerlHash
        bsr MAIN
CONSOLE_SETUP:
        loadlib P24, "kernel32.dll"
        dlfunc P0, P24, "GetStdHandle", "pi"
        set I0, 1
        set I5, -11
        invoke
        set P25["kernel32"], P24
        set P25["handle_as_*"], P5
        new P0, .PerlHash
        set P25["console"], P0
        dlfunc P0, P24, "GetStdHandle", "ii"
        set I0, 1
        set I5, -11
        invoke
        set P25["handle_as_int"], I5
        ret
CONSOLE_INFO:
        set P24, P25["kernel32"]
        dlfunc P0, P24, "GetConsoleScreenBufferInfo", "iip"
        set I5, P25["handle_as_int"]
        new P5, .UnManagedStruct
        set P5, 32
        set I0, 1
        invoke
        set P0, P25["console"]
        set I0, 0               # dwSize.X
        bsr UMS_GET_SHORT
        set P0["xbuf"], I1
        print "X is "
        print I1
        set I0, 2               # dwSize.Y
        bsr UMS_GET_SHORT
        set P0["ybuf"], I1
        print "Y is "
        print I1
        print "\n"
        set I0, 4               # dwCursorPosition.X
        bsr UMS_GET_SHORT
        set P0["curx"], I1
        set I0, 6               # dwCursorPosition.Y
        bsr UMS_GET_SHORT
        set P0["cury"], I1
        ret

CONSOLE_HOME:
        set P1, P25["console"]
        set P24, P25["kernel32"]
        dlfunc P0, P24, "SetConsoleCursorPosition", "ipi"
        set I0, 1
        set P5, P25["handle_as_*"]
        set I5, 0
        invoke
        ret

CONSOLE_CLEAR:
        set P1, P25["console"]
        set P24, P25["kernel32"]                       # 87655
        dlfunc P0, P24, "FillConsoleOutputCharacterA", "iicilp"
        set I0, 1

        new P5, .UnManagedStruct
        set P5, 8

        set I5, 0                       # Coords
        set I1, P1["xbuf"]
        set I2, P1["ybuf"]
        mul I6, I1, I2                  # Length
        set I7, 32                      # Char
        set I8, P25["handle_as_int"]    # Handle

        invoke
        ret

DUMP_BUF5:
        set I0, 0
DUMP_BUF_LOOP5:
        eq I0, I1, DUMP_BUF_END5
        print I0
        print " "
        set I2, P5[I0]
        print I2
        print "\n"
        inc I0
        branch DUMP_BUF_LOOP5
DUMP_BUF_END5:
        ret

WIN32_ERROR:
        set P24, P25["kernel32"]
        dlfunc P0, P24, "GetLastError", "lv"
        set I0, 1
        invoke
        print "Error number: "
        print I5
        print "\n"
        ret

        # P5 UnManagedStruct
        # I0 offset in UMS
        # I1 return
UMS_GET_SHORT:
        pushi
        set I2, P5[I0]
        inc I0
        set I3, P5[I0]
        shl I3, I3, 8
        add I3, I3, I2
        save I3
        popi
        restore I1
        ret

MAIN:   bsr CONSOLE_SETUP
        bsr CONSOLE_INFO
        bsr CONSOLE_CLEAR
        bsr CONSOLE_HOME
        end
Posted by Dan at May 7, 2003 04:42 PM | TrackBack (0)
Comments

To be perfectly fair, escape sequences won't work in modern-day Win32 Consoles (those running CMD.EXE). And that's the architecture that I have at my disposal day in, and day out. So I tend to code for that and backport to Unixy stuff. Reports are though that the ANSI compatability stuff works fine in Cygwin, OSX, Linux, and Win32.

So the BASIC Chess program I'm porting at the moment should look perfectly fine in Win32 and ANSI terminals...

Posted by: Clinton at May 13, 2003 11:44 PM