### Author Topic: Miscellaneous snippets  (Read 80553 times)

#### jj2007

• Moderator
• Member
• Posts: 13274
• Assembly is fun ;-)
##### Animated GIF
« Reply #105 on: January 16, 2019, 02:26:28 AM »
include \masm32\MasmBasic\Res\MbGui.asm
Event Paint
.if !Exist("test.img")
FileWrite "test.img", esi, LastFileSize
.endif
GuiImage "test.img", fit
EndOfCode
« Last Edit: February 14, 2019, 01:21:17 PM by jj2007 »
Masm32 Tips, Tricks and Traps

Some members can't live without insulting others. It tells us a lot about them.

#### jj2007

• Moderator
• Member
• Posts: 13274
• Assembly is fun ;-)
##### 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

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.50GHz5078 µs for generating 262144 random doubles66 ms for 512 strings`
Masm32 Tips, Tricks and Traps

Some members can't live without insulting others. It tells us a lot about them.

#### jj2007

• Moderator
• Member
• Posts: 13274
• Assembly is fun ;-)
##### Duplicate bits
« Reply #107 on: February 17, 2019, 02:34:07 PM »

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        111      1    11            11`
Source attached. The macro will also work for plain Masm32 sources.
Masm32 Tips, Tricks and Traps

Some members can't live without insulting others. It tells us a lot about them.

#### jj2007

• Moderator
• Member
• Posts: 13274
• Assembly is fun ;-)
##### 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”."

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
Masm32 Tips, Tricks and Traps

Some members can't live without insulting others. It tells us a lot about them.

#### TimoVJL

• Member
• Posts: 1223
##### 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

#### aw27

• Guest
##### 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: 1223
##### 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: 13274
• Assembly is fun ;-)
##### 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`
Masm32 Tips, Tricks and Traps

Some members can't live without insulting others. It tells us a lot about them.

#### aw27

• Guest
##### Re: Miscellaneous snippets
« Reply #113 on: May 09, 2019, 03:57:50 AM »
:( itoa  _itoa  :lol: purest Assembly

#### jj2007

• Moderator
• Member
• Posts: 13274
• Assembly is fun ;-)
##### 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)]
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]
`12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829FizzBuzz` ;)
Masm32 Tips, Tricks and Traps

Some members can't live without insulting others. It tells us a lot about them.

#### jj2007

• Moderator
• Member
• Posts: 13274
• Assembly is fun ;-)
##### 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

Masm32 Tips, Tricks and Traps

Some members can't live without insulting others. It tells us a lot about them.

#### daydreamer

• Member
• Posts: 2185
• my kind of REAL10 Blonde
##### 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
my none asm creations
http://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."

#### jj2007

• Moderator
• Member
• Posts: 13274
• Assembly is fun ;-)
##### 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.
Masm32 Tips, Tricks and Traps

Some members can't live without insulting others. It tells us a lot about them.

#### LiaoMi

• Member
• Posts: 1038
##### 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: 13274
• Assembly is fun ;-)
##### 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)
Masm32 Tips, Tricks and Traps

Some members can't live without insulting others. It tells us a lot about them.