Author Topic: Calculate value of selected text  (Read 269 times)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13302
  • Assembly is fun ;-)
    • MasmBasic
Calculate value of selected text
« 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\Plugins.inc  ; Hit F6 to build this plugin, then test it from the System & Plugins menu
.code
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 +-*/
.Repeat
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
Endsw_
lodsb
xchg al, cl ; remember the operation (+-*/)
.Until !cl
invoke SendMessage, hRE, EM_REPLACESEL, 1, Str$("%6f", ST(0)v)
  .endif
  ret
PiEntry endp
LibMain proc uses esi edi ebx instance:DWORD, reason:DWORD, unused:DWORD
    or eax, -1
    ret
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)