Author Topic: MasmBasic  (Read 153297 times)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
Re: MasmBasic - a fast and easy-to-use library
« Reply #75 on: July 26, 2013, 08:03:43 AM »
After over 150 downloads, it seems time for an update...

Apart from fixing minor glitches, there are three significant changes in MasmBasic of 26.7.2013:

1. MasmBasic's debug macro has learned to display variables and registers also in hex and binary format:
        deb 1, "On loop entry:", al, ecx, $esi, xmm0, xmm1, ST, ST(5)        ; xmm in lowercase, FPU regs in uppercase
        deb 4, "#4 will show in the console:", xmm0, f:xmm0                ; display xmm0 as integer (default) and float with f: prefix
        deb 5, "#5 will be written to DebLog.txt:", ebx, $My$, $MyArray$(n)
        usedeb=0                        ; disable debugging completely (no code inserted - very handy...)
        deb 1, "This box will never pop up", eax
        usedeb=16                        ; force hex display
        deb 4, "Hexadecimal:", eax, xmm1, ST(3)                ; limited to 32 bits, i.e. low dword of xmm regs, FPU as int 32
        usedeb=2                        ; force binary display
        deb 4, "Binary:", eax, xmm1, ST(3)                ; limited to 32 bits
        usedeb=1                        ; decimal display (default)
        deb 4, "Multiple:", eax, x:eax, b:eax                ; override usedeb: show arguments in decimal, hexadecimal and binary format

        If you are still not desperate enough to launch Olly, give deb a try.
        Nothing is more powerful for bug-chasing.

2. ClearLocals replaces ClearLocalVariables and is now much simpler (and a lot faster, too):
        MyTest proc uses edi esi ebx arg1:DWORD, arg2:RECT
        LOCAL v1, v2, rc:RECT, buffer[100]:BYTE
          ClearLocals        ; first line after the LOCALs
          MsgBox 0, Str$("The value of v1: %i", v1), "Test clv:", MB_OK
          ret
        MyTest endp

        ClearLocals is fast, compact (5 bytes per call), and leaves all registers intact.

3. StackBuffer has become simpler, and you can use more than one buffer:
        MyTest proc uses edi esi ebx arg1:DWORD, arg2:RECT
        LOCAL rc:RECT, sbuf1, sbuf2, whatever[100]:BYTE
         ; optional: ClearLocals        ; first line after the LOCALs
          mov sbuf1, StackBuffer(100000)        ; allocate two fat buffers, and make sure
          mov sbuf2, StackBuffer(4000h)        ; they are 16-byte aligned for use with SSE2
          PrintLine "Start buffer 1:", Tb$, Hex$(sbuf1)
          PrintLine "Start buffer 2:", Tb$, Hex$(sbuf2)
          StackBuffer()        ; release second buffer
          StackBuffer()        ; release first buffer (sb without args = free the buffer)
          ret
        MyTest endp

   - buffer size is limited by start address of stack;
   - the start address is aligned to 16 bytes for use with SIMD instructions
   - can be combined with ClearLocals, but no zero-init performed for the stack buffer
   - StackBuffer does the stack probing for you

Download the library from the top of this thread.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
crtbuf and StackBuffer()
« Reply #76 on: July 29, 2013, 07:16:26 PM »
A small addition for version 29 July concerns the crtbuf macro (see also StackBuffer() above):
        crtbuf ThisExe$, MAX_PATH                        ; create a buffer in the uninitialised data section
        invoke GetModuleFileName, 0, ThisExe$, MAX_PATH        ; use it...
        crtbuf pBuffer, 1000000, 16                        ; create a 1 Mio bytes buffer in .data?, align 16 for use with SSE2

So SIMD fans can now specify 16-byte alignment (default is DWORD). Note that StackBuffer() is always aligned to 16 bytes but not zeroed.

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: MasmBasic - a fast and easy-to-use library
« Reply #77 on: July 30, 2013, 02:55:14 AM »
Jochen,

So SIMD fans can now specify 16-byte alignment (default is DWORD). Note that StackBuffer() is always aligned to 16 bytes but not zeroed.

good feature. It avoids a GPF in some cases. Thank you.  :t

Gunther
Get your facts first, and then you can distort them.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
Hex$ displays XMM and FPU registers
« Reply #78 on: August 05, 2013, 10:29:52 AM »
MasmBasic version 5 August 2013 is much improved on the deb and Hex$() macros. Example:

include \masm32\MasmBasic\MasmBasic.inc        ; download
        Init
        ; create a WORD array (byte, dword, qword, realX are also valid sizes):
        Dim MyW() As WORD
        mov MyW(0), 100        ; give it a start value
        xor ecx, ecx
        .Repeat
                mov ax, MyW(ecx)
                add ax, cx
                mov MyW(ecx+1), ax
                deb 6, "Fill a WORD array", ecx, MyW(ecx), MyW(ecx+1)        ; print the first 6 iterations to the console
                inc ecx
        .Until ecx>999

        Print Str$("\n%i loops OK\n", ecx)
        Print Str$("MyW(ecx-2)=%i\n", MyW(ecx-2))        ; show the last two values assigned
        Inkey Str$("MyW(ecx-1)=%i\n", MyW(ecx-1))
        Exit
end start


Output:
Fill a WORD array
ecx             0
MyW(ecx)        100
MyW(ecx+1)      100

Fill a WORD array
ecx             1
MyW(ecx)        100
MyW(ecx+1)      101

Fill a WORD array
ecx             2
MyW(ecx)        101
MyW(ecx+1)      103

Fill a WORD array
ecx             3
MyW(ecx)        103
MyW(ecx+1)      106

Fill a WORD array
ecx             4
MyW(ecx)        106
MyW(ecx+1)      110

Fill a WORD array
ecx             5
MyW(ecx)        110
MyW(ecx+1)      115

1000 loops OK
MyW(ecx-2)=38851
MyW(ecx-1)=39849


The Hex$() macro can now handle more sizes, including the 4 DWORDS of xmm registers, and has learned to display exotic formats, such as FPU and XMM registers:

include \masm32\MasmBasic\MasmBasic.inc        ; download
.data
MyByte        db 12h
MyWord        dw 1234h
MyDword        dd 12345678h
MyQword        dq 1234567812345678h
MyReal8        REAL8 1234567812345678r        ; r means the hex representation of a real number
MyPackedQwords dq 11aa22bb33cc44ddh, 55aa66bb77cc88ddh
        Init
        mov ecx, 12345678h
        PrintLine Hex$(cl)        ; reg8, 12 - same for dh, ah etc
        PrintLine Hex$(ch)        ; reg8, 12 - same for dh, ah etc
        PrintLine Hex$(cx)        ; reg16, 1234
        PrintLine Hex$(ecx)        ; reg32, 12345678
        PrintLine Hex$(123)        ; immediate
        PrintLine Hex$(MyByte)        ; global and local variables
        PrintLine Hex$(MyWord)
        PrintLine Hex$(MyDword)
        PrintLine Hex$(MyQword)        ; QWORD will be displayed with one space as 12345678 90123456
        movups xmm1, oword ptr MyPackedQwords
        PrintLine Hex$(xmm1)        ; if QWORD is not enough, get e.g. 11AA22BB 33CC44DD 55AA66BB 77CC88DD
        PrintLine Hex$(MyReal8)        ; same as QWORD
        fldpi
        PrintLine CrLf$, "PI=", Hex$(ST)        ; PI as 10-BYTE hex
        Let esi=CrLf$+"Result = "+Hex$(287454020)+"h"        ; in case you need the trailing h or a leading 0x, use Let or Cat$()
        Inkey esi
        Exit
end start


Output:
78
56
5678
12345678
0000007B
12
1234
12345678
12345678 12345678
11AA22BB 33CC44DD 55AA66BB 77CC88DD
12345678 12345678

PI=4000 C90FDAA2 2168C235

Result = 11223344h


If you find a problem, please let me know.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
Bugfix for displaying Hex$(xmmreg)
« Reply #79 on: August 12, 2013, 01:09:38 PM »
Version 5 August of MasmBasic displayed OWORDs in the incorrect order. The bug is fixed in version 12 August.
The reason, or rather: my bad excuse, is that I try to keep compatibility to good old MASM 6.15 - and the latter doesn't know about OWORDs. My workaround is usual to declare two QWORDs instead:

; MyO   OWORD 00112233445566778899aabbccddeeffh   ; no good for ML 6.15 and lower
; MyO   QWORD 0011223344556677, 8899aabbccddeeffh ; solution #0 - WRONG
MyO     QWORD 8899aabbccddeeffh, 0011223344556677 ; solution #1 - RIGHT


As you can easily guess, I had tested the new Hex$(xmm1) with solution #0 :redface:

Below a simple demo (the deb macro uses Hex$() internally):

include \masm32\MasmBasic\MasmBasic.inc        ; download
.data
O0        OWORD 0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFh
O1        OWORD 0FFFF99FFFFFFFFFFFFFF11FFFFFFFFFFh
O3        OWORD 0ffeeddccbbaa99887766554433221100h

        Init
        movups xmm0, O0
        movups xmm1, O1
        movups xmm3, O3
        movaps xmm2, xmm0        ; copy for pcmpeqb
        PCMPEQB xmm2, xmm1
        deb 4, "x0/1/2", x:xmm0, x:xmm1, x:xmm2, x:xmm3
        Print CrLf$, "16 bits", Tb$, Tb$, Tb$, "5432109876543210", CrLf$
        PMOVMSKB eax, xmm2        ; show in ax where xmm0 differs to xmm1
        xor ecx, ecx
        not ax        ; we are interested in bits set
        deb 4, "MSKB", b:ax
        bsr ecx, eax
        deb 4, "msb set", ecx
        bsf ecx, eax
        deb 4, "lsb set", ecx
        Inkey
        Exit
end start

Output:
x0/1/2
x:xmm0          FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
x:xmm1          FFFF99FF FFFFFFFF FFFF11FF FFFFFFFF
x:xmm2          FFFF00FF FFFFFFFF FFFF00FF FFFFFFFF
x:xmm3          FFEEDDCC BBAA9988 77665544 33221100

16 bits                 5432109876543210
MSKB    b:ax            0010000000100000
msb set ecx             13
lsb set ecx             5

Magnum

  • Member
  • *****
  • Posts: 2242
Re: MasmBasic - a fast and easy-to-use library
« Reply #80 on: August 12, 2013, 01:50:17 PM »
MasmBasic is a library that allows to use BASIC syntax in assembler, i.e. it is not a "separate" language but rather a library of macros and routines, fully compatible with the latest Masm32 SDK (version 11), MASM (from version 6.15 upwards, see e.g. version 8.0) and JWasm. The usual disclaimers apply - do not use for military purposes, in hospitals and anywhere else where buggy applications could cause damage. You have been warned 8)


I wish you had posted that earlier.

I used it to write a program that monitors C-eye-PAV.

I think you are safe though.

Andy
Take care,
                   Andy

Ubuntu-mate-16.04-desktop-amd64

http://www.goodnewsnetwork.org

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
Comparing OWORDs and QWORDs
« Reply #81 on: August 15, 2013, 10:58:39 AM »
Update 15 August (download):

Qcmp, Ocmp
.data        ; for testing
qSmall    qWORD 7700000000000001h
qBig      qWORD 7700000000000003h
oSmall    OWORD 77000000000000000000000000000001h
oBig      OWORD 77000000000000000000000000000003h        ; OWORD for JWasm and higher ML.exe versions
; oBig    qWORD 00000000000000003h, 7700000000000000h        ; 2 QWORDS for ML 6.15
.code
        Qcmp qBig, qSmall        ; compare two global variables
        mov ecx, offset oBig        ; use a pointer ...
        Ocmp ecx, oSmall        ; ... for one (or both) of them
        movups xmm0, OWORD PTR oSmall        ; even ML 6.15 understands that
        oqDeb=1        ; if this flag is set, Qcmp or Ocmp print e.g. "ecx greater xmm0" to console
        Ocmp ecx, xmm0        ; a pointer and an XMM reg (xmm0...xmm2 will be trashed)
        deb 4, "Result", flags        ; CzSo, i.e. Carry? and Sign? set
Rem
        - returns flags as in a cmp eax, edx comparison (control for overflow!)
        - trashes eax and edx, xmm0 and xmm1; do not use edx as input
        - you cannot use both ecx and ebx as input pointers (an error will be thrown)

## for comparing floats (REAL4, REAL8, REAL10): ##
Fcmp
        MyPI_hi        REAL4        3.14160
        ...
        Fcmp MyPI_hi, PI, medium        ; PI is what you think it is
        .if FcmpLesser
                Print Str$("MyPI_hi at %f is lower than the real PI\n", MyPI_hi)
        .elseif Zero?
                Print Str$("MyPI_hi at %f is exact\n", MyPI_hi)
        .else
                Print Str$("MyPI_hi at %f is higher than the real PI\n", MyPI_hi)
        .endif
Rem        - returns Zero? and Sign? flags (and only these are valid): Sign? means "first arg below second arg"
        - you may use FcmpGreater and FcmpLess (aka !Sign? and Sign?)
        - single arg, e.g. Fcmp xmm1, tests for zero
         - see also QCmp and Ocmp for comparing QWORDs and OWORDs
         - almost any number formats can be compared, including xmm registers etc

Magnum

  • Member
  • *****
  • Posts: 2242
Re: MasmBasic - a fast and easy-to-use library
« Reply #82 on: August 15, 2013, 11:43:38 AM »
MasmBasic is a library that allows to use BASIC syntax in assembler, i.e. it is not a "separate" language but rather a library of macros and routines, fully compatible with the latest Masm32 SDK (version 11), MASM (from version 6.15 upwards, see e.g. version 8.0) and


When I clicked on version 8, it said off limits.

Andy
Take care,
                   Andy

Ubuntu-mate-16.04-desktop-amd64

http://www.goodnewsnetwork.org

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
Re: MasmBasic - a fast and easy-to-use library
« Reply #83 on: August 15, 2013, 04:54:56 PM »
Thanks, Andy - I just corrected the link to Erol's post. Caution, though, when going to the M$ site:

Quote
The Microsoft Macro Assembler 8.0 (MASM) is a tool that consumes x86 assembly language programs and generates corresponding binaries.

Make a backup of your sources ;)

Or, much better idea, use JWasm - fully compatible with Masm32 and MasmBasic, and at least twice as fast as the latest M$ snails. RichMasm has now autodetection for JWasm:
   ; if OPT_Assembler not explicitly set, check if ML is old and JWasm is available
   .if Exist("\Masm32\Bin\ml.exe")
           .if Instr_(GfDate$(-1), "1999")
                   mov ecx, Chr$("\Masm32\bin\JWasm.exe")
                   .if Exist(ecx)
                           invoke lstrcat, edi, offset txOptAss
                   .else
                           MsgBox 0, Cat$("You need"+CrLf$+ecx), "Hi", MB_OK
                   .endif
           .endif
   .endif

dedndave

  • Member
  • *****
  • Posts: 8749
  • Still using Abacus 2.0
    • DednDave
Re: MasmBasic - a fast and easy-to-use library
« Reply #84 on: August 15, 2013, 05:01:13 PM »
PacMasm ?   :biggrin:


jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
PacMasm
« Reply #85 on: August 15, 2013, 05:20:57 PM »
PacMasm ?   :biggrin:
include \masm32\MasmBasic\MasmBasic.inc        ; download
        Init
        GetFiles \Masm32\*.asm
        push eax
        For_ ebx=0 To eax-1
                Kill Files$(ebx)
        Next

        Inkey Str$("Dave, wifey is waiting, and %i files are gone!!!!", stack)
        Exit
end start  :greensml:

Antariy

  • Member
  • ****
  • Posts: 541
Re: MasmBasic - a fast and easy-to-use library
« Reply #86 on: August 15, 2013, 06:11:27 PM »
:greensml:



C:\MASM32\>ML the_proper_code.asm
Microsoft (R) Macro Assembler Version 6.15.8803
Copyright (C) Microsoft Corp 1981-2000.  All rights reserved.

 Assembling: the_proper_code.asm
 Yum-yum!



C:\MASM32\>ML the_untidy_code.asm
Microsoft (R) Macro Assembler Version 6.15.8803
Copyright (C) Microsoft Corp 1981-2000.  All rights reserved.

 Assembling: the_untidy_code.asm
the_untidy_code.asm(9) : warning A4011: multiple .MODEL directives found : .MODEL ignored
 *sound of belch*


jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
Re: MasmBasic - a fast and easy-to-use library
« Reply #87 on: August 15, 2013, 06:17:32 PM »
Post the sources, Alex :biggrin:

Gunther

  • Member
  • *****
  • Posts: 3517
  • Forgive your enemies, but never forget their names
Re: MasmBasic - a fast and easy-to-use library
« Reply #88 on: August 16, 2013, 02:18:20 AM »
Jochen,

Post the sources, Alex :biggrin:

he will, he will. Please be patient.  :lol:

Gunther
Get your facts first, and then you can distort them.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7737
  • Assembler is fun ;-)
    • MasmBasic
Olly
« Reply #89 on: August 18, 2013, 11:11:05 AM »
Update 18 August concerns only MasmBasic's default editor, RichMasm (located in \Masm32\RichMasm\RichMasm.exe after installation):

include \masm32\MasmBasic\MasmBasic.inc        ; download
        Init
;       if RichMasm finds at least one int 3 that is not commented out, the editor
;       will launch \Masm32\OllyDbg\ollydbg.exe for the current exe with arguments
        int 3        ; Hit F9 in Olly, it will stop here
        Print "The commandline args are [", CL$(1), "] and [", CL$(2), "]"
        Exit
end start

RichMasm options for seeing symbols in Olly:
OPT_Assembler   JWasm         ; download - must be present in \bin (ML 6.15 works, too)
OPT_DebugA      /Zi           ; activate symbols
OPT_Linker      link          ; Masm32 standard, i.e. version 6.14, must be present in \bin
OPT_DebugL      /debug        ; for debugging
OPT_Arg1        "Hello coder"
OPT_Arg2        "what's cooking in the Masm32 forum?"


Note that Olly must be present as \Masm32\OllyDbg\ollydbg.exe

When Olly starts a console app, the console window will be in the foreground. This is rarely what you want, therefore RichMasm has now learned to move Olly to the foreground, so that you can directly hit F9 without having to click the bl**dy console away.