Author Topic: Miscellaneous snippets  (Read 20495 times)

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 9733
  • 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: 9733
  • 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: 9733
  • 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.

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 9733
  • Assembler is fun ;-)
    • MasmBasic
FizzBuzz
« Reply #108 on: May 09, 2019, 12:33:31 AM »
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

  • Member
  • ***
  • Posts: 475
Re: Miscellaneous snippets
« Reply #109 on: May 09, 2019, 01:50:29 AM »
In C, perhaps it works ::)
Code: [Select]
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

AW

  • Member
  • *****
  • Posts: 2385
  • Let's Make ASM Great Again!
Re: Miscellaneous snippets
« Reply #110 on: May 09, 2019, 02:21:18 AM »
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

  • Member
  • ***
  • Posts: 475
Re: Miscellaneous snippets
« Reply #111 on: May 09, 2019, 02:59:28 AM »
Nice!
4 bytes shorter, but not C99.

Jose's version unpacked:
Code: [Select]
#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:
Code: [Select]
#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;
}
« Last Edit: May 09, 2019, 05:11:23 AM by TimoVJL »
May the source be with you

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 9733
  • Assembler is fun ;-)
    • MasmBasic
Re: Miscellaneous snippets
« Reply #112 on: May 09, 2019, 03:30:54 AM »
Choose is purest Assembly :P

Code: [Select]
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

AW

  • Member
  • *****
  • Posts: 2385
  • Let's Make ASM Great Again!
Re: Miscellaneous snippets
« Reply #113 on: May 09, 2019, 03:57:50 AM »
 :( itoa  :biggrin: _itoa  :lol: purest Assembly

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 9733
  • Assembler is fun ;-)
    • MasmBasic
Re: Miscellaneous snippets
« Reply #114 on: May 09, 2019, 11:51:40 AM »
Certain similarities should be accepted 8)

Code: [Select]
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:
Code: [Select]
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

  • Moderator
  • Member
  • *****
  • Posts: 9733
  • Assembler is fun ;-)
    • MasmBasic
Plotting data
« Reply #115 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



daydreamer

  • Member
  • ****
  • Posts: 925
  • watch Chebyshev on the backside of the Moon
Re: Miscellaneous snippets
« Reply #116 on: May 18, 2019, 03:54:37 AM »
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
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D

jj2007

  • Moderator
  • Member
  • *****
  • Posts: 9733
  • Assembler is fun ;-)
    • MasmBasic
Re: Miscellaneous snippets
« Reply #117 on: May 18, 2019, 10:29:03 AM »
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

  • Member
  • ****
  • Posts: 586
Re: Plotting data
« Reply #118 on: May 18, 2019, 06:22:21 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

  • Moderator
  • Member
  • *****
  • Posts: 9733
  • Assembler is fun ;-)
    • MasmBasic
Re: Plotting data
« Reply #119 on: May 18, 2019, 07:06:10 PM »
is 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)