The MASM Forum

Projects => MasmBasic & the RichMasm IDE => Topic started by: jj2007 on August 05, 2022, 08:23:29 AM

Title: Calculate value of selected text
Post by: jj2007 on August 05, 2022, 08:23:29 AM
A plugin for RichMasm: select a text, e.g. 0D903h/4444*333+22-1, then go to the menu System & Plugins and click on Calculate selection. The selected text will be replaced with the result. Here is the source:

Code: [Select]
include \masm32\MasmBasic\Plugins\  ; Hit F6 to build this plugin, then test it from the System & Plugins menu
ExternDef txName:BYTE
txName db "Calculate selection" ; $export  <<< info for the .def file
txDesc db "§Select e.g. '100/4+3', then click the plugin", 0 ; a short description for the menu
PiEntry proc hRE:DWORD ; $export  <<< info for the .def file
  .if piLen>2 ; 0D903h/4444*333+22-1 ; select & test; expected result is 4183.87
xchg eax, esi ; ptr to selected text in esi
xor ecx, ecx ; cl will hold +-*/
MovVal ST(0), esi ; returns digits used in dl, and a flag (dec/hex/bin) in dh
.Break .if edx==-127 ; flag NaN
movzx edx, dl ; len only
add esi, edx ; move on to next number
Switch_ ecx
Case_ "+":  fadd
Case_ "-":  fsub
Case_ "*":  fmul
Case_ "/":  fdiv
xchg al, cl ; remember the operation (+-*/)
.Until !cl
invoke SendMessage, hRE, EM_REPLACESEL, 1, Str$("%6f", ST(0)v)
PiEntry endp
LibMain proc uses esi edi ebx instance:DWORD, reason:DWORD, unused:DWORD
    or eax, -1
LibMain endp
end LibMain

To install the plugin, extract the DLL to \Masm32\MasmBasic\Plugins\CalcSelection.dll, then restart RichMasm (needs latest version of 5 August 2022 (