Author Topic: Miscellaneous snippets  (Read 4497 times)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8507
  • Assembler is fun ;-)
    • MasmBasic
GetRandomStrings
« Reply #30 on: March 15, 2018, 01:30:08 AM »
Could be useful for testing sort algos etc:

include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  Dll "RandomStrings"                   ; the DLL must be in the same folder
  Declare RandomString
  .Repeat
        For_ ecx=0 To 5
                Print RandomString(), " "
        Next
        Inkey cfm$("- more (no=n)?\n")
  .Until eax=="n" || eax==VK_ESCAPE
EndOfCode


Output:
Code: [Select]
imprisoning barked channel superstitious practical spectre - more (no=n)?
currants shower agitation lions motherly forces - more (no=n)?
negus proofs devil chose observation individuals - more (no=n)?
quarrelled whistled place commanded stipulation cannot - more (no=n)?
deeds settled taught verdict unsocial goeth - more (no=n)?
nefarious herald extinguishes accounted system military - more (no=n)?
worse colouring attacks transfixed hearts binding - more (no=n)?
instruments rally singly sword until profit - more (no=n)?
tranquilly luring convinced unchecked trade hears - more (no=n)?
violent packet sternly tearing kneeled crumbled - more (no=n)?
... etc ...

anunitu

  • Member
  • *****
  • Posts: 1018
Re: Miscellaneous snippets
« Reply #31 on: March 15, 2018, 03:33:13 AM »
i am thinking JJ is the REAL MASM Wizard for sure.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8507
  • Assembler is fun ;-)
    • MasmBasic
Re: Miscellaneous snippets
« Reply #32 on: March 15, 2018, 05:37:33 AM »
Thanks - too much honour :icon_redface:

Here is version 2, with pseudo and real random modes (and 20,000 words for choice):

include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  Dll "RandomStrings"                   ; the DLL must be in the same folder
  Declare RandomString, 1
  .Repeat
        For_ ecx=0 To 5
                Print RandomString(0), " "      ; 0=pseudo, 1=real random
        Next
        Inkey cfm$("- more (no=n)?\n")
  .Until eax=="n" || eax==VK_ESCAPE
EndOfCode


In mode 0, the sequence is always identical. It looks random, and is random according to some popular measures of randomness, but you'll always see the same strings. This is particular useful if, for example, your sort algo stumbles over a specific word, and you want to debug that event. It's also a bit faster - in "real" mode, one Million strings need almost one second on my i5. The pseudo mode is 70 times faster.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8507
  • Assembler is fun ;-)
    • MasmBasic
Tweety rotates
« Reply #33 on: April 26, 2018, 12:24:16 PM »
Little demo for image rotation (strangely enough, this GdiPlus function has never been used in this forum ::)):

GuiParas equ "Image rotation demo", w400, h440
include \masm32\MasmBasic\Res\MbGui.asm
  GuiImageCallback Rotate       ; user-defined function
  SetGlobals halfWidth, halfHeight, REAL4:rotateAngle, movX, movY
Event Paint
  imul eax, GuiWidth, 100       ; width*100/128
  sar eax, 8
  add eax, 20
  mov halfWidth, eax
  imul eax, GuiHeight, 100      ; height*100/128
  sar eax, 8
  mov halfHeight, eax
  GuiImage 99, 0, 0, halfWidth, halfHeight      ; res ID, x, y, w, h 
  GuiCls                               ; trigger next paint event
EndOfEvents
Rotate:
  fild halfWidth                        ; an integer
  fadd FP4(35.0)                        ; add a fixed x offset
  fstp movX                             ; Gdi+ needs a float
  fild halfHeight
  fadd FP4(32.0)
  fstp movY
  gdi+ GdipTranslateWorldTransform, giGraphics, movX, movY, MatrixOrderPrepend  ; move to the middle
  gdi+ GdipRotateWorldTransform, giGraphics, rotateAngle, MatrixOrderPrepend    ; rotate
  fld rotateAngle                       ; add one degree for next paint event
  fadd FP4(1.0)
  fstp rotateAngle
  ret
GuiEnd

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8507
  • Assembler is fun ;-)
    • MasmBasic
Where do you live?
« Reply #34 on: May 03, 2018, 09:39:29 AM »
include \masm32\MasmBasic\MasmBasic.inc         ; download
include Geo.inc
  Init
  PrintLine "GEO_LCID:   ", Tb$, Geo$(GEO_LCID)
  PrintLine "GEO_RFC1766:", Tb$, Geo$(GEO_RFC1766), CrLf$
  Inkey "I live in ", Geo$(), " (aka ", Geo$(GEO_OFFICIALNAME), ") at ", Geo$(GEO_LATITUDE), " North, ", Geo$(GEO_LONGITUDE), " East"
EndOfCode


Output:
Code: [Select]
GEO_LCID:       00000809
GEO_RFC1766:    en-it

I live in Italia (aka Repubblica italiana) at 42.768 North, 12.492 East

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8507
  • Assembler is fun ;-)
    • MasmBasic
Push & Pop xmm registers
« Reply #35 on: May 06, 2018, 08:03:06 PM »
Inspired by the Campus post PUSH & POP question by DayDreamer:

include \Masm32\MasmBasic\Res\JBasic.inc        ; ## builds in 32- or 64-bit mode with ML and UAsm
include PushPopXmm.inc
.code
xvalue0         OWORD 11111111222222223333333344444444h
xvalue1         OWORD 55555555666666667777777788888888h
Init           ; OPT_64 1      ; put 0 for 32 bit, 1 for 64 bit assembly
  PrintLine Chr$("This code was assembled with ", @AsmUsed$(1), " in ", jbit$, "-bit format")
  movups xmm0, xvalue0
  movups xmm7, xvalue1
  usedeb=1
  deb 4, "before", x:xmm0, x:xmm7
  ppxInit
  PushX xmm0
  PushX xmm7
  PopX xmm0                     ; note the order: we
  PopX xmm7                     ; are swapping values
  deb 4, "popped & swapped", x:xmm0, x:xmm7
  ; PopX xmm0                           ; uncomment to see the error handling
  MsgBox 0, "Wow, it works!!!!", "Hi", MB_OK or MB_SETFOREGROUND
EndOfCode


Output:
Code: [Select]
This code was assembled with UAsm64 in 64-bit format
before
x:xmm0  3333333344444444h
x:xmm7  7777777788888888h
popped & swapped
x:xmm0  7777777788888888h
x:xmm7  3333333344444444h

Attached the macro and a standard MasmBasic example.

daydreamer

  • Member
  • ***
  • Posts: 466
Re: Miscellaneous snippets
« Reply #36 on: May 07, 2018, 01:02:13 AM »
great macros :t
Quote from Flashdance
Nick  :  When you give up your dream, you die.
*wears a flameproof asbestos suit*

HSE

  • Member
  • ****
  • Posts: 710
  • <AMD>< 7-32>
Re: Where do you live?
« Reply #37 on: May 07, 2018, 05:44:32 AM »

Output:
Code: [Select]
GEO_LCID:       00000809
GEO_RFC1766:    en-it

I live in Italia (aka Repubblica italiana) at 42.768 North, 12.492 East
Are tou sure?

 :biggrin:
Code: [Select]
GEO_LCID:       00000C0A
GEO_RFC1766:    es-es

I live in España (aka Reino de España) at 40.396 North, -3.551 East

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8507
  • Assembler is fun ;-)
    • MasmBasic
GeoInfo
« Reply #38 on: May 07, 2018, 08:08:04 AM »
Well, I do live in Italy, but I haven't check how exact the LatLong figures are. Any idea how the OS determines a user's position?

HSE

  • Member
  • ****
  • Posts: 710
  • <AMD>< 7-32>
Re: GeoInfo
« Reply #39 on: May 07, 2018, 09:28:14 AM »
Any idea how the OS determines a user's position?

OS don't know user's position. I think it's just a point in country center. You declare country when setting language and location. In spanish language España it's default country. In italian language... let me think... mmh  ::)

daydreamer

  • Member
  • ***
  • Posts: 466
Re: GeoInfo
« Reply #40 on: May 07, 2018, 02:47:29 PM »
Well, I do live in Italy, but I haven't check how exact the LatLong figures are. Any idea how the OS determines a user's position?
Same as timezone= the capital of your country?cant you check Rome in google maps and see if its same long and latitude?
Quote from Flashdance
Nick  :  When you give up your dream, you die.
*wears a flameproof asbestos suit*

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8507
  • Assembler is fun ;-)
    • MasmBasic
Re: GeoInfo
« Reply #41 on: May 07, 2018, 05:08:02 PM »
Same as timezone= the capital of your country?cant you check Rome in google maps and see if its same long and latitude?

Yes, you can: Enter the result as e.g. 42.768, 12.492 into Google Maps. It seems that the Geo$() function returns the middle of the country. In theory, you can fine-tune your location, but it's a per user setting in the Control Panel, be careful.

LordAdef

  • Member
  • ****
  • Posts: 588
Re: Miscellaneous snippets
« Reply #42 on: May 09, 2018, 04:30:36 PM »
Quote
GEO_LCID:       00000416
GEO_RFC1766:    pt-us

I live in Estados Unidos (aka Estados Unidos da América) at 39.450 North, -98.908 East

Hey, the little guy placed me in the US!

daydreamer

  • Member
  • ***
  • Posts: 466
Re: Miscellaneous snippets
« Reply #43 on: May 11, 2018, 04:17:15 AM »
Curious if masm or masm basic also supports print in hexadecimal like c++  cout has?
Quote from Flashdance
Nick  :  When you give up your dream, you die.
*wears a flameproof asbestos suit*

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 8507
  • Assembler is fun ;-)
    • MasmBasic
Re: Miscellaneous snippets
« Reply #44 on: May 11, 2018, 06:28:44 AM »
Curious if masm or masm basic also supports print in hexadecimal like c++  cout has?

Sure:

include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  mov ecx, 123456789
  Print Str$("Decimal:\t%i\n", ecx)
  mov esi, 12345678h
  PrintLine "Hexadecimal:", Tb$, Hex$(esi)
  movd xmm0, esi
  pshufd xmm0, xmm0, 0
  deb 4, "for debugging", ecx, x:esi, x:xmm0
  Inkey "-- hit any key --"
EndOfCode


Code: [Select]
Decimal:        123456789
Hexadecimal:    12345678

for debugging
ecx             123456789
x:esi           12345678
x:xmm0          12345678 12345678 12345678 12345678

The Masm32 SDK has the hex$() macro, e.g. as print hex$(esi), 13, 10