Author Topic: MasmBasic  (Read 439427 times)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
For each x$ in My$() and Val("12, 45, 67", 3)
« Reply #330 on: May 14, 2016, 02:17:03 PM »
MasmBasic version 14 May 2016 has two new features:

1. For each x$ in My$():
See demo here. Works with arrays of strings, integers, real numbers and structures (created e.g. with Dim myrc() As RECT)

2. Val("12, 45, 67", pos):
Imagine you have a line of settings in some ini file, and you want to know the value of the sixth entry... cumbersome, right?

include \masm32\MasmBasic\MasmBasic.inc      ; download
  SetGlobals settings$="0, 0, 1366, 768, 3, 123, 456, 789"
  Init
  Inkey Str$("The value of the 6th number is %i", Val(settings$, 6))
EndOfCode

Output:
The value of the 6th number is 123

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
MasmBasic & Windows 10
« Reply #331 on: May 26, 2016, 12:46:49 PM »
Following this thread about the Rich Edit control, and unexpected error messages in Windows 10, RichMasm got a minor revision of the way it loads RichEd20.dll

Please reinstall. The new function is available to MasmBasic users as invoke MbLoadRich. A simple window (<50 lines) with a Rich Edit control is attached; check the WM_CREATE handler, as shown below. In a nutshell: If you have MS Office or the free Word Viewer installed, loading a fat RTF file will be a factor 30-50 faster.

Code: [Select]
WndProc proc uses esi edi ebx hWnd, uMsg, wParam:WPARAM, lParam:LPARAM
  SWITCH uMsg
  CASE WM_CREATE
      ; picks the best RichEd20.dll available, or, if available, the one in the current folder:
      invoke MbLoadRich
      invoke CreateWindowEx, WS_EX_CLIENTEDGE, chr$("RichEdit20A"), NULL,
        WS_CHILD or WS_VISIBLE or WS_BORDER or WS_VSCROLL or ES_MULTILINE,
        9, 9, 570, 352, hWnd, 103, wcx.hInstance, NULL
ExternDef RichEditUsed:DWORD ; optional: check which version was installed
xchg eax, ecx
SetWin$ ecx=Cat$("This control was created using "+CrLf$+RichEditUsed)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #332 on: June 22, 2016, 09:06:47 AM »
Version 22 June 2016 features some improvements of the RichMasm editor (see here), plus one new LineCount() macro and an improvement of PopCount().

LineCount - pretty fast, see timings
      mov ecx, LineCount(pBuffer)                ; count carriage returns (Ascii 13, 0Dh)
      mov ecx, LineCount(pBuffer, 1000)          ; count CRs in the first 1000 bytes of the buffer
      mov ecx, LineCount(pBuffer, 1000, lf)      ; count linefeeds (Ascii 10, 0Ah)
      mov ecx, LineCount(pBuffer, 1000, "a")     ; count the char a
Rem   - returns DWORD in eax
      - use instead of EM_EXLINEFROMCHAR (which returns wrapped lines, not very useful for coding)


The new PopCount() allows limiting the count to 16 or 8 bits (this is not documented in MbGuide.rtf; note that PopCount() runs on all CPUs and is almost as fast as the native popcnt instruction that you may find on recent CPUs):

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init

  mov ebx, 88888888h
  PrintLine Str$("In ebx, %i bits are set: ", PopCount(ebx)), Bin$(ebx)
  mov ebx, 8888h
  Print Str$("In bx, %i bits are set\n", PopCount(16:ebx))
  mov ebx, 88h
  Print Str$("In bl, %i bits are set\n\n", PopCount(8:ebx))

  mov esi, cfm$("\nThis\nstring\nhas\nplenty\nof\nlines,\nbut\ncounting\nthem\nis\neasy")
  Inkey "The string", esi, Str$("\nhas %i lines", LineCount(esi))

EndOfCode


Output:
Code: [Select]
In ebx, 8 bits are set: 10001000100010001000100010001000
In bx, 4 bits are set
In bl, 2 bits are set

The string
This
string
has
plenty
of
lines,
but
counting
them
is
easy
has 11 lines

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
MasmBasic interpreter
« Reply #333 on: June 30, 2016, 10:58:18 AM »
MasmBasic version 30 June has some improvements under the hood. Attached is a proof of concept for an "interpreter" - launch it, and edit the prefilled text.

Other changes regard the editor. A new option:
Code: [Select]
OPT_Cls 0 ; 0=do not clear the console when building a new version
Default remains "clear the console when building".

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #334 on: July 03, 2016, 11:04:32 AM »
Inspired by the A close to authodox API Window thread, MasmBasic and RichMasm can now build 64-bit sources - example attached.

Inter alia, Vasily Sotnikov's includes and libs are required in \Masm64\Include and \Masm64\Lib
Plus, you need \Masm64\bin\ml64.exe etc

Note that the attached *.asc file, which opens in RichMasm, is the complete project. The resource file is built "on the fly", see the two red Rsrc at the end, while the x64 xml file is now included in the MasmBasic package. The icon files in \Masm32\MasmBasic\icons work fine with x64 8)

One of RichMasm's nice features is that an int 3 triggers the debugger when building a project. This works now with the x64 debugger, too. See the WM_CLOSE message for an example.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Dual 64/32-bit assembly
« Reply #335 on: July 16, 2016, 11:40:01 AM »
MasmBasic update of 17 July 2016 has two new templates for "dual 32/64 building". The OPT_64 switch determines which version to use. A standard Masm32 installation is enough, no additional libraries required.

Here is the simpler console version:

include \Masm32\MasmBasic\Res\JBasic.inc
; ### simple console demo, assembles in 32- or 64-bit mode with ML64, AsmC, JWasm, HJWasm ###
  j@start     
  Print "This code was assembled with "      ; OPT_64 1      ; put 0 for 32 bit, 1 for 64 bit assembly
  Print Chr$(@AsmUsed$(1))
  ifidn @Environ(o64), <1>
      Print Chr$(" in 64-bit format", 13, 10)
  else
      Print Chr$(" in 32-bit format", 13, 10)
  endif
  Print "Print Str$(123), CrLf: "
  if 0
      Print Str$(123), 13, 10      ; not possible
  else
      PrintLine Str$(123)      ; much better ;-)
  endif
  Print "Type a number: "
  mov rsi, Input$()
  Print Str$("The value of your number is %i\n", Val(rsi))
  Print "64-bit assembly is easy, it seems..."
  j@end


; activate your favourite debugger by eliminating the x, then insert int 3 somewhere in your code and hit F6
OxPT_DebPath   \Masm64\arkdasm\arkDasm.exe
OxPT_DebPath   \Masm64\x64Dbg\release\x64\x64dbg.exe

Current macros available for 64-bit code (the absolute minimum for the usual homework request...):
Code: [Select]
jinvoke apiarg, args:VARARG
rv FuncName:REQ,args:VARARG
repargA rarg
Chr$ any_text:VARARG
Hex$ arg
Str$ sform$, sNum0:VARARG
Input$
Val arg$
Print args
PrintLine args
@AsmUsed$()
Err$()

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Dual 64/32-bit assembly
« Reply #336 on: July 30, 2016, 03:16:40 AM »
Side-by-side assembly ;)

(in CreateWindowEx, the @64 variable sets the x coordinate to a different position)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #337 on: August 06, 2016, 08:34:49 AM »
Update 6 August features Inkey and deb macros for 64-bit code - see menu File/New Masm code:
Dual 32/64 bit console/GUI   templates that compile both as 64 and 32-bit applications

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #338 on: August 07, 2016, 07:14:39 PM »
Minor update 8 August: SetListBox got more options; default options are fast but produce more code. Standard example:

include \masm32\MasmBasic\Res\MbGui.asm
  GuiControl MyLb, "listbox", y 60, h 940
  GuiControl MyEd, "static", text "Double-click to open a file - Дважды щелкните, чтобы открыть файл", h 50
  GetFiles *.as?|*.rc      ; asc, asm, rc
  SortFiles                ; show latest files on top
  SetListbox Files$()      ; Fill the listbox with UTF-8 encoded file names
  Event Command
  .if NotifyCode==LBN_DBLCLK
      uShEx LbSel$         ; open selected file with ShellExecuteW (u=assume UTF-8 for LbSel$)
  .endif
GuiEnd


For a speed test, see http://masm32.com/board/index.php?topic=5573.0

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #339 on: August 15, 2016, 10:24:24 AM »
Update 15 August:
- SetListbox may now take a callback function to decide if a string in a given array should be added
- Under menu File/New Masm source, a new template "controls" was added, inter alia with a SetListBox example

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #340 on: August 30, 2016, 11:08:02 AM »
Update 30 August:

- more complete dual 64/32 bit include files (no external libraries required)

- GetHash function:

include \masm32\MasmBasic\MasmBasic.inc
      Init
      Let esi=FileRead$("\Masm32\qEditor.exe")
      .if GetHash(esi, LastFileSize, sha)     ; FileRead$ sets LastFileSize, also for binary files
            PrintLine "The SHA of qEditor.exe is   ", Tb$, Hex$(xmm0), " ", Hex$(ecx)
      .else
            PrintLine "Hashing failed: ", Err$()
      .endif
      void GetHash(esi, LastFileSize)
      mov ecx, edx                              ; fifth word returned in edx
      Print "Fast MD5 of qEditor.exe is ", Tb$, Hex$(xmm0), CrLf$
      GetHashRev=1                              ; use official MD5 byte order
      .if GetHash(FileRead$("\Masm32\qEditor.exe"))   ; MD5, len of string will be calculated
            PrintLine "qEditor, byte order MD5 specs: ", Tb$, Hex$(xmm0)
      .endif
EndOfCode


- returns success (1) or failure (0) in eax
- four dwords are returned in xmm0
- in case of SHA, the fifth dword will be in edx
- results can be displayed as a Hex$(xmm0), plus Hex$(edx) for SHA. The byte order will be reversed, compared to
  online hash calculators. You may use GetHashRev=1 to follow the MD5 specs, but more code will be generated
- with only one para, GetHash calculates the length; this is meaningful only for text files

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Bug warning for MasmBasic
« Reply #341 on: August 31, 2016, 11:02:44 AM »
Attention, the 64-bit examples in version 30 August choked in Win10. Fixed for version 31 August - my apologies :redface:

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #342 on: September 10, 2016, 10:53:32 AM »
Version 10 September 2016 sees one major change: Instead of JWasm, the RichMasm IDE will now install HJWasm32 if it can't find it in \Masm32\bin

You can test this feature by
- renaming \Masm32\bin\HJWasm32.exe to HJWasm32_old.exe
- during MasmBasic installation, hitting F6 while the help file is shown

Feedback welcome - and thanks a lot to Habran and Johnsa for their hard work :t

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Alloc16 for SSE
« Reply #343 on: September 20, 2016, 10:13:02 AM »
Version 20 September 2016 introduces two new macros, Alloc16 and Free16:

include \masm32\MasmBasic\MasmBasic.inc      ; download
      Init
      Dim PtrSSE() As DWORD
      For_ ct=0 To A16Max-1      ; 100 aligned pointers
            Alloc16 Rand(10000)
            mov PtrSSE(ct), eax
            Print Hex$(al), " "
      Next
      For_ ct=0 To A16Max-1
            Free16 PtrSSE(ct)
      Next
      EndOfCode
Rem      for use with SSE instructions that require 16-bit alignment


Typical output:
Code: [Select]
F0 D0 B0 B0 80 30 50 00 D0 D0 10 20 70 10 90 70 40 50 70 60 B0 B0 30 20 00 30 90 00 50 20 A0 30 E0 10 F0 20 B0 80 F0 50 E0 70 00 90 C0 F0 10 F0 50 E0 90 60 A0 20 A0 20 F0
C0 40 C0 50 A0 B0 20 E0 30 80 70 B0 A0 30 C0 50 B0 00 20 D0 F0 10 80 A0 A0 B0 10 A0 C0 70 10 20 F0 50 D0 B0 40 80 90 D0 B0 D0 60

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13937
  • Assembly is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #344 on: October 10, 2016, 11:59:56 AM »
Update 10 October 2016: The MasmBasic Switch_ macro is now also available for dual 64-/32-bit builds (see menu File/New Masm source/Dual 32/64 bit console/GUI templates, and the RichMasm screenshot below). The source builds as 32- or 64-bit code with ML.exe, HJWasm (default) or AsmC. No other libraries required, just a valid Masm32 installation plus MasmBasic.

Other minor additions:

mcs      multiple commands - see example in the message loop below
      mcs mov eax, 100 : add eax, 30 : sub eax, 7: deb 4, "sum:", eax : Inkey "-- press any key --"
      mcs mov eax, 100 : add eax, 30 : sub eax, 7: <Print Str$("result: eax=%i\n", eax)>
Rem      - mcs allows to put several commands on one line, separated by a colon as in Basic
      - if macros depend on previous commands, like e.g. Str$(...), <put them in brackets>


xmov      extended mov
      xmov MyR8, 123456.7890123456      ; assign a double directly
      xmov MyR4, MyR8      ; mem to mem, different operand size
      xmov someSQ, someSD
      xmov someSD, someSQ
      xmov ct, -5
      xmov MyR4, 32767
Rem      - use if in doubt about the best way to shove a value into a memory location (check the disassembly)
      - works with MasmBasic and 64-/32-bit JBasic


jinvoke
      jinvoke MyTest, &v0, *v1, addr v2, offset v3, &v4, v5
Rem      - jinvoke is a macro that works like invoke but assembles with 32- and 64-bit code alike