Author Topic: Miscellaneous snippets  (Read 53365 times)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
Re: Miscellaneous snippets
« Reply #165 on: January 05, 2021, 10:52:02 PM »
I wish to establish whether or not FreeBASIC DOS version can work with in-line assembly (prefer DOS version at this stage but will also consider windows version which I have used a bit).

Would you be able to supply a very simple FreeBASIC program that uses in-line assembly

Richard,

That is a question for the FreeBasic forum. I have never touched FB for DOS, so I cannot give you advice on that, sorry...

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
A 14 lines editor
« Reply #166 on: January 15, 2021, 12:34:06 PM »
Note this is the bare minimum - just a demo. No checks, no bells and whistles. Use at your own risk.
Code: [Select]
GuiParas equ "A full-fledged raw Win32 GUI", icon Ball, b Turquoise ; title, icon, background
GuiMenu equ @File, &Open, &Save, -, E&xit, @Edit, Undo, Copy, Paste ; define a menu (only Open & Save are implemented)
include \masm32\MasmBasic\Res\MbGui.asm
  GuiControl RichEdit, "richedit", CL$() ; load what you find in the commandline
  GuiControl Status, "statusbar"
Event Menu
  Switch_ MenuID
  Case_ 0 ; Open (menu entries start with 0)
  If_ FileOpen$("FB sources=*.bas|Includes=*.bi;*.inc|Resources=*.rc|All files=*.*") Then SetWin$ hRichEdit=FileRead$(FileOpen$())
  Case_ 1 ; Save
  FileWrite CL$(), stream:hRichEdit ; save contents of the RichEdit control
  SetWin$ hStatus=CL$()+" was saved"
  Endsw_
GuiEnd
OPT_Arg1 ?:\Masm32\MasmBasic\AscUser\OpenSaveFB.asc ; for testing with RichMasm

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
Convert tabs to spaces
« Reply #167 on: February 19, 2021, 10:17:35 AM »
Just drag the tabbish source over the attached exe.

include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  Let esi=FileRead$(CL$())
  Let esi=Replace$(esi, Tb$, Space$(8)))
  FileWrite "NoMoreTabs.bas", esi
  ShEx "NoMoreTabs.bas"         ; launch it with ShellExecuteEx
EndOfCode


(I don't recommend it, though - I love my tabs and would never exchange them for spaces :badgrin:)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
Size of Chinese text, Utf-8 vs Utf-16
« Reply #168 on: February 26, 2021, 08:19:46 PM »
include \masm32\MasmBasic\MasmBasic.inc
  Init
  Let esi=FileRead$(99)         ; WarAndPeaceChinese.txt from resource
  Let edi=Utf8$(esi)
  deb 4, "wLen", wLen(esi)      ; Utf-16 chars
  deb 4, "uLen", uLen(edi)      ; Utf-8 chars
  deb 4, "Len", Len(edi)        ; bytes
  uMsgBox 0, edi, "Chinese text as Utf-8:", MB_OK
  wMsgBox 0, esi, "Chinese text as Utf-16:", MB_OK
EndOfCode
Rsrc
#include "resource.h"
IDI_APPLICATION ICON    "\\Masm32\\MasmBasic\\icons\\Globe.ico"         // Calc, Disk, Editor, Eye, Globe, MasmBasic, Smiley
01 RT_MANIFEST         "\\Masm32\\MasmBasic\\Res\\XpManifest.xml"
99 RCDATA      "WarAndPeaceChinese.txt"
Rsrc


Code: [Select]
wLen    eax             526
uLen    eax             525
Len     eax             1449

So that is 1449 vs 2*526 bytes, which means that Utf-8 needs 37.7% more space in memory.
When saving the Utf-8 file, and zipping both together, the packed sizes differ by only 5.5%, though.

Project attached, requires MasmBasic.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
Compound chars, Utf-8 vs Utf-16
« Reply #169 on: February 26, 2021, 09:13:25 PM »
Lots of people argue on the web whether Utf-16 is "harmful", and what are the pros and cons of Utf-8 vs Utf-16. As a matter of fact, both work just fine, as demonstrated below with two compound characters that are not in the Basic Multilingual Plane. For Chinese text, the space requirements are very similar, as demonstrated above, for English text, Utf-8 clearly wins, of course.

include \masm32\MasmBasic\MasmBasic.inc
  Init
  Cls
  Let edi="aaa  שָׁ  bbb  𝕥  ccc"         ; two compound characters with 6 and 4 bytes in Utf-8
  PrintLine "Utf-8:", CrLf$, HexDump$(edi, 32)
  Let esi=wRec$(edi)            ; convert to Utf-16
  PrintLine "Utf-16:", CrLf$, HexDump$(esi, 48)
  if 0  ; activate if needed
        lea ecx, [2*wLen(esi)]
        FileWrite "compound.txt", esi, ecx
  endif
  uMsgBox 0, edi, "Utf-8:", MB_OK
  wMsgBox 0, esi, "Utf-16:", MB_OK
EndOfCode


Output (20 = 32 dec are the spaces):
Code: [Select]
Utf-8:
002BD2B0  61 61 61 20 20 D7 A9 D7 81 D6 B8 20 20 62 62 62 aaa  שָׁ  bbb
002BD2C0  20 20 F0 9D 95 A5 20 20 63 63 63 00 00 00 00 00   𝕥  ccc.....

Utf-16:
0028DEF0  61 00 61 00 61 00 20 00 20 00 E9 05 C1 05 B8 05 a.a.a. . .�.�.�.
0028DF00  20 00 20 00 62 00 62 00 62 00 20 00 20 00 35 D8  . .b.b.b. . .5�
0028DF10  65 DD 20 00 20 00 63 00 63 00 63 00 00 00 00 00 e� . .c.c.c.....

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
CoVid-19 stats
« Reply #170 on: March 21, 2021, 01:02:49 AM »
Data come from the Allgemeine Ortskrankenkasse (AOK) Berlin. The graph shows how many employees went on sick leave because they tested CoVid-19 positive.

Code: [Select]
GuiParas equ "Covid by profession", w1200, h630, b Grey, icon Plot
include \masm32\MasmBasic\Res\MbGui.asm
  SetGlobals language=2 ; default is English
  GuiControl MyCanvas, "canvas"
  Recall 99, L$(), tab ; res 99 = CovidAOK.tab
Event CanvasPaint
  For_ ct=0 To L$(?)-2
sar Val(L$(ct, 0)), 2 ; /4
xchg eax, ecx
GuiTextBox 20, ct*30+12, ecx, 26, Cat$(" "+L$(ct, language+1)), fcol White, bcol Red, DT_LEFT
  Next
  GuiTextBox 800, 200, 200, auto, L$(ct, language+1), fcol Black, bcol LiteYellow, DT_WORDBREAK, font -20:FW_BOLD
Event Key
  Switch_ VKey
  Case_ VK_D: Clr language ; German
  Case_ VK_I: m2m language, 1 ; Italian
  Case_ VK_E: m2m language, 2 ; English
  Case_ VK_F: m2m language, 3 ; French
  Endsw_
  GuiRefresh
EndOfCode


jj2007

  • Moderator
  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
Grabbing the output of a launched program
« Reply #171 on: March 21, 2021, 11:12:03 PM »
include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  StringToArray Launch$("cmd.exe /c dir *.asm"), f$()   ; use Launch$ to grab the output, translate into a string array
  For_ each esi in f$(): <Let f$(ForNextCounter)=Mid$(esi, 35)>  ; eliminate date, time, size, e.g. 27.08.17  23:34            16,527 SortAnIntegerArray.asm
  QSort f$()
  For_ each esi in f$(): PrintLine esi
EndOfCode


An alternative:
Code: [Select]
include \masm32\MasmBasic\MasmBasic.inc
  Init
  StringToArray Launch$("cmd.exe /c dir /O-D *.asm"), f$() ; use Launch$ to grab the output, translate into a string array
  Delete f$(0), 5 ; eliminate the five strings at the beginning of a dir command
  Delete f$(f$(?)-2), 2 ; eliminate the two trailing strings
  For_ each esi in f$(): PrintLine esi ; print all files
EndOfCode

Output:
Code: [Select]
20.03.21  18:53               873 CovidAOK.asm
19.03.21  15:53            12,246 CheckNewPosts.asm
...
29.06.13  00:59             4,687 Win64_3e.asm
25.06.11  11:03             4,276 cids.asm

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
Put environment variables to string array
« Reply #172 on: March 22, 2021, 10:39:01 PM »
include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  GetRegArray "HKLM\SYSTEM\ControlSet001\Control\Session Manager\Environment", MyEnv$(), MyData$()
  QSortMode 1, 0, DataIndex() As DWORD  ; case-insensitive, don't skip leading spaces
  QSort MyEnv$()                        ; sort by name of key
  Open "O", #1, "Environment.tab"
  For_ each esi in MyEnv$(): <PrintLine #1, esi, Tb$, MyData$(DataIndex(ForNextCounter))>
  Close #1
  ShEx "Environment.tab"
EndOfCode


Output (shortened):
Code: [Select]
ComSpec %SystemRoot%\system32\cmd.exe
DXSDK_DIR       C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\
FP_NO_HOST_CHECK        NO
...
VS140COMNTOOLS  C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\
windir  %SystemRoot%

Attached a version that writes the variables to a tab-delimited file and opens it in your default *.tab viewer (which might be MS Excel, for example).

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
GetFiles for batch files
« Reply #173 on: April 03, 2021, 11:52:11 PM »
The dir command is lousy, but fortunately we have a handy MasmBasic command: GetFiles :thumbsup:

This little proggie should be put into a batch file e.g. as
Code: [Select]
\Masm32\bin\VS14\ml64.exe /c /Zp8 /Zi /Zd /Zf %1
echo ----------- assembly done --------------------
echo.
\Masm32\bin\VS14\link.exe /subsystem:console /entry:start /debug %~dpn1.obj /out:%~dpn164.exe
echo.
\Masm32\MasmBasic\Res\GetFiles %~dpn1* -time -age:2

Here is the (shortened) output for the source short.asm:
Code: [Select]
Microsoft (R) Macro Assembler (x64) Version 14.00.24210.0
...
----------- assembly done --------------------

Microsoft (R) Incremental Linker Version 14.00.24215.1
Copyright (C) Microsoft Corporation.  All rights reserved.

----- files created 14:50:18
389120  03.04.2021  14:50:18    Short64.pdb
14336   03.04.2021  14:50:18    Short64.exe
77928   03.04.2021  14:50:18    Short64.ilk
309334  03.04.2021  14:50:18    Short.obj

The arguments:
Code: [Select]
-time show the current time as ----- files created 14:50:18 (see above)
-age:2 show only files that are less than 2 seconds old (i.e. ignore older stuff)

Source & exe attached.