Author Topic: Miscellaneous snippets  (Read 85758 times)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Reading doubles from an array
« Reply #180 on: January 19, 2022, 04:40:42 AM »
include \masm32\MasmBasic\MasmBasic.inc
  SetGlobals REAL8 min8, max8
  Init
  PrintCpu 0
  elements=2000000              ; two Million elements
  Dim MyReal8() As REAL8
  For_ ct=0 To 19
        .if !Exist("MyReals.dat") || ct<5       ; measure creation times for the first 5
                NanoTimer()
                For_ ecx=0 To elements-1
                    Rand(-100, 100, MyReal8(ecx))   ; create a random number between -100 and +100
                Next
                PrintLine Str$("Creating an array of %i random doubles took ", ecx), NanoTimer$()
                ArrayStore "MyReals.dat", MyReal8()
        .endif
        NanoTimer()
        ArrayRead MyReal8(), "MyReals.dat"      ; read array from disk
        ArrayMinMax MyReal8(), min8, max8       ; assign min+max to two variables
        PrintLine Str$("Reading and getting min=%7f", min8), Str$(" and max=%7f took ", max8), NanoTimer$()
  Next
  Inkey "hit any key"
EndOfCode


Output:
Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
Creating an array of 2000000 random doubles took 83 ms
Reading and getting min=-99.99998 and max=99.99964 took 29 ms
Creating an array of 2000000 random doubles took 37 ms
Reading and getting min=-99.99989 and max=100.00000 took 43 ms
Creating an array of 2000000 random doubles took 39 ms
Reading and getting min=-99.99983 and max=99.99999 took 31 ms
Creating an array of 2000000 random doubles took 38 ms
Reading and getting min=-99.99996 and max=99.99998 took 32 ms
Creating an array of 2000000 random doubles took 38 ms
Reading and getting min=-99.99998 and max=99.99991 took 28 ms
Reading and getting min=-99.99998 and max=99.99991 took 27 ms
Reading and getting min=-99.99998 and max=99.99991 took 26 ms
Reading and getting min=-99.99998 and max=99.99991 took 27 ms
Reading and getting min=-99.99998 and max=99.99991 took 26 ms
Reading and getting min=-99.99998 and max=99.99991 took 27 ms
Reading and getting min=-99.99998 and max=99.99991 took 28 ms
Reading and getting min=-99.99998 and max=99.99991 took 25 ms
Reading and getting min=-99.99998 and max=99.99991 took 29 ms
Reading and getting min=-99.99998 and max=99.99991 took 29 ms
Reading and getting min=-99.99998 and max=99.99991 took 27 ms
Reading and getting min=-99.99998 and max=99.99991 took 31 ms
Reading and getting min=-99.99998 and max=99.99991 took 29 ms
Reading and getting min=-99.99998 and max=99.99991 took 28 ms
Reading and getting min=-99.99998 and max=99.99991 took 30 ms
Reading and getting min=-99.99998 and max=99.99991 took 29 ms

See also GB32 forum

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Re: Miscellaneous snippets
« Reply #181 on: February 02, 2022, 08:10:25 AM »
Some benchmarks for GB32

HSE

  • Member
  • *****
  • Posts: 2463
  • AMD 7-32 / i3 10-64
Re: Miscellaneous snippets
« Reply #182 on: February 02, 2022, 08:18:38 AM »
Hi JJ!

Where are your benmarchs of GB32?
Equations in Assembly: SmplMath

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Re: Miscellaneous snippets
« Reply #183 on: February 02, 2022, 08:43:23 AM »
Embedded in the source. The code is based on scalion's alias Nicolas Rey's tests.

Quote
The program consist in 4 test :

Test 1 "Init" - Initialisation of 10000 item of string array with a letter followed by numbers.
Test 2 "Swap" - Swap all items one time with another (not sorting) ->  49 995 000 swaps.
Test 3 "Mid" - Concat each 9999 first items with 2 parts of item+1 : mid(a(i+1),2) + mid(a(i+1),1,1)
Test 4 "Sort" - Sort ascending the array.

HSE

  • Member
  • *****
  • Posts: 2463
  • AMD 7-32 / i3 10-64
Re: Miscellaneous snippets
« Reply #184 on: February 02, 2022, 08:52:33 AM »
Perfect. Thanks  :thumbsup:
Equations in Assembly: SmplMath

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Get drive types
« Reply #185 on: February 04, 2022, 04:22:41 AM »
include \masm32\MasmBasic\MasmBasic.inc         ; download
  SetGlobals buffer[40]:BYTE
  Init
  lea edi, buffer
  invoke GetLogicalDriveStrings, 40, edi
  ; Print HexDump$(edi, 32)
  lea esi, [edi-2]             ; to compensate leading 2*inc
  .While 1
        inc esi
        inc esi
        m2m edx, 32             ; len of buffer
        .Break .if !Instr_(FAST, esi, ":\", 2 or 64)    ; 64=len in edx, 2=ignore case of first char
        lea esi, [eax-1]        ; put esi on the drive letter
        xchg rv(GetDriveType, esi), ecx
        .if Choose(ecx, "unknown", "no root", "removable", "fixed", "remote", "cdrom", "ramdisk")==ChooseString         ; *)
                PrintLine esi, " is type ", eax         ; Str$(" (value %i)", ecx)
       .else
                PrintLine "Drive ", esi, Str$(" is type %i", ecx)
        .endif
  .Endw
EndOfCode

*):
unknown         equ 0
no root         equ 1
DRIVE_REMOVABLE equ 2
DRIVE_FIXED     equ 3
DRIVE_REMOTE    equ 4
DRIVE_CDROM     equ 5
DRIVE_RAMDISK   equ 6


Code: [Select]
C:\ is type fixed
D:\ is type cdrom
E:\ is type removable
J:\ is type fixed

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Convert binary file to dq 123, 456, 789...
« Reply #186 on: April 12, 2022, 11:24:18 PM »
Just for fun :biggrin:

include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  MbHexDumpLen=819200           ; let's be generous (default is 8192 bytes)
  Let esi=FileRead$(CL$())      ; read the file supplied in the commandline
  FileWrite "~testTmp.asm", HexDump$(esi, LastFileSize, dq)     ; use QWORD format
  ShEx "~testTmp.asm"           ; open in your IDE
EndOfCode


Usage: drag any executable (any file, actually) over bin2dq.exe

Output:
Code: [Select]
.DATA
hdBytes=49152
hdStart LABEL BYTE
.radix 16
dq 00000000300905A4D, 00000FFFF00000004, 000000000000000B8, 00000000000000040, 00000000000000000, 00000000000000000, 00000000000000000, 00000008000000000
dq 0CD09B4000EBA1F0E, 0685421CD4C01B821, 072676F7270207369, 06F6E6E6163206D61, 06E75722065622074, 020534F44206E6920, 00A0D0D2E65646F6D, 00000000000000024
...
dq 00D3E796C626D6573, 0000000000000000A, 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000
dq 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000
dq 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000, 00000000000000000
.radix 10
.code
; FileWrite "test.exe", offset hdStart, hdBytes

P.S., to test your result, you can use, for example:

Code: [Select]
include \masm32\MasmBasic\MasmBasic.inc
include ~testTmp.asm

  Init
  FileWrite "bin2test.exe", offset hdStart, hdBytes
  Launch "bin2test.exe"
EndOfCode

If you prefer pure Masm32 SDK for testing, use this:

Code: [Select]
include \masm32\include\masm32rt.inc
include ~testTmp.asm

.code
start:
  invoke write_disk_file, chr$("bin2test.exe"), offset hdStart, hdBytes  ; re-create the exe
  invoke WinExec, chr$("bin2test.exe"), SW_SHOW
  exit
end start

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Buttons
« Reply #187 on: August 15, 2022, 06:22:16 AM »
Attached a program I am just working on, with several different kinds of buttons.

What is interesting: It behaves differently on Win 7 and Win 10. On the latter OS, some of the buttons receive up to 8 WM_PAINT messages in a row. Something is seriously wrong, and I am trying to find out what. It might have to do with a different behaviour of the CDDS_* messages, but I am not yet sure. I will keep you posted.

quarantined

  • Guest
Re: Buttons
« Reply #188 on: August 15, 2022, 06:33:46 AM »
Something is seriously wrong, and I am trying to find out what....

Thanks, but what I posted in the Campus is not supposed to act like a button exactly. It behaves like I intended it to.  :cool:

NoCforMe

  • Member
  • *****
  • Posts: 1104
Re: Miscellaneous snippets
« Reply #189 on: August 15, 2022, 08:08:06 AM »
Well, that's OK. There's no law that says you can't make a static control behave enough like a button to suit your purposes. You won't get a knock on your door in the middle of the night from the Coding Police.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Tiny calculator
« Reply #190 on: August 29, 2022, 07:52:26 PM »
Just for fun - project attached. TinyCalcV2.zip is an advanced version with hex and binary display. It will accept most common number formats as input.

Code: [Select]
GuiParas equ "Tiny calculator", w200, h130, m4, b LiteBlueGreen ; width+height, margins, background colour
include \masm32\MasmBasic\Res\MbGui.asm
  GuiControl Calc, "Edit", bcol LiteYellow, w700, fcol Black, font -14, h0+32 ; colours & fonts have no effect with rtf files
  GuiControl Go, "button", "-!!> clipboard", x700, w300, h0+32, bcol LiteYellow
  GuiControl Status, "statusbar", "Type a formula"
  SetGlobals Result:REAL10, res$
Event Command
  .if MenuID==Go && nCode==BN_CLICKED
SetClip$=res$
invoke SendMessage, hWnd_, WM_CLOSE, 0, 0
  .elseif MenuID==Calc && nCode==EN_CHANGE
xor ecx, ecx
SetWin$ hStatus=0
mov esi, Win$(hCalc)
.if Instr_(esi, "+")
inc ecx
.else
.if Instr_(esi, "-")
inc ecx
inc ecx
.else
.if Instr_(esi, "*")
dec ecx
.else
.if Instr_(esi, "/")
dec ecx
dec ecx
.endif
.endif
.endif
.endif
.if ecx
mov byte ptr [eax], 0 ; separate left and right halves
inc eax
MovVal ST(0), eax ; right half of expression
.if edx && edx<20
MovVal ST(0), esi
Switch_ ecx
Case_ 1: fadd
Case_ 2: fsubr
Case_ -1: fmul
Case_ -2: fdivr
Endsw_
fstp Result
Let res$=Str$(Result)
xchg eax, ecx
push ecx
add ecx, Len(ecx)
.Repeat
dec ecx
.Until byte ptr [ecx]!="0"
mov byte ptr [ecx+2], 0
pop eax
SetWin$ hStatus=Cat$("Result="+res$)
.endif
.endif
  .endif
GuiEnd
« Last Edit: August 30, 2022, 02:52:03 AM by jj2007 »

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Tiny calculator
« Reply #191 on: August 31, 2022, 07:59:58 PM »
This grew quickly, the source is now a whopping 149 lines :biggrin:

Version 3 has a tooltip, knows about 2**3=8 and accepts PI as first parameter. Enjoy :biggrin:



P.S.: Binary result and bit descriptor for e.g. 123*456 look like this:

1101101100011000
5432109876543210

It can be helpful to see which bits are set.