The MASM Forum

Projects => MasmBasic & the RichMasm IDE => Topic started by: jj2007 on April 28, 2016, 10:09:34 PM

Title: A guide to the RichMasm editor
Post by: jj2007 on April 28, 2016, 10:09:34 PM
When you install MasmBasic (http://masm32.com/board/index.php?topic=94.0) (a library that allows to use Basic syntax in assembler), the first thing you'll see after clicking OK is the file MbGuide.rtf in MasmBasic's editor called "RichMasm", a special editor for working with assembler source code. It has been designed for long sources, and concentrates on effective navigation rather than on beauty and/or abundance of features. The package includes a number of easy-to-use "skeletons", including dll and library examples.

On a green background, some simple instructions to build your first Hello World are shown.

In the right half of the screen, you can click into a listbox with "found matches", in this case: all 200+ occurrences of the Find string .Mb (an internal code to identify help topics). For example, if you change the string to .Mb*file and hit Enter, the find box will contain all 19 help entries dealing with files. The wildcard * means "find a line that contains both strings"; the string push eax*****pop would mean "find a line that contains push eax and has a pop on the same line or within the next 4 lines".

To the right, there are also a few bookmarks; you can click into them to jump e.g. to the "StringToArray" section. Bookmarks are defined by selecting a (meaningful) text, e.g. start: or Parser proc, and hitting Ctrl D. In contrast to so-called "bookmarks" in other editors, RichMasm bookmarks will be saved with the text, so that you can work with them next time you open the document. Press Alt K to toggle between keys and bookmarks view.

Another way to navigate in the text are the left and right arrows in the upper right corner: The document's history is stored with the source, and by clicking on the left arrow, you can go to sections that you edited earlier. Hitting the Alt Left or Alt Right keys will do the same.

To explore the menus, please go to the top of MbGuide.rtf and left-click on try 90+ snippets as shown below:
(http://www.webalice.it/jj2006/pics/RmGuide.jpg)

A new document opens, MbSnippets.asc.
- Hover over the File menu, it will open without clicking:

(http://www.webalice.it/jj2006/pics/RmMenu1.jpg)

As usual, Open, Save, Save as are on top; what you will most frequently need is New Masm source, the F6 and F7 keys, the Guide to MasmBasic, and the recent files list. Hopefully, you will never need the Last good version entry 8)

Some notes:

- New Masm source opens the file Template.asc; click on any link to "create a new project" (hint: "MessageBox" in the upper left corner opens a simple Hello World with really boring grey on black colours). Just edit according to your needs. When you try to save your work, you will be prompted to choose a new name, i.e. you are not allowed to overwrite the template.

- The yellow area "to build any"... on top is an optional extra window that serves to keep a portion of the document visible independently of the current edit position. Imagine you have a structure defined somewhere near the top, and you'd like to see the definition while working with it near the end of the document; just select the lines you want to see and press Ctrl F12 (if nothing is selected, the window disappears).

- The word Init is selected. If you hit F6 (=build & run), RichMasm will search backwards to "include..." and forward to either end start or EndOfCode, and if they are present, it will conclude that user wants to build only the section that contains Init. The whole document has 94 such "snippets", you can build and run them all either by selecting Init by hand, or by clicking into the "94 matches" listbox and hitting F6.

- In the upper right corner under "Find:", the text L1200... indicates Line1200, Column 3, +4 characters selected, 73d is the decimal Ascii code of the selected "I", while 49 6E 69 74h is the hexadecimal representation of Init.

- Now hover over the Edit & Format menu:
There are a number of formatting options, like colours, tabs and fonts, plus functions for exporting selected text. You will need the F4 (comment) key fairly often; when selecting several lines of text, the whole block gets commented out. Pressing F4 again turns comments into code, and in addition colours MasmBasic commands in blue, while push/pop pairs are formatted in red, for obvious reasons.

Hint for those who are scared of coloured code: Press Ctrl A (select all), then twice Ctrl E. The whole text will be black.

(http://www.webalice.it/jj2006/pics/RmMenu2.jpg)

- Finally, the menus System & Plugins, Help and AutoCode can be defined in \Masm32\MasmBasic\Res\menus.ini:
 
(http://www.webalice.it/jj2006/pics/RmMenu3.jpg)

Note that the help files, where technically possible, receive the currently selected text for jumping to their dedicated help page.

Here is an example of the output window, which can be toggled pressing the F7 key:

(http://www.webalice.it/jj2006/pics/RmOutput.jpg)

The code contains an error, so when hitting F6, RichMasm highlights the error message in the output window and jumps to the error line in the main edit window.
Title: Re: A guide to the RichMasm editor
Post by: satpro on April 30, 2016, 05:01:39 AM
GREAT post jj.  Keep 'em coming.
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on April 30, 2016, 08:04:20 AM
Thanks, satpro ;)

One element of the RichMasm philosophy is that a "project" doesn't need to be distributed over a dozen tiny little files. If possible, why not use one single file? Why, for example, should we use a separate resource file, if it can be integrated into the main source? Attached a simple example - a single *.asc file. Open it in RichMasm, edit a resource string (e.g. change "Welcome" to "Good night") and hit F6.

(http://www.webalice.it/jj2006/pics/RmRsrc.jpg)

As you can see in the screenshot, the AutoCode menu has an entry "Resource section". Just put the cursor behind GuiEnd (or end start/EndOfCode, wherever your source ends), click this entry and voilà, you have now an editable resource section. Every little change that you make can immediately be tested, you don't even have to save first (but do save it when you feel the code is OK 8)).
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on May 20, 2016, 11:29:21 AM
As of version 19 May 2016 (http://masm32.com/board/index.php?topic=94.0), RichMasm can assign keystrokes to menu functions:

- hover over a menu title, e.g. System & Plugins
- move down until the desired function is highlighted, e.g. "Find on disk" (a plugin)
- hold Shift and right-click into the highlighted menu item
- the menu closes, and you see "Press a key" in the status bar in the upper right corner
- press a key, e.g. f

Afterwards, the key sequence F12, f will do exactly what the menu would do. Hitting F12 twice will toggle between the list of shortcut keys and your bookmarks (try also pressing Alt K repeatedly).

Note this is saved immediately, and your shortcuts will not be overwritten by a re-installation of MasmBasic.
(http://www.webalice.it/jj2006/pics/RmMenuF12.png)
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on June 17, 2016, 12:12:05 PM
RichMasm beta: extract to \Masm32\MasmBasic\RichMasm.exe (you might rename the old one before).
Attachment removed, the beta has been promoted and is now included in the normal release (http://masm32.com/board/index.php?topic=94.0) :icon_cool:

Main feature is a much speedier (20*) load of files in the megabyte range. The culprit was the EM_LINEINDEX message. You may test the difference with the F9 key, which displays a file info message:
Quote
File info (when opened):
17.06.2016, 03:14:35, 1720890 bytes in 29146 lines

Loading MasmBasic.asc took 0.70 seconds

Other changes:
- in case of assembly errors, the jump to the offending line is now more precise for large files
- searching has become much faster; for example, finding 3483 times eax in the 18,000 lines of the RichMasm source takes now about 50ms on the i5, counted from hitting Return to seeing the listbox; in short: it's immediate
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on July 01, 2016, 09:22:42 AM
With RichMasm, you don't normally need any options - most of the \Masm32\examples sources, for example, build just fine by hitting F6. The editor detects fairly accurately if your source is a GUI application (/subsystem:Windows) or a console program. And yet, you can set quite a number of options. They consist of an uppercase OPT_SomeKeyword and are embedded in the source. Thus, they are available also when you copy the file to another computer, or you post it in the forum.

Here is a loose collection. In practice, OPT_Arg1, OPT_Wait and OPT_Icon are the most frequently needed options.

OPT_Assembler   ml                      ; mlv9, Jwasm, asmc, ... whatever is in \masm32\bin\
OPT_Linker      link                    ; polink, ...
OPT_Olly        1                       ; 0...9: use \masm32\OllyDbg\OLLYDB1.EXE (default is OllyDbg.exe)
OPT_DebugA      /Fl /Sn                 ; add items to the assembler commandline
OPT_DebugL      /verbose xfile.obj      ; add items to the linker commandline
OPT_Debug       1                       ; enable the heap debugger (useful if you suspect heap corruption; console only)
OPT_Susy        WINDOWS                 ; force subsystem: windows or console (but autodetect rarely fails)
OPT_Out         somepath.exe            ; force a different output file
OPT_Run         dosomething.exe with args
OPT_Res         someresourcefile        ; default are rsrc and ThisFile; no .rc extension please
OPT_Icon        smiley                  ; see \Masm32\MasmBasic\icons
OPT_Arg1        pass some arguments to the exe
OPT_Arg2        pass even more arguments
OPT_DelDef      0                       ; do not delete def files
OPT_DelExp      0
OPT_DelLib      1
OPT_DelPdb      1
OPT_DelLst      1
OPT_DelObj      1
OPT_DelTmp      0                       ; do not delete Tmp_File.asm
OPT_DelRes      1
OPT_DelBatch    1
OPT_Verbose     1                       ; show related files after building
OPT_BatA   SayHello                      ; run any bat or exe, e.g. SayHello.bat after assembling
OPT_BatL   congrats                      ; run any bat or exe, e.g. congrats.exe after linking
OPT_Wait        0                       ; do not wait for a keypress (useful e.g. for Windows apps with subsystem console)
OPT_Tmp2Asm     0                       ; create MyFile.asm
OPT_Tgt         dll                     ; default is exe, otherwise dll or rml aka RichMasm Lib


When they appear inside the code, use ; OPT_something, or
Code: [Select]
if 0
  OPT_Wait 0
  OPT_Susy Console
endif

Note that when building a project with RichMasm, all options become temporary environment variables. You can use them in your code with Masm's built-in @Environ macro, for example:
Code: [Select]
; OPT_Arg1 These are 'some arguments'
tmp$ CATSTR <Print "Arg1=[>, @Environ(oArg1), <]">
tmp$
Title: 64-bit assembly with RichMasm
Post by: jj2007 on August 30, 2016, 10:40:40 PM
Step-by-step instructions to build 64-bit assembly programs:
1. install the latest MasmBasic version from here (http://masm32.com/board/index.php?topic=94.0)
2. after clicking Accept & Install, you'll see the MasmBasic help file (light green background)
3. select Init below the -- same as 64-bit code: -- line
4. hit F6 to see your first 64-bit program build and run

More examples are in menu File/New Masm source. Note that the deb macro has some limitations, as compared against its big 32-bit brother (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1019). Inter alia, it works only in console applications:

Code: [Select]
deb 1, "Testing the debug macro:", rax, rcx, rsi, xmm0, xmm1, ST(0), ST(1), ST(2), ST(3)
Let me know if you run into any trouble (AntiVirus, ...) 8)
Title: Adding db " and ", 0
Post by: jj2007 on November 26, 2016, 11:39:29 AM
With MasmBasic version 26 Nov 2016 (http://masm32.com/board/index.php?topic=94.0), RichMasm has a new feature, inspired by Hutch:

Code: [Select]
Imagine some boring
text that you would
like to turn into a
Masm-readable bunch
of db declarations.

- select the text
- hit Alt R (replace)
- in the edit control in the upper right corner, enter db "]#[", 0
- hit Enter to confirm
- click Yes in the MsgBox and see this:

Code: [Select]
db "Imagine some boring", 0
db "text that you would", 0
db "like to turn into a", 0
db "Masm-readable bunch", 0
db "of db declarations.", 0

The ]#[ marker is a placeholder for the line content. For adding text only to the left, use e.g. $Data]#[ (i.e. no text on the right).

You may use this for a new variant of the Data (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1290) macro with a leading $ sign, meaning "no quotes needed to recognise this as a string":

include \masm32\MasmBasic\MasmBasic.inc
$Data Imagine some boring
$Data text that you would
$Data like to turn into an
$Data easy-to-use array of
$Data strings for display.

  Init
  Read my$()
  For_ ecx=0 To eax-1
      PrintLine my$(ecx)
  Next
  Inkey "... simple, right?"
EndOfCode


Output:
Code: [Select]
Imagine some boring
text that you would
like to turn into an
easy-to-use array of
strings for display.
... simple, right?
Title: Integrated debugging
Post by: jj2007 on January 23, 2017, 11:59:38 PM
Wouldn't it be nice if you could hit F6 to build your project, and see immediately the debugger?

Code: [Select]
include \masm32\include\masm32rt.inc ; plain Masm32 for the fans of pure assembler

.code ; make sure Olly sits in \Masm32\OllyDbg\ollydbg.exe
start:
  mov eax, chr$("Welcome to OllyDbg") ; you will see this text in Olly
  mov eax, chr$("Hit F9 to advance to the breakpoint")
  xor ebx, ebx ; argument counter
  .While 1
mov esi, cmd$(ebx)
int 3 ; set a breakpoint for Olly
.Break .if ecx!=1 ; read the Masm32 help on cmd$() to understand why
print "arg #"
print str$(ebx), ":", 9
print esi, 13, 10
inc ebx
  .Endw
  inkey
  exit

end start

OPT_Symbols 1 ; create symbols for OllyDbg
OPT_Arg1 "Hello Masm32 Forum"
OPT_Arg2 what's going on today?

When opened in RichMasm, the snippet (attached) builds as console application when hitting F6, but instead of running the exe directly, OllyDbg will be launched. Hit F9 and see what happens at the breakpoint.

If you comment out the breakpoint: ; int 3, RichMasm understands that you don't want to debug, and launches the executable normally.

Note also that for console applications, RichMasm takes care to activate OllyDbg instead of the console output window - a minor feature maybe, but if you have to activate the debugger a hundred times because the console is always in front, you may start to appreciate RichMasm's little service ;-)
Title: Plug-ins for the RichMasm editor
Post by: jj2007 on February 01, 2017, 10:25:46 AM
RichMasm has a comfortable plug-in interface, giving access to e.g. the selected text, the handle to the RichEdit control, etc.

Attached are two examples:
- FindOnDisk allows to quickly search files for two search patterns
- CompileBasic is for compiling sources written in various BASIC dialects, plus some C/C++ variants.

Both need to be extracted to \Masm32\MasmBasic\Plugins
After a restart of RichMasm, they appear in System & Plugins menu.
Title: Working with Unicode file names
Post by: jj2007 on February 11, 2017, 12:42:37 PM
Most people never need filenames in exotic languages, but sometimes it might be necessary.
Attached is an example of a source with an Arabic name and an Arabic MsgBox.
Open it in RichMasm (version 11 February 2017 or later (http://masm32.com/board/index.php?topic=94.0)) and hit F6 to build the demo.
Title: Re: A guide to the RichMasm editor
Post by: mabdelouahab on February 11, 2017, 06:21:24 PM
Hi jj
Quote
you need \masm32\bin\HJWasm32.exe ...
Why?
I have Windows 8.1 X64 Arabic, And I use HJWasm64.exe To create 32/64 application
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on February 11, 2017, 08:50:28 PM
Hi mabdelouahab,

Probably, RichMasm saw a very old ML.exe in your \bin and suggested to download & install HJWasm32.exe. You can indeed use any MASM-compatible assembler except the old one (6.14) that comes with the Masm32 package.

In the example I posted above, there are OPT_* lines. Change them according to your needs, for example:
Code: [Select]
OPT_Assembler mlv615
OPT_Linker linkv614

Assuming, of course, there are \Masm32\bin\mlv615.exe and \Masm32\bin\linkv614.exe on your disk. If there are no OPT_* lines, or they are disabled with e.g. OxPT..., RichMasm uses HJWasm32 and polink.

I attach a 64-bit example, with OPT_Linker linkv10 (version 6.14 and polink won't work with an Arabic filename and 64-bit code)
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on February 14, 2017, 12:43:44 PM
Update 14 Feb 2017: RichMasm (http://masm32.com/board/index.php?topic=94.0) works now also with Unicode file names and 64-bit code, see attachment. The restrictions regarding assemblers and linkers are no longer valid, i.e. it works fine with ML, HJWasm32, AsmC and various linkers including polink.

include \Masm32\MasmBasic\Res\JBasic.inc      ; ## console demo, builds in 32- or 64-bit mode with ML, AsmC, JWasm, HJWasm ##
  Init            ; OPT_64 1      ; put 0 for 32 bit, 1 for 64 bit assembly
  PrintLine Chr$("This code was assembled with ", @AsmUsed$(1), " in ", jbit$, "-bit format")
  wMsgBox 0, 'This is a "text" for a Unicode MessageBox', "Title:", MB_OK
  wMsgBox 0, "Это окно с сообщением", "Это название:", MB_OK
  wMsgBox 0, "هذا هو مربع الرسالة.", "أهلا بك", MB_OK
  wMsgBox 0, '这是一个消息框。', "这是一个标题:", MB_OK
EndOfCode
Title: Re: A guide to the RichMasm editor
Post by: mabdelouahab on February 14, 2017, 04:23:46 PM
hi JJ, I've downloaded Update .
How do I deal with this?
Code: [Select]
** Start C:\Masm32\MasmBasic\Res\bldallRM.bat **
*** user-defined OPT_xx variables: ***
Page de codes activeÿ: 65001
**** 64-bit assembly ****

OPT_Arg1:
OPT_Arg2:
OPT_Out:  Tmp_File.exe
OPT_Res:  Tmp_file


*** Assemble, link and run صندوق رسالة ***

*** Assemble using \masm32\bin\HJWasm32 /c /Zp8 -win64 tmp_file.asm ***
HJWasm v2.17, Dec  5 2016, Masm-compatible assembler.
Portions Copyright (c) 1992-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.

Error A2106: Cannot open file: "tmp_file.asm" [ENOENT]
*** Assembly Error ***
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on February 14, 2017, 08:09:42 PM
How do I deal with this?
Code: [Select]
Error A2106: Cannot open file: "tmp_file.asm" [ENOENT]

Sorry for that, mabdelouahab :icon14:

It works for me with Win7-64 and Win10, Italian versions. Now the question is why RichMasm can't open (or can't find) your tmp_file.asm :(

Does your folder name contain non-Ansi characters? If yes, then version 14 Feb 2017 (http://masm32.com/board/index.php?topic=94.0) should work for you. I just uploaded it, there was a SetCurrentDirectoryA that made the assembly fail if the path contained "true" Unicode. Please let me know if it works now.

Anybody else having this kind of problems?
Title: Re: A guide to the RichMasm editor
Post by: LordAdef on February 15, 2017, 07:44:48 PM
Rock solid here JJ.

By the way, did you notice this line in his report:

Quote
*** Assemble, link and run صندوق رسالة ***

Title: Re: A guide to the RichMasm editor
Post by: jj2007 on February 15, 2017, 10:42:39 PM
Rock solid here JJ.

Thanks :biggrin:

Quote
By the way, did you notice this line in his report:

Quote
*** Assemble, link and run صندوق رسالة ***

Yes indeed, RichMasm used Ansi filenames - call it a bug. The latest version (15 Feb (http://masm32.com/board/index.php?topic=94.0)) handles it correctly (F9 produces the big MessageBox):

(http://www.webalice.it/jj2006/pics/BuildUnicodeFiles.png)

Project attached, needs MasmBasic 15 Feb 2017 (http://masm32.com/board/index.php?topic=94.0).
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on March 21, 2017, 05:59:50 AM
The attached beta has a new version of RichMasm allowing, inter alia, to translate hyperlinks very directly into source code - see second attachment.

GuiParas      equ "ToolTips for the SysLink control", w300, h80
include \masm32\MasmBasic\Res\MbGui.asm
  GuiControl GetHelp, "syslink", h0+20, "Click here (http://masm32.com/board/index.php?topic=94.0) to download MasmBasic, or here (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm) for online help"
  GuiControl VisitMoscow, "syslink", y0+20, h0+40, "Get info on Moscow (http://вднх-отдых.рф): Развлечения, история, музеи, рестораны-кафе, гостиницы, информация"

Event Command
  .if NotifyCode==NM_CLICK && (wParam==GetHelp || wParam==VisitMoscow)
      wMsgBox 0, Link$(), "Open this page?", MB_OKCANCEL
      If_ eax==IDOK Then <wShEx Link$()>
  .endif
GuiEnd
Rsrc
#include "resource.h"
IDI_APPLICATION ICON      "\\Masm32\\MasmBasic\\icons\\Smiley.ico"
01 RT_MANIFEST      "\\Masm32\\MasmBasic\\Res\\XpManifest.xml"
Rsrc

This is how it looks in the editor:(http://www.webalice.it/jj2006/pics/SysLinkDemo.png)

EDIT: Beta removed, the current version (http://masm32.com/board/index.php?topic=94.0) has the new features now.
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on May 01, 2017, 09:47:06 AM
As Edsger Wybe Dijkstra (https://en.wikiquote.org/wiki/Edsger_W._Dijkstra) once noted,
Quote
It is practically impossible to teach good programming to students that have had a prior exposure to Visual Studio: as potential programmers they are mentally mutilated beyond hope of regeneration.

For these poor beings, who spread their PROTOs and MACROs over a dozen tiny files in order to call their mess a "project", there is HELP now: The attached RichMasm beta extends the search feature (select a word, hit F3) to files that are INCLUDEd in the source.

This is limited to those files that are included by an UPPERCASE INCLUDE at the moment when the file was opened. So to test it, change the desired include to INCLUDE, then save and reload the file:

include \masm32\MasmBasic\MasmBasic.inc
; include \masm32\include\masm32rt.inc
INCLUDE mymacros.inc
INCLUDElib msvcrt.lib

Here, only mymacros.inc is included in the search.
Title: Re: A guide to the RichMasm editor
Post by: TWell on May 01, 2017, 12:41:44 PM
Quote
It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.
At that time BASIC was weak with subroutines and structured programming. Quick Basic and Turbo Basic correct some problems.

Asm programmers needs those little files when creating libraries as ml don't have support for COMDAT.

EDIT: forgot to say [h]jwasm and asmc have support for it.

A good IDE's have a 'Find in files' feature, like poide.exe has.
Title: Re: A guide to the RichMasm editor
Post by: hutch-- on May 01, 2017, 01:53:25 PM
I wonder what capacity the COMDAT (common data) has that cannot be done with MASM's "PUBLIC" keyword. It is routine to make something PUBLIC in MASM and access it from other modules.
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on May 02, 2017, 03:00:21 AM
A good IDE's have a 'Find in files' feature, like poide.exe has.

Pelle's IDE is among the better ones, but the "find in files" feature is a joke. Check out the RichMasm beta to see the difference.
Title: Re: A guide to the RichMasm editor
Post by: TWell on May 02, 2017, 03:48:26 AM
I tried to download it, but Avast ate it.
So what kind of feature it should be?
Title: Re: A guide to the RichMasm editor
Post by: LordAdef on May 02, 2017, 04:00:51 AM
Tim, bypass Avast for 10 minutes and download it. Avast is not asm community friend  :icon_redface:
Title: Re: A guide to the RichMasm editor
Post by: LiaoMi on May 02, 2017, 04:20:39 AM
Hi!

I compared the toolbar by looking at the picture in this top and noticed that only my menu is so badly displayed ... About how it looks like you can see below - https://ibb.co/bWF8Lk (https://ibb.co/bWF8Lk)
(https://preview.ibb.co/h32YmQ/masmbasic_gui.jpg)

Maybe this problem has already happened?

Thankeee!
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on May 02, 2017, 05:36:26 AM
only my menu is so badly displayed

The menu font is a problem indeed. One day I will have to find a solution :(

Reason is that when the Aero themes came out, the old GetWindowDC trick stopped working.

@Tim: really, can't you disable Avast for 5 minutes?? Btw Jotti shows that 16 out of 19 AVs are OK with that RM beta. (https://virusscan.jotti.org/en-US/filescanjob/lu046rsxpu) For the unpacked version, it's 18/19 OK. (https://virusscan.jotti.org/en-US/filescanjob/gudbybij3c)
Title: Re: A guide to the RichMasm editor
Post by: LordAdef on May 02, 2017, 03:33:34 PM
Working fine here JJ
Title: Detecting and debugging heap corruption
Post by: jj2007 on May 22, 2017, 09:36:45 PM
A simple demo showing how to detect heap corruption:

Code: [Select]
include \masm32\include\masm32rt.inc ; plain Masm32

.code
txString db "Hello, this is a simple string, 50 characters long", 0
start:
  mov esi, rv(HeapAlloc, rv(GetProcessHeap), HEAP_ZERO_MEMORY, 22h) ; 22h=34 bytes, not enough for txString

  print "before doing illegal things to esi="
  print hex$(esi), 13, 10

  invoke lstrcpy, esi, addr txString ; this is commonly called "buffer overflow"
  print " after doing illegal things", 13, 10, 10, "[" ; but it works just fine, because the heap
  print esi, "]", 13, 10, 10 ; doesn't yet know that it's "corrupt" now ;-)

  print "----- OK, we are now freeing esi, and strange things will happen: -----"
  invoke HeapFree, rv(GetProcessHeap), 0, esi
  inkey "----- buffer was freed -----"
  exit

end start ; OPT_Debug 1 ; this option requires RichMasm

When hitting F6 in RichMasm (MasmBasic's IDE (http://masm32.com/board/index.php?action=unread)), you will see this output:
Code: [Select]
before doing illegal things to esi=002A5920
 after doing illegal things

[Hello, this is a simple string, 50 characters long]

----- OK, we are now freeing esi, and strange things will happen: -----
## HEAP[HeapCorruption.exe]:
## Heap block at 002A5918 modified at 002A5942 past requested size of 22

## HEAP[HeapCorruption.exe]:
## Invalid address specified to RtlFreeHeap( 002A0000, 002A5920 )
----- buffer was freed -----

Project attached. Let me know if it can be improved.
Title: Rich text to the forum
Post by: jj2007 on July 11, 2017, 10:37:19 PM
With MasmBasic version 11 July 2017, the formatted output has been improved: select text, hit F11, paste in a reply. Example:

include \masm32\MasmBasic\MasmBasic.inc         ; download (http://masm32.com/board/index.php?topic=94.0)
  if 0  ; copy the line below, then hit F6 to get the db version
        chartab                 dw "10","01","12","21","13","31"
        ; expected output:
        chartab                 db "0","1","1","0","2","1","1","2","3","1","1","3"
  endif
  Init                          ; *** translate Masm dw format to db for use with UAsm ***
  Let esi=Clip$()               ; get string from clipboard (limit is about 160k)
  lea ecx, [2*Len(esi)]         ; the byte format is longer, so we need a bigger buffer
  Let edi=New$(ecx)
  push esi
  push edi
  .Repeat
        lodsb
        If_ al=="w" Then mov al, "b"    ; dw->db
        stosb
        .Break .if !al
        .if al==34
                push [esi]
                lodsb
                movsb           ; first digit
                mov al, 34
                stosb
                mov al, ","
                stosb
                mov al, 34
                stosb
                pop eax
                stosb           ; second digit
                movsb           ; second quote
        .endif
  .Until 0
  pop edi
  pop esi
  Inkey "Before: ", esi, CrLf$, "After:  ", edi, CrLf$, "Copy to clipboard? (y)", CrLf$
  If_ eax=="y" Then SetClip$ edi
EndOfCode
Title: Re: A guide to the RichMasm editor
Post by: FORTRANS on July 12, 2017, 02:01:10 AM
Hi,

   Just for feedback, a screenshot.

HTH,

Steve
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on July 12, 2017, 04:12:43 AM
Hi Steve,

The lines are wrapped. What's your screen resolution? Mine is 1366x768, and even with enlarged fonts they are not long enough to wrap. Or did you mean something else?

Jochen
Title: Re: A guide to the RichMasm editor
Post by: FORTRANS on July 12, 2017, 05:10:38 AM
Hi Jochen,

The lines are wrapped. What's your screen resolution?

   Usually 1024x768.  Sometimes higher, though not very often.

Quote
Mine is 1366x768, and even with enlarged fonts they are not long enough to wrap. Or did you mean something else?

   Well, yes the lines wrap, but I just thought you might find it
useful to see your work on another's system.  Maybe the colors
and fonts are different for instance.  You may try to have all your
computers set up similarly.  I used to do that a while back, and
had some odd results when one of my programs was used on
someone else's computer.  (Code page different I think.)

Regards,

Steve N.
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on July 12, 2017, 05:25:26 AM
Well, yes the lines wrap, but I just thought you might find it useful to see your work on another's system.  Maybe the colors and fonts are different for instance.

Colours and fonts look similar, but 1024x768 is indeed a bit low for today's standards. Anyway, it's the browser who decides when to wrap lines.

But I understand the problem very well. Often, pages just look garbled, OK buttons are below the visible area, etc - messy sites made by professional web developers who only made one small mistake: to assume that the user also has that great 4k screen that a decent IT professional is entitled to enjoy 8)
Title: Re: A guide to the RichMasm editor
Post by: HSE on July 12, 2017, 06:40:19 AM
Good practice is:
Code: [Select]
include \masm32\MasmBasic\MasmBasic.inc         ; download
  if 0  ; copy the line below, then hit F6 to get the db version
        chartab                 dw "10","01","12","21","13","31"
        ; expected output:
        chartab                 db "0","1","1","0","2","1","1","2","3","1","1","3"
  endif
  Init                          ; *** translate Masm dw format to db for use with UAsm ***
  Let esi=Clip$()               ; get string from clipboard (limit is about 160k)
  lea ecx, [2*Len(esi)]         ; the byte format is longer, so we need a bigger buffer
  Let edi=New$(ecx)
  push esi
  push edi
  .Repeat
        lodsb
        If_ al=="w" Then mov al, "b"    ; dw->db
        stosb
        .Break .if !al
        .if al==34
                push [esi]
                lodsb
                movsb           ; first digit
                mov al, 34
                stosb
                mov al, ","
                stosb
                mov al, 34
                stosb
                pop eax
                stosb           ; second digit
                movsb           ; second quote
        .endif
  .Until 0
  pop edi
  pop esi
  Inkey "Before: ", esi, CrLf$, "After:  ", edi, CrLf$, "Copy to clipboard? (y)", CrLf$
  If_ eax=="y" Then SetClip$ edi
EndOfCode
And everybody can to choose colors :biggrin:
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on July 12, 2017, 07:12:00 AM
Good practice is:

So why do almost all IDEs and many programming sites come along with syntax highlighting? Bad practice?
Title: Re: A guide to the RichMasm editor
Post by: HSE on July 12, 2017, 08:11:17 AM
In the box is more easy to copy!!

LATER: I also have configurated 1024x768
Title: Re: A guide to the RichMasm editor
Post by: FORTRANS on July 12, 2017, 10:28:40 PM
Hi,

   BTW, if I have a choice of wrapped comments or uncommented
code, I choose having wrapped comments.  Many denizens of
this forum seem to like uncommented code though.  Maybe they
have a phobia of wrapping.  <g>

Cheers,

Steve
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on July 13, 2017, 12:36:56 AM
In the box is more easy to copy!!

That was before the Mozilla "coders" decided to disable the Code: [Select] function :(

I have a choice of wrapped comments or uncommented code, I choose having wrapped comments.

The code box doesn't wrap them, that may look better but to read them you have to click the horizontal scroll bar. Not the best solution either :icon13:

  Recall Utf8$(wCL$()), L$()    ; translate the file supplied through the (Unicode) commandline to a text array (if the file is Unicode, the strings will be in UTF-8 format; if needed, you can get Unicode back with wRec$(L$(n)) - see also ConvertCp$)


Code: [Select]
  Recall Utf8$(wCL$()), L$() ; translate the file supplied through the (Unicode) commandline to a text array (if the file is Unicode, the strings will be in UTF-8 format; if needed, you can get Unicode back with wRec$(L$(n)) - see also ConvertCp$)
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on October 10, 2017, 02:47:44 AM
I am trying to figure out if it's worthwhile to look for the debugger paths when doing a fresh install of MasmBasic. Currently, the default paths are hardcoded (see: integrated debugging (http://masm32.com/board/index.php?topic=5314.msg63302#msg63302)) to

\Masm32\x64Dbg\release\x64\x64dbg.exe for 64-bit debugging
\Masm32\OllyDbg\ollydbg.exe for 32-bit code.

Therefore the question: Where are your debuggers located?
Title: Re: A guide to the RichMasm editor
Post by: Biterider on October 10, 2017, 04:45:27 AM
Hi
only as a hint, Visual Studio stores the debugger location here:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug]


Biterider
Title: Re: A guide to the RichMasm editor
Post by: LiaoMi on October 10, 2017, 05:39:20 AM
I am trying to figure out if it's worthwhile to look for the debugger paths when doing a fresh install of MasmBasic. Currently, the default paths are hardcoded (see: integrated debugging (http://masm32.com/board/index.php?topic=5314.msg63302#msg63302)) to

\Masm32\x64Dbg\release\x64\x64dbg.exe for 64-bit debugging
\Masm32\OllyDbg\ollydbg.exe for 32-bit code.

Therefore the question: Where are your debuggers located?

Unchangeable folder paths in this case better  :P
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on October 10, 2017, 06:03:27 AM
Code: [Select]
include \masm32\MasmBasic\MasmBasic.inc
  Init
  PrintLine GetRegVal("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug", "Debugger")
  SetReg64
  Inkey GetRegVal("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AeDebug", "Debugger")
EndOfCode

yields
"C:\Masm32\OllyDbg\ollydbg.exe" -AEDEBUG %ld %ld
"C:\Masm32\x64Dbg\release\x64\x64dbg.exe" -a %ld -e %ld

I attach the project. These are the just-in-time debuggers, should be fine :t

Btw a pretty old key: The AeDebug subkey stores user preferences for Dr. Watson and other debugging tools that run on Windows 2000 (https://technet.microsoft.com/en-us/library/cc939482.aspx)

No such key on Win10, but Olly shows up at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\RADAR\HeapLeakDetection\DiagnosedApplications\ollydbg.exe  :bgrin:

Unchangeable folder paths in this case better  :P

Why that?
Title: Re: A guide to the RichMasm editor
Post by: LiaoMi on October 10, 2017, 10:45:30 PM

Unchangeable folder paths in this case better  :P

Why that?

Hi,

the debugger often receives updates, sometimes there are errors that you need to debug. I have three versions, clean current release, stable version for work, and the current version with plug-ins, this package is always updated by adding plugins or changing the settings. Therefore, the exact binding for me is better  :biggrin:
Title: Re: A guide to the RichMasm editor
Post by: jj2007 on October 11, 2017, 01:05:44 AM
I have three versions, clean current release, stable version for work, and the current version with plug-ins, this package is always updated by adding plugins or changing the settings. Therefore, the exact binding for me is better  :biggrin:

OK, so the best solution will be to
- test if a debugger is already set in RichMasm's prefs
- if not, check for the registry key
- if there is none, see if there is *dbg*.exe inside the \Masm32 tree.

I guess that will add a few hundred bytes to RichMasm's 72kB :(