Author Topic: Miscellaneous snippets  (Read 324 times)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7750
  • Assembler is fun ;-)
    • MasmBasic
Miscellaneous snippets
« on: August 20, 2017, 08:02:31 AM »
include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  Dim MyInts() As DWORD
  For_ ct=0 To 999
        mov MyInts(ct), Rand(100)       ; 0...99
  Next
  Open "O", #0, "even.txt"
  Open "O", #1, "odd.txt"
  For_ ct=0 To MyInts(?)-1
        test MyInts(ct), 1
        setne al
        movsx ecx, al
        Print #ecx, Str$("%__i ", MyInts(ct))
  Next
  Close
  PrintLine     "even.txt: ", FileRead$("even.txt"), CrLf$
  Inkey         "odd.txt:  ", FileRead$("odd.txt")
EndOfCode


Output (shortened):
Code: [Select]
even.txt:  86  82  22  78   2  56  36  14  58  46  68  16  84  12  94  42   0  16  90  42   8  94  90  52  92  90  42  60  88  86  10  64  3
8  92  16  24  92  10  46  34  28  66  74  92  88  82  40  44  26  84  94  48  32   8   0  44  68  40  50   0   2  12  38  12  82  80   0  7
2  76   0  74   8   0  76  64   8  28  46  10  48   6  94  90  10  84  68  62   2  82  96   0  98  20  36  12  96  14  98   8  96

odd.txt:   45   7  13  65  51  57  67  25  97  63  23  77  87  95  39  57  33  87  55  99   5  47  69  79  87  67  19  95  83  49  21  85  4
9  53   7  39  25  31  49  83  17  65  29  27  99  61  19  67  67  77  49  61  73  79  29  47  31  95  49  45  55  81  71  29   7  79  65  2
3  89  93  19  81  17  19  87  83  63  93  25  75  51  79  75  99   7  55  17  19  57  45  33  83  81  87  51  75  81  79  55  93

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7750
  • Assembler is fun ;-)
    • MasmBasic
A suicide proggie
« Reply #1 on: November 10, 2017, 09:39:13 PM »
Ever tried to kill the current executable? It's not that straightforward, for security reasons, but it can be done with little helpers:
Code: [Select]
include \masm32\include\masm32rt.inc ; pure Masm32

.data?
ThisExe db MAX_PATH dup(?)

.code
start:
  mov edi, offset ThisExe
  invoke lstrcpy, edi, chr$("KillMe.exe ")
  invoke lstrcat, edi, rv(GetCommandLine)  ; this returns the path of the current exe
  invoke WinExec, edi, SW_MINIMIZE
  add edi, 11 ; advance to compensate for <KillMe.exe >
  print "check your disk to see if "
  print edi
  print " was killed"
  exit

end start

Source and binaries attached, must sit in the same folder as the current exe.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7750
  • Assembler is fun ;-)
    • MasmBasic
Re: Miscellaneous snippets
« Reply #2 on: November 12, 2017, 09:14:40 PM »
Playing with inline assembly in FreeBasic, source attached; see discussion. Watch out for the red calls below...

Code: [Select]
Function LeapYearJ(ByVal yea As ULong) As ULong
Asm
  mov ecx, dword ptr [yea]
  test ecx, 3
  setz al
  jne 0f
  mov eax, ecx
  cdq
  push 100
  idiv dword ptr [esp] ' 0, 4, 8, ..., 96, 0
  test edx, edx ' mod 100=0 should set zero flag
  jne 1f
  mov eax, ecx
  cdq
  push 400
  idiv dword ptr [esp]
  pop eax
  dec edx '*** invert the zero flag ***
  sets dl ' sign set if edx was zero
  test dl, dl
1:
  pop eax
  setnz al
0:
  movsx eax, al
  mov [function], eax ' MichaelW
End Asm
End Function
...
Code: [Select]
for i = 1 to years ' assembly function, inlined by compiler
asm int 3 ' forum
lyears=lyears+LeapYearJ(i)
asm nop
asm nop
asm nop
asm nop
next

00407C20        �> �CC                �int3
00407C21        �. �8B45 D0           �mov eax, [ebp-30]
00407C24        �. �8945 D8           �mov [ebp-28], eax
00407C27        �. �C745 DC 00000000  �mov dword ptr [ebp-24], 0
00407C2E        �. �C70424 24A04000   �mov dword ptr [esp], offset 0040A02 ; ASCII "C:\FreeBasic\tmp\TmpFile.bas"
00407C35        �. �E8 169EFFFF       �call 00401A50                       ; �TmpFile.00401A50
00407C3A        �. �56                �push esi                            ; �Arg1
00407C3B        �. �8945 C0           �mov [ebp-40], eax                   ; �
00407C3E        �. �C70424 80A04000   �mov dword ptr [esp], offset 0040A08 ; �ASCII "LEAPYEARJ"
00407C45        �. �E8 669EFFFF       �call 00401AB0                       ; �TmpFile.00401AB0
00407C4A        �. �57                �push edi
00407C4B        �. �8945 BC           �mov [ebp-44], eax
00407C4E        �. �8B4D D8           �mov ecx, [ebp-28]
00407C51        �. �F7C1 03000000     �test ecx, 00000003
00407C57        �. �0F94C0            �setz al
00407C5A        �.�75 22             �jnz short 00407C7E
00407C5C        �. �89C8              �mov eax, ecx
00407C5E        �. �99                �cdq
00407C5F        �. �6A 64             �push 64
00407C61        �. �F73C24            �idiv dword ptr [esp]
00407C64        �. �85D2              �test edx, edx
00407C66        �.�75 12             �jnz short 00407C7A
00407C68        �. �89C8              �mov eax, ecx
00407C6A        �. �99                �cdq
00407C6B        �. �68 90010000       �push 190
00407C70        �. �F73C24            �idiv dword ptr [esp]
00407C73        �. �58                �pop eax
00407C74        �. �4A                �dec edx
00407C75        �. �0F98C2            �sets dl
00407C78        �. �84D2              �test dl, dl
00407C7A        �> �58                �pop eax
00407C7B        �. �0F95C0            �setnz al
00407C7E        �> �0FBEC0            �movsx eax, al
00407C81        �. �8945 DC           �mov [ebp-24], eax
00407C84        �. �8B7D BC           �mov edi, [ebp-44]
00407C87        �. �893C24            �mov [esp], edi
00407C8A        �. �E8 219EFFFF       �call 00401AB0                       ; �TmpFile.00401AB0
00407C8F        �. �50                �push eax                            ; �Arg1
00407C90        �. �8B45 C0           �mov eax, [ebp-40]                   ; �
00407C93        �. �890424            �mov [esp], eax                      ; �
00407C96        �. �E8 B59DFFFF       �call 00401A50                       ; �TmpFile.00401A50
00407C9B        �. �50                �push eax                            ; �Arg1
00407C9C        �. �8B45 DC           �mov eax, [ebp-24]                   ; �
00407C9F        �. �0145 D4           �add [ebp-2C], eax                   ; �
00407CA2        �. �90                �nop                                 ; �
00407CA3        �. �90                �nop                                 ; �
00407CA4        �. �90                �nop                                 ; �
00407CA5        �. �90                �nop                                 ; �
00407CA6        �. �8B45 D0           �mov eax, [ebp-30]                   ; �
00407CA9        �. �40                �inc eax                             ; �
00407CAA        �. �8945 D0           �mov [ebp-30], eax                   ; �
00407CAD        �. �3D 80969800       �cmp eax, 989680                     ; �
00407CB2        �.�0F86 68FFFFFF     �jbe 00407C20                        ; �

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7750
  • Assembler is fun ;-)
    • MasmBasic
List of files in a zip archive
« Reply #3 on: November 26, 2017, 10:55:33 PM »
Build it, then drag an archive over the exe:

include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  UnzipInit CL$()
  .if !Sign?
        For_ ecx=0 To edx-1
                PrintLine Files$(ecx)
        Next

  .endif
  Inkey CrLf$, "Now copy the names from the console menu"
EndOfCode


Output e.g. from the COM interface archive:
Code: [Select]
GetComInterface.asc
GetComInterface.dll

aw27

  • Member
  • ****
  • Posts: 854
  • Let's Make ASM Great Again!
Re: A suicide proggie
« Reply #4 on: November 27, 2017, 02:08:34 AM »
Ever tried to kill the current executable? It's not that straightforward, for security reasons, but it can be done with little helpers:
Code: [Select]
include \masm32\include\masm32rt.inc ; pure Masm32

.data?
ThisExe db MAX_PATH dup(?)

.code
start:
  mov edi, offset ThisExe
  invoke lstrcpy, edi, chr$("KillMe.exe ")
  invoke lstrcat, edi, rv(GetCommandLine)  ; this returns the path of the current exe
  invoke WinExec, edi, SW_MINIMIZE
  add edi, 11 ; advance to compensate for <KillMe.exe >
  print "check your disk to see if "
  print edi
  print " was killed"
  exit

end start

Source and binaries attached, must sit in the same folder as the current exe.

Come on, JJ  :badgrin: :badgrin:

Try this instead: http://masm32.com/board/index.php?topic=6713.0

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7750
  • Assembler is fun ;-)
    • MasmBasic
Set wallpaper with IActiveDesktop::SetWallpaper
« Reply #5 on: December 13, 2017, 10:45:07 AM »
This should work but it doesn't :(

It builds fine and returns S_OK, no error, but it does not set the wallpaper. MSDN says it's supported for XP and earlier - seriously? But even on XP it has no effect.

include \masm32\MasmBasic\MasmBasic.inc
include IActiveDesktop.inc      ; IActiveDesktop STRUCT, CLSID_ActiveDesktop, IID_IActiveDesktop (attached)
.code
SetMyWallpaper proc pDesktopImg  ; adapted from johnsa's code
LOCAL pAD
  invoke CoCreateInstance, addr CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, addr IID_IActiveDesktop, addr pAD
  .if eax==S_OK
       CoInvoke pAD, IActiveDesktop.SetWallpaper, pDesktopImg, 0
       deb 4, "SetWp", $Err$()         ; OK, operation completed - but no effect
       CoInvoke pAD, IActiveDesktop.Release
  .endif
  ret
SetMyWallpaper endp
  Init
  If_ Not rv(CoInitializeEx, 0, COINIT_APARTMENTTHREADED) Then invoke SetMyWallpaper, wChr$("\Masm32\examples\exampl04\car\car.jpg")
  invoke CoUninitialize
EndOfCode

Vortex

  • Member
  • *****
  • Posts: 1729
Re: Miscellaneous snippets
« Reply #6 on: December 14, 2017, 05:41:02 AM »
Hi Jochen,

Can you try the attached sample?

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7750
  • Assembler is fun ;-)
    • MasmBasic
Re: Miscellaneous snippets
« Reply #7 on: December 14, 2017, 06:56:10 AM »
Hi Erol,
Your example changed slightly the position of the existing wallpaper, but the old image remained...
To be continued ;-)

Vortex

  • Member
  • *****
  • Posts: 1729
Re: Miscellaneous snippets
« Reply #8 on: December 14, 2017, 07:07:05 AM »
Hi Jochen,

Not sure but maybe specifying the full path of the image can solve the problem.

Code: [Select]
WSTR        DesktopImg,"test.bmp"
.
.
coinvoke    pAD,IActiveDesktop,SetWallpaper,OFFSET DesktopImg,0

GetFullPathName can retrieve the full path.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 7750
  • Assembler is fun ;-)
    • MasmBasic
Re: Miscellaneous snippets
« Reply #9 on: December 14, 2017, 08:09:15 AM »
Hi Erol,

Full path works, thanks a lot! Now I'll try my version 8)

P.S.: It works ;)

include \masm32\MasmBasic\MasmBasic.inc
include IActiveDesktop.inc      ; IActiveDesktop STRUCT, CLSID_ActiveDesktop, IID_IActiveDesktop
.code
SetMyWallpaper proc pDesktopImg         ; adapted from Vortex and johnsa's code
LOCAL pAD, wpo:WALLPAPEROPT
  ClearLocals
  .if !rv(CoCreateInstance, addr CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER, addr IID_IActiveDesktop, addr pAD)
        add wpo.dwSize, WALLPAPEROPT
        add wpo.dwStyle, WPSTYLE_CENTER or WPSTYLE_STRETCH
        CoInvoke pAD, IActiveDesktop.SetWallpaper, wRec$(pDesktopImg), 0
        CoInvoke pAD, IActiveDesktop.SetWallpaperOptions, addr wpo, 0
        CoInvoke pAD, IActiveDesktop.ApplyChanges, AD_APPLY_ALL
        CoInvoke pAD, IActiveDesktop.Release
  .endif
  ret
SetMyWallpaper endp
  Init
  Let esi=wCL$()
  .if !rv(CoInitializeEx, 0, COINIT_APARTMENTTHREADED)
        wMsgBox 0, esi, "Set as wallpaper?", MB_OKCANCEL or MB_ICONQUESTION
        If_ eax==IDOK Then <invoke SetMyWallpaper, Qtrim$(Utf8$(esi))>
        invoke CoUninitialize
  .endif
EndOfCode


Full project attached, including a really nice wallpaper - a very special view of a European city. Extract all files to a folder, then drag the jpg over the exe :biggrin:
« Last Edit: December 14, 2017, 03:11:03 PM by jj2007 »