Author Topic: Miscellaneous snippets  (Read 12320 times)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 9091
  • Assembler is fun ;-)
    • MasmBasic
Animated GIF
« Reply #105 on: January 16, 2019, 02:26:28 AM »
include \masm32\MasmBasic\Res\MbGui.asm
Event Paint
  .if !Exist("test.img")
        Let esi=FileRead$("https://i.gifer.com/6H3K.gif")
        FileWrite "test.img", esi, LastFileSize
  .endif
  GuiImage "test.img", fit
EndOfCode
« Last Edit: February 14, 2019, 01:21:17 PM by jj2007 »

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 9091
  • Assembler is fun ;-)
    • MasmBasic
Creating a matrix of doubles and store it to a text file
« Reply #106 on: February 08, 2019, 09:14:05 PM »
This snippet
- generates a 512*512 matrix of random double values
- writes all values to a string array
- saves the strings to disk

include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  elements=512
  PrintCpu 0
  NanoTimer()
  Dim matrix(elements,elements) As REAL8
  For_ ct=0 To elements-1
        For_ ecx=0 To elements-1
                Rand(-12345.6789, 12345.6789, matrix(ct, ecx))
        Next
  Next
  PrintLine NanoTimer$(), Str$(" for generating %i random doubles", matrix(?))
  NanoTimer()
  Dim out$()                    ; create a zstring array
  Let edi=New$(16*elements)    ; prepare for long numbers, e.g. 12345.6789
  For_ ct=0 To elements-1
        push edi
       For_ ecx=0 To elements-1
                invoke MbCopy, edi, Str$("%9f\t", matrix(ct, ecx)), -2
                xchg eax, edi
        Next
        and dword ptr [edi-1], 0
        pop edi
       Let out$(ct)=edi
  Next
  PrintLine NanoTimer$(), Str$(" for %i strings", ct)
  Store "Matrix.txt", out$()    ; save to disk
  Inkey "wanna see the result? (y)"
  If_ eax=="y" Then ShEx "Matrix.txt"   ; show it in Notepad etc
EndOfCode


Typical output:
Code: [Select]
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
5078 ┬Ás for generating 262144 random doubles
66 ms for 512 strings

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 9091
  • Assembler is fun ;-)
    • MasmBasic
Duplicate bits
« Reply #107 on: February 17, 2019, 02:34:07 PM »
include \masm32\MasmBasic\MasmBasic.inc         ; download

DuplicateBits MACRO arg
Local Lb0, Lb1, Lb2
  mov edx, arg
  xor eax, eax
  push ecx
  mov ecx, 11000000000000000000000000000000b
Lb0:
  rol ecx, 2                            ; in the first round, ecx will be 11b
  shr edx, 1                            ; get the carry flag
  je Lb1
  jnc Lb0
  or eax, ecx
  jmp Lb0
Lb1:
  jnc Lb2
  or eax, ecx
Lb2:
  pop ecx
  EXITM <eax>
ENDM

  Init
  m2m ecx, 1
  mov edi, 256
  .Repeat
        mov esi, ecx
        shr edi, 1
        or esi, edi
        Print Right$(Cat$(Replace$(Bin$(esi), "0", " ")), 8 ), Spc4$     ; print the original without the 0s
        Print Right$(Cat$(Replace$(Bin$(DuplicateBits(esi)), "0", " ")), 16), CrLf$  ; print the duplicate
        shl ecx, 1
  .Until ecx>255
  Inkey CrLf$, "-- hit any key --"
EndOfCode


Output:
Code: [Select]
1      1    11            11
 1    1       11        11
  1  1          11    11
   11             1111
   11             1111
  1  1          11    11
 1    1       11        11
1      1    11            11

Source attached. The macro will also work for plain Masm32 sources.