Author Topic: MasmBasic  (Read 213970 times)

2B||!2B

  • Regular Member
  • *
  • Posts: 29
Re: MasmBasic
« Reply #435 on: April 19, 2018, 04:51:57 PM »
I have a question. Why do you use FPU/MMX/SSE instructions set so often? are there any advantages by using them rather than using the standard x86 instructions set? like faster execution, smaller code size maybe?

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8826
  • Assembler is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #436 on: April 19, 2018, 05:32:39 PM »
There are no MMX instructions in MasmBasic (MMX code is inefficient, and there are conflicts with the FPU).

For calculations, the FPU is generally being used; it's not faster (and not slower) than the corresponding SSE* instructions, but internally it gives REAL10 precision, while SSE* offers only double precision, i.e. REAL8.

SSE* code is generally not shorter than ordinary x86 (but there are exceptions). However, it's often a lot faster. Take a simple example, getting the length of a zero-delimited string. There is a little thread here showing how the Masm32 team developed the current MasmBasic Len() macro. The thread has 149 replies, meaning it took a while to find the fastest option. Check yourself how fast it is, compared to the strlen function of the C runtime library (remember C is the fastest language on Earth :P):

Code: [Select]
include \masm32\MasmBasic\MasmBasic.inc
  Init
  Recall "\Masm32\include\Windows.inc", L$() ; get a string array
  push eax ; #elements
  xor ebx, ebx
  xor edi, edi
  NanoTimer()
  .Repeat
invoke crt_strlen, L$(ebx)
add edi, eax ; calculate total len
inc ebx
  .Until ebx>=stack
  Print "The CRT needs ", NanoTimer$(), Str$(" for calculating the length of %i strings", ebx), Str$(", a total of %i bytes\n", edi)
  xor ebx, ebx
  xor edi, edi
  NanoTimer()
  .Repeat
add edi, Len(L$(ebx)) ; calculate total len
inc ebx
  .Until ebx>=stack
  pop edx
  Inkey " MB Len needs ", NanoTimer$(), Str$(" for calculating the length of %i strings", ebx), Str$(", a total of %i bytes\n", edi)
EndOfCode

Note that Windows.inc has relatively short strings. The difference between the speed of Len vs crt_strlen is much bigger for long strings. For example, if you apply the code above to a bible.txt file, with longer strings, the difference is twice as much. I won't tell you how much because there a C/C++ coders here in the forum, and they might fall into a deep depression if they see the results :icon_cool: 

Attached a full example, comparing also the Instr() performances of MasmBasic and the CRT (if that sounds interesting, see Benchmark 2: Alice in Wonderland for a CRT strstr vs Boyer-Moore comparison)

2B||!2B

  • Regular Member
  • *
  • Posts: 29
Re: MasmBasic
« Reply #437 on: April 20, 2018, 03:35:12 PM »
Ah ok i got it now thanks for clarifying that. Actually i used some FPU instructions for floating point result (like progress bar 0.# for example) but have never used SSE instructions.  :biggrin:

I won't tell you how much because there a C/C++ coders here in the forum, and they might fall into a deep depression if they see the results :icon_cool: 

Lol, they can still use inline ASM though  :biggrin:

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8826
  • Assembler is fun ;-)
    • MasmBasic
Floor & Ceil
« Reply #438 on: April 25, 2018, 05:24:34 PM »
include \masm32\MasmBasic\MasmBasic.inc         ; download
  SetGlobals MyR4:REAL4, MyW:WORD=-123, MyDw:DWORD=-123
  Init
  PrintLine cfm$("number\tFloor(number)\tCeil(number)")
  PrintLine Str$("%4f", MyW), Str$("\t%5f", Floor(MyW)v), Str$("  \t%5f", Ceil(MyW)v), " (WORD size integer)"
  PrintLine Str$("%4f", MyDw), Str$("\t%5f", Floor(MyDw)v), Str$("  \t%5f", Ceil(MyDw)v), " (DWORD size integer)"
  For_ ecx=0 To 29
        Rand(-99.9, +99.9, MyR4)
        PrintLine Str$("%4f ", MyR4), Str$("\t%5f", Floor(MyR4)v), Str$("  \t%5f", Ceil(MyR4)v)
  Next
EndOfCode


Output:
Code: [Select]
number  Floor(number)   Ceil(number)
-123.0  -123.00         -123.00 (WORD size integer)
-123.0  -123.00         -123.00 (DWORD size integer)
-9.629  -10.0000        -9.0000
-85.50  -86.000         -85.000
-72.59  -73.000         -72.000
72.67   72.000          73.000
31.14   31.000          32.000
64.07   64.000          65.000
-55.86  -56.000         -55.000
57.42   57.000          58.000
-95.26  -96.000         -95.000
3.386   3.0000          4.0000
12.49   12.000          13.000
-27.00  -28.000         -27.000
15.36   15.000          16.000
35.19   35.000          36.000
-49.67  -50.000         -49.000
-70.70  -71.000         -70.000
17.82   17.000          18.000
-7.807  -8.0000         -7.0000
37.56   37.000          38.000
95.73   95.000          96.000
27.80   27.000          28.000
-67.03  -68.000         -67.000
68.09   68.000          69.000
-75.14  -76.000         -75.000
-53.27  -54.000         -53.000
55.16   55.000          56.000
75.63   75.000          76.000
90.65   90.000          91.000
88.01   88.000          89.000
-21.09  -22.000         -21.000

Not included in the current (December '17) release, therefore attached as FloorCeil.inc; you may add it to your MasmBasic.inc.

Note the red v after the Str$("format", Numberv): Floor() and Ceil() always return ST(0) for further processing; if you just print the value, it needs to be popped via fstp st from the FPU. That's what the v does. Of course, you can also pop it directly into another variable, specified as second argument:

  Floor(123.456, MyDw)
  Print Str$("dw(123.456)=%i", MyDw)


The first argument can be an immediate (interpreted as double) or a DWORD, WORD, REAL4 ... REAL10 variable.

tenkey

  • Regular Member
  • *
  • Posts: 15
Missing .inc file
« Reply #439 on: October 25, 2018, 08:52:22 AM »
The second example in the MasmBasic guide fails to assemble due to missing DualWin.inc file. I see LibUsed$() example is also using JBasic.inc instead of MasmBasic.inc. MasmBasic Version 25 Dec 2017.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8826
  • Assembler is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #440 on: October 25, 2018, 11:07:41 AM »
Thanks for the feedback. Did you get any error messages?

Please tell me if the following files are present (this helps me to understand what went wrong):
  \Masm32\MasmBasic\Res\GetPT.exe
  \Masm32\MasmBasic\Res\StructInfo.txt

- Extract the attached files to \Masm32\MasmBasic\Res
- Try again to build the example; if that doesn't work, try to run \Masm32\MasmBasic\Res\GetPT.bat

Note that the 64-bit examples build also with MASM; to do so, add an OPTion as follows:
Code: [Select]
include \masm32\MasmBasic\Res\JBasic.inc ; OPT_Assembler ML
However, RichMasm prefers UAsm and will try to install it if it's not present at \Masm32\bin\Uasm64.exe
You can do that manually, too: install UAsm64, i.e. extract \Masm32\bin\Uasm64.exe

Let me know if it works, please.

2B||!2B

  • Regular Member
  • *
  • Posts: 29
Re: MasmBasic
« Reply #441 on: November 08, 2018, 02:09:01 PM »
Hi jj,

Is there a Unicode Split$() version?

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8826
  • Assembler is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #442 on: November 08, 2018, 07:34:28 PM »
No, but it could be implemented. Something like this works already - not Unicode but Utf8:

include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  Let esi="Введите\nтекст\nздесь"
  For_ ct=0 To Split$(esi, "\n", My$())-1
        PrintLine "[", My$(ct), "]"
  Next
EndOfCode


Output:
Code: [Select]
[Введите]
[текст]
[здесь]

What exactly do you want to do?

2B||!2B

  • Regular Member
  • *
  • Posts: 29
Re: MasmBasic
« Reply #443 on: November 08, 2018, 09:16:31 PM »
Cool, but i don't want to assign my string to a new variable. My string is like "####STRING####STRING" etc
So i use #### as string delimiter.
But i used wInstr and it worked fine for me. Although i have to add a null terminator after each substring, this way i can use the string in it's source.

tenkey

  • Regular Member
  • *
  • Posts: 15
Re: MasmBasic
« Reply #444 on: November 09, 2018, 06:38:43 PM »
Thanks for the feedback. Did you get any error messages?
*** Assemble using \masm32\bin\UAsm64 /c /coff /Zd /Zi /Zf tmp_file.asm ***
UASM v2.46, Dec  8 2017, Masm-compatible assembler.
Portions Copyright (c) 1992-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.

*** using Res\JBasic32.lib ***
\masm32\MasmBasic\Res\JBasic.inc(558) : Error A2106: Cannot open file: "\Masm32\MasmBasic\Res\DualWin.inc" [ENOENT]
 \masm32\MasmBasic\Res\JBasic.inc(558): Included by
  Tmp_File.asm(1): Main line
Quote
Please tell me if the following files are present (this helps me to understand what went wrong):
  \Masm32\MasmBasic\Res\GetPT.exe
  \Masm32\MasmBasic\Res\StructInfo.txt
GetPT.asm exists, but not GetPT.exe.
StructInfo.txt exists.
Quote
- Extract the attached files to \Masm32\MasmBasic\Res
Extracted the files and ran GetPT.exe in a command prompt. Had to  turn off security.
The example (32-bit) assembled and ran.
I re-enabled security and now the file names \masm32\masmbasic\mbguide.exe and \masm32\masmbasic\tmp_file.exe are flagged as suspicious. polink can't create them - access denied.
I can probably create the exe files in another directory, but that means i'll have to use a custom BAT file for each example.

*** Assemble using \masm32\bin\UAsm64 /c /coff /Zd /Zi /Zf tmp_file.asm ***
UASM v2.46, Dec  8 2017, Masm-compatible assembler.
Portions Copyright (c) 1992-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.

*** MasmBasic version 25.12.2017 ***
Tmp_File.asm: 6 lines, 2 passes, 99 ms, 0 warnings, 0 errors

*** Link  MbGuide.obj rsrc.res  using polink, sub Console /debug /mapinfo:lines***
Writing debug information
Compacting CodeView information
POLINK: fatal error: Access is denied.
*** Link error ***

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8826
  • Assembler is fun ;-)
    • MasmBasic
Re: MasmBasic
« Reply #445 on: November 09, 2018, 07:01:33 PM »
So it's all a security thing. If I used an AV, I would simply exclude the whole \Masm32 folder. Assembly code is considered unsafe because it's unusual by the dumb AV brigade, see our dedicated sub-forum AV Software sh*t list 8)