News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Miscellaneous snippets

Started by jj2007, August 20, 2017, 08:02:31 AM

Previous topic - Next topic

jj2007

#105
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

jj2007

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:Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
5078 µs for generating 262144 random doubles
66 ms for 512 strings

jj2007

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: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.

jj2007

The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag. The text of the programming assignment is as follows:

Quote"Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"."

include \masm32\MasmBasic\MasmBasic.inc         ; download
  Init
  For_ ct=1 To 100
        xor esi, esi            ; flag Fizz or Buzz
        mov eax, ct
        cdq
        mov ecx, 3
        div ecx
        test edx, edx
        .if Zero?
                inc esi
                Print "Fizz"
        .endif
        mov eax, ct
        cdq
        mov ecx, 5
        div ecx
        test edx, edx
        .if Zero?
                inc esi
                Print "Buzz"
        .endif
        .if !esi
                Print Str$(ct)
        .endif
        Print " "
  Next
  Inkey "ok?"
EndOfCode

TimoVJL

In C, perhaps it works ::)int __cdecl main(void)
{
char *Fizz = "Fizz";
char *Buzz = "Buzz";
char *pF, *pB;
for (int i=0; i<100; i++) {
pF = 0;
pB = 0;
if (i) {
if (i % 3 == 0) pF = Fizz;
if (i % 5 == 0) pB = Buzz;
}
if (pF || pB) {
if (pF) printf(pF);
if (pB) printf(pB);
printf("\n");
} else printf("%d\n", i);
}
return 0;
}
May the source be with you

aw27

Another C version (may be can be made shorter   ::)):

int main()
{
   char buff[16];
   for (int i = 1; i <= 100; i++)
      printf("%s\n", !(i % 3) && !(i % 5) ? "FizzBuff": (!(i % 5) ? "Buzz": (!(i % 3) ? "Fizz": itoa(i,buff,10))));
}

TimoVJL

#111
Nice!
4 bytes shorter, but not C99.

Jose's version unpacked:
#pragma comment(lib, "msvcrt.lib")
char *_itoa(int value, char *dst, int base);
void __cdecl mainCRTStartup(void)
{
__declspec(dllimport) void __cdecl exit(int status);
int __cdecl main(void);
exit(main());
}
int main(void)
{
   char buff[16];
char *p;
   for (int i = 1; i <= 100; i++) {
if (!(i % 3) && !(i % 5)) p = "FizzBuff";
else if (!(i % 5)) p = "Buzz";
else if (!(i % 3)) p =  "Fizz";
else p = _itoa(i,buff,10);
puts(p);
}
return 0;
}

EDIT:#pragma comment(lib, "msvcrt.lib")
char *_itoa(int value, char *dst, int base);
void __cdecl mainCRTStartup(void)
{
__declspec(dllimport) void __cdecl exit(int status);
int __cdecl main(void);
exit(main());
}
int __cdecl main(void)
{
char *Fizz = "Fizz";
char *Buzz = "Buzz";
char *pF, *pB;
char buf[16];
for (int i=0; i<100; i++) {
pF = 0;
pB = 0;
if (i % 3 == 0) pF = Fizz;
if (i % 5 == 0) pB = Buzz;
if (pF || pB) {
if (pF) printf(pF);
if (pB) printf(pB);
puts("");
} else {
puts(_itoa(i, buf, 10));
}
}
return 0;
}
May the source be with you

jj2007

Choose is purest Assembly :P

include \masm32\MasmBasic\MasmBasic.inc
  Init
  For_ ct=1 To 100
        xor ebx, ebx ; flag Fizz or Buzz
        mov eax, ct
        cdq
        mov ecx, 3
        div ecx
        test edx, edx
        sete bl
        mov eax, ct
        mov ecx, 5
        div ecx
        test edx, edx
        sete dl
        lea ebx, [ebx+2*edx]
        Print Choose(ebx, Str$("%i ", ct), "Fizz ", "Buzz ", "FizzBuzz ")
  Next
  Inkey "ok?"
EndOfCode

aw27

 :( itoa  :biggrin: _itoa  :lol: purest Assembly

jj2007

Certain similarities should be accepted 8)

printf("%s\n", !(i % 3) && !(i % 5) ? "FizzBuff": (!(i % 5) ? "Buzz": (!(i % 3) ? "Fizz": itoa(i,buff,10))));
Print Choose(ebx, Str$("%i ", ct), "Fizz ", "Buzz ", "FizzBuzz ")


Full version:

include Modulo.inc
  Init
  For_ ecx=1 To 30
        lea ebx, [2*IsModulo(ecx, 5)]
        add ebx, IsModulo(ecx, 3)
       PrintLine Choose(ebx, Str$("%i ", ecx), "Fizz ", "Buzz ", "FizzBuzz ")
  Next
  Inkey
EndOfCode


I wonder if it should be called ModZero(src, imm)?

Output:1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz

;)

jj2007

include \masm32\MasmBasic\Res\MbGui.asm
  ArraySet current() As double=120, 125, 122, 119, 134, 138, 141, 159, 162, 178
  ArraySet voltage() As double=250, 210, 180, 155, 140, 125, 110, 100, 90, 80
  SetAxisX "Электроток", s 0, d 1/5, grid 1   ; s=start value, d=difference between gridlines
  SetAxisY "Voltage", s 0, d 5/5, grid 1
Event Paint
  ArrayPlotValues "%i V", 8, -12        ; format$, dx & dy positions
  ArrayPlot current():voltage(), RgbCol(0, 0, 255), lines=3
  ArrayPlot exit, "繪製科學數據非常容易!"
EndOfCode



daydreamer

Impressive Jochen :t
how fast is arrayplot compared to GDI?
is it masmbasic functions used in Lord Adef's game,scrolling and sprite functions included already in masmbasic?
strange if not,because its included GET and PUT in qbasic
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

jj2007

Thanks, Daydreamer. Arrayplot is Gdi, actually. Speedwise, check the Sinus plot template (attached). Re GET & PUT: There are ways to do something similar in MasmBasic, but I doubt that LordAdef is using them.

LiaoMi

Quote from: jj2007 on May 16, 2019, 01:33:12 PM
include \masm32\MasmBasic\Res\MbGui.asm
  ArraySet current() As double=120, 125, 122, 119, 134, 138, 141, 159, 162, 178
  ArraySet voltage() As double=250, 210, 180, 155, 140, 125, 110, 100, 90, 80
  SetAxisX "Электроток", s 0, d 1/5, grid 1   ; s=start value, d=difference between gridlines
  SetAxisY "Voltage", s 0, d 5/5, grid 1
Event Paint
  ArrayPlotValues "%i V", 8, -12        ; format$, dx & dy positions
  ArrayPlot current():voltage(), RgbCol(0, 0, 255), lines=3
  ArrayPlot exit, "繪製科學數據非常容易!"
EndOfCode




Hi jj2007,

is it possible to make smoothing lines? the soul requires aesthetics)
Smoothing a curve in VB.Net https://www.codeproject.com/Questions/358853/Smoothing-a-curve-in-VB-Net

jj2007

Quote from: LiaoMi on May 18, 2019, 06:22:21 PMis it possible to make smoothing lines?

Probably, but the CodeProject guy is on the wrong track. For scientific data visualisation, you can't use "shortcuts" of that sort, they must be exact. What you can do, though, is smoothing a ragged line by anti-aliasing:


If I find the time, I'll find a way to let GdiPlus draw these lines 8)