flat assembler
Message board for the users of flat assembler.

Index > Main > Getting started "this way"

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
schlukhash



Joined: 07 Nov 2013
Posts: 9
schlukhash
Hello all!

Im new to FASM, and besides SIMD and math skills i havnt got much going for me.

My goal is to implement the simd routines ive written over the years, and wrap a gui around them so that I can further my hobby as a vst programmer. And be platform independent at the same time.

Are there any places i should start reading up on gui and macro based programming? To accheive a good bedding.
Also is there an instruction chart i can print and store somewhere?

All the best.
Post 07 Nov 2013, 14:02
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 19:10; edited 1 time in total
Post 07 Nov 2013, 14:06
View user's profile Send private message Reply with quote
schlukhash



Joined: 07 Nov 2013
Posts: 9
schlukhash
It is a good way to start, Thanks!

"Haha, that will surely help you!"

processing signals Very Happy
Post 07 Nov 2013, 15:29
View user's profile Send private message Reply with quote
schlukhash



Joined: 07 Nov 2013
Posts: 9
schlukhash
Jeez what a day, completely worn out cant do nothing with it yet.. Laughing
Post 08 Nov 2013, 16:31
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
You will want to look at Intel 's Developer Manual Volumes 1 and 2(A/B) and AMD Programmer's Manual Volumes 3 and 4. Another useful quick reference can be found here: http://ref.x86asm.net/.

The macro syntax is specific to FASM - you can see examples in the includes subdirectory or look/ask around this forum.
Post 08 Nov 2013, 17:52
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
schlukhash wrote:

Im new to FASM, and besides SIMD and math skills i havnt got much going for me.


I am new to SIMD and would be greatly looking forward to read some tutorials here about it and about mathematics background. Wink

A very warm welcome ! Cool
Post 08 Nov 2013, 18:56
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7796
Location: Kraków, Poland
Tomasz Grysztar
schlukhash wrote:
Also is there an instruction chart i can print and store somewhere?
Please try this one: http://board.flatassembler.net/topic.php?t=7803
I think it is the best one that was ever posted here.
Post 08 Nov 2013, 19:28
View user's profile Send private message Visit poster's website Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
Quote:
SIMD + math

That's my kind of thing! Smile
If you want to look into the simd instructions, I would suggest fractal image generation (mandelbrot, julia sets, ect.). I have posted several fractal generators throughout the board, and the best one so far is at
http://board.flatassembler.net/topic.php?t=15436
Post 09 Nov 2013, 08:30
View user's profile Send private message Reply with quote
schlukhash



Joined: 07 Nov 2013
Posts: 9
schlukhash
@ cod3b453/Tomasz Grysztar Thanks! to the printer it goes.

@ shutdownall, Given I have some free time next weekend ill be happy to oblige. Think it will be simple to get along with it. For audio I usually move 4 32bit words around, been learning something about double words from tthsqe's example.

@ tthsqe, Those are some nice fractals. One of the best ive seen in years! Seems the ASM has no trouble what so ever crunching those numbers Very Happy With the example i get lost trying to find out how gui implementations work. Are there some examples around on how to load a bitmap, or create a mouse clickable area "button"?
Post 10 Nov 2013, 15:13
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
schlukhash wrote:
Are there some examples around on how to load a bitmap, or create a mouse clickable area "button"?


Download the examples in flatassemblers example section:
http://www.flatassembler.net/examples.php

I had many ideas from examples.
You can compile the fasm/examples installed with FASM too very easy (just load, compile and execute).
Post 10 Nov 2013, 21:05
View user's profile Send private message Send e-mail Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
schlukhash,
I don't think the standard fasm examples include something about bitmaps. It does take a while to learn the windowz api functions, and I have converged on the following method. This example draws an animated ellipse (the GUI specific code is after the Start label):
Code:
format PE GUI 5.0 
entry Start 

include 'win32axp.inc' 

DIB_RGB_COLORS = 0 


section '.code' code readable executable 


                      align  16 
SquaredDistanceEstimate:  ; plotting F(x,y)=0 
                          ; given x at xmm0 and y at xmm1 (double precision), this should return F(x,y)^2/(gradF(x,y).gradF(x,y)) in xmm0 (single precision) 
                          ; for F(x,y) = x^2+a y^2-1, this is (x^2+a y^2-1)^2/((2x)^2+(2a y)^2) 
                      mulsd  xmm0,xmm0 
                      mulsd  xmm1,xmm1 
                      mulsd  xmm1,[a] 
                      movsd  xmm2,xmm1 
                      mulsd  xmm2,[a] 
                      addsd  xmm1,xmm0 
                      addsd  xmm2,xmm0 
                      subsd  xmm1,[.const_1] 
                      mulsd  xmm1,xmm1 
                      mulsd  xmm2,[.const_4] 
                   cvtsd2ss  xmm0,xmm1 
                   cvtsd2ss  xmm1,xmm2 
                      rcpss  xmm1,xmm1 
                      mulss  xmm0,xmm1 
                        ret 

                      align  8 
        .const_1: dq 1.0 
        .const_4: dq 4.0 

                      align  16 
ConvertToGrey:      ;  given # at xmm0, convert to color in eax 
                    ;  min(255,1.5*255/(1+#)) 
                        mov  eax,3*128 
                        mov  ecx,255 
                   cvtsi2ss  xmm1,eax 
                      rcpss  xmm0,xmm0 
                      mulss  xmm1,xmm0 
                   cvtss2si  eax,xmm1 
                        cmp  eax,ecx 
                      cmova  eax,ecx 
                        mov  ecx,0x00010101 
                       imul  ecx 
                        ret 


; these are the size of the plotted region and the width of the line 
                      align  8 
         scale dq 2.0 
         width dq 1.0 

DrawBitmap:            push  ebp 
                        mov  ebp,esp 
                        sub  esp,8*8 

virtual at ebp-8*8 

 xmin  dq ? 
 xmax  dq ? 
 xstep dq ? 
 ymin  dq ? 
 ymax  dq ? 
 ystep dq ? 
 fac   dd ? 

end virtual 


                ; a = 2 + sin(t/500) 

                     invoke  GetTickCount 
                        mov  dword[esp-4],eax 
                       fild  dword[esp-4] 
                        mov  dword[esp-4],500 
                       fild  dword[esp-4] 
                      fdivp  st1,st0 
                       fsin 
                       fld1 
                       fadd  st1,st0 
                      faddp  st1,st0 
                       fstp  qword[a] 



                   ; find the window 
                       fild  [Bitmap.x] 
                       fild  [Bitmap.y] 
                      fdivp  st1,st0 
                      fsqrt 
                        fld  [scale] 
                        fld  st0 
                       fmul  st0,st2 
                        fst  [xmax] 
                       fchs 
                       fstp  [xmin] 
                     fdivrp  st1,st0 
                        fst  [ymax] 
                       fchs 
                       fstp  [ymin] 

                   ; find the pixel fac 
                       fild  [Bitmap.x] 
                       fild  [Bitmap.y] 
                      fmulp  st1,st0 
                        fld  [xmax] 
                        fld  [xmin] 
                      fsubp  st1,st0 
                        fld  [ymax] 
                        fld  [ymin] 
                      fsubp  st1,st0 
                      fmulp  st1,st0 
                        fld  [width] 
                      fmulp  st1,st0 
                      fdivp  st1,st0 
                       fstp  [fac] 

                   ; find xstep and ystep 
                        fld  [xmax] 
                        fld  [xmin] 
                      fsubp  st1,st0 
                       fild  [Bitmap.x] 
                      fdivp  st1,st0 
                       fstp  [xstep] 
                        fld  [ymax] 
                        fld  [ymin] 
                      fsubp  st1,st0 
                       fild  [Bitmap.y] 
                      fdivp  st1,st0 
                       fstp  [ystep] 

                   ; fill the bitmap 
                        mov  ebx,[ppvBits] 
                        mov  esi,[Bitmap.y] 
            .l1:        mov  edi,[Bitmap.x] 
              .l2:    movsd  xmm0,[xmax] 
                      movsd  xmm1,[ymax] 
                   cvtsi2sd  xmm2,edi 
                   cvtsi2sd  xmm3,esi 
                      mulsd  xmm2,[xstep] 
                      mulsd  xmm3,[ystep] 
                      subsd  xmm0,xmm2     ; x 
                      subsd  xmm1,xmm3     ; y 
                       call  SquaredDistanceEstimate 
                      mulss  xmm0,[fac] 
                       call  ConvertToGrey 
                        mov  [ebx],eax 
                        add  ebx,4 
                        sub  edi,1 
                        jnz  .l2 
                        sub  esi,1 
                        jnz  .l1 


                        mov  esp,ebp 
                        pop  ebp 
                        ret 





;;;;;;;;;;;;;;;;;;;; this is all standard stuff to open the window ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 

Start: 

                     invoke  GetModuleHandle,0 
                        mov  [wc.hInstance],eax 
                        mov  [hInstance],eax 
                     invoke  LoadIcon,0,IDI_APPLICATION 
                        mov  [wc.hIcon],eax 
                     invoke  LoadCursor,0,IDC_ARROW 
                        mov  [wc.hCursor],eax 
                     invoke  RegisterClass,wc 
                       test  eax,eax 
                         jz  Error 
                     invoke  CreateWindowEx,0,MainWindowClass,MainWindowTitle,WS_VISIBLE+WS_OVERLAPPEDWINDOW,100,100,600,600,NULL,NULL,[wc.hInstance],NULL 
                        mov  [hMainWindow],eax 
                       test  eax,eax 
                         jz  Error 

  .MsgLoop:          invoke  PeekMessage,msg,NULL,0,0,PM_REMOVE 
                        cmp  [msg.message],WM_QUIT 
                         je  .EndLoop 
                     invoke  TranslateMessage,msg 
                     invoke  DispatchMessage,msg 
                        add  byte[PaintTimer],8 
                        jnz  .MsgLoop 
                       call  Paint 
                     invoke  Sleep,20 
                        jmp  .MsgLoop 
  .EndLoop: 
                     invoke  ExitProcess,[msg.wParam] 

Error:               invoke  MessageBox,NULL,_error,NULL,MB_ICONERROR+MB_OK 
                     invoke  ExitProcess,0 



proc WindowProc hwnd,wmsg,wparam,lparam 

                       push  esi edi ebx 

                        cmp  [wmsg],WM_CREATE 
                         je  .wmcreate 
                        cmp  [wmsg],WM_PAINT 
                         je  .wmpaint 
                        cmp  [wmsg],WM_SIZE 
                         je  .wmsize 
                        cmp  [wmsg],WM_DESTROY 
                         je  .wm_destroy 

  .defwndproc:       invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam] 
                        jmp  .return 

  .wmcreate: 

  .wmsize:           invoke  GetClientRect,[hwnd],rect 
                        mov  eax,[rect.right] 
                       test  eax,eax 
                         jz  .returnz 
                        mov  eax,[rect.right] 
                        mov  ecx,[rect.bottom] 
                        mov  [Bitmap.x],eax 
                        mov  [Bitmap.y],ecx 
                        mov  ecx,[hBitmap] 
                       test  ecx,ecx 
                         jz  .wmpaint_newbm 
                        jmp  .wmpaint_redobm 

  .wmpaint:          invoke  GetClientRect,[hMainWindow],rect 
                        mov  eax,[rect.right] 
                        mov  ecx,[hBitmap] 
                       test  eax,eax 
                         jz  .returnz 
                       test  ecx,ecx 
                         jz  .wmpaint_newbm 
    .wmpaint_ok:       call  Paint 
                        jmp  .returnz 
    .wmpaint_redobm: invoke  DeleteObject,[hBitmap] 
    .wmpaint_newbm:     mov  [bmiheader.biSize],sizeof.BITMAPINFOHEADER 
                        mov  eax,[Bitmap.x] 
                        mov  [bmiheader.biWidth],eax 
                        mov  eax,[Bitmap.y] 
                        neg  eax 
                        mov  [bmiheader.biHeight],eax 
                        mov  [bmiheader.biPlanes],1 
                        mov  [bmiheader.biBitCount],32 
                        mov  [bmiheader.biCompression],BI_RGB 
                        mov  eax,[Bitmap.x] 
                       imul  eax,[Bitmap.y] 
                        lea  eax,[4*eax+16] 
                        mov  [bmiheader.biSizeImage],eax 
                        mov  [bmiheader.biXPelsPerMeter],0 
                        mov  [bmiheader.biYPelsPerMeter],0 
                        mov  [bmiheader.biClrUsed],0 
                        mov  [bmiheader.biClrImportant],0 
                     invoke  CreateDIBSection,0,bmiheader,DIB_RGB_COLORS,ppvBits,0,0 
                        mov  [hBitmap],eax 
                        jmp  .wmpaint_ok 

.wm_destroy:         invoke  PostQuitMessage,0 

.returnz:               xor  eax,eax 
.return:                pop  ebx edi esi 
                        ret 
endp 




Paint:                  cmp  [hBitmap],0 
                         je  .Done 

                     invoke  InvalidateRect,[hMainWindow],rect,FALSE 
                     invoke  BeginPaint,[hMainWindow],ps 
                        mov  [hdc],eax 
                     invoke  CreateCompatibleDC,eax 
                       test  eax,eax 
                         jz  Error 
                        mov  [hMemDC],eax 

                       call  DrawBitmap 

                     invoke  SelectObject,[hMemDC],[hBitmap] 
                     invoke  BitBlt,[hdc],0,0,[rect.right],[rect.bottom],[hMemDC],0,0,SRCCOPY 
                     invoke  DeleteDC,[hMemDC] 
                     invoke  EndPaint,[hMainWindow],ps 

        .Done:          ret 






section '.idata' import data readable writeable 

 library kernel32,'KERNEL32.DLL',\ 
         user32,'USER32.DLL',\ 
         gdi32,'GDI32.DLL' 

include 'api\kernel32.inc' 
include 'api\user32.inc' 
include 'api\gdi32.inc' 


section '.data' data readable writeable 


align 4 

  wc WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,COLOR_BTNFACE+1,NULL,MainWindowClass 

align 1 
  MainWindowTitle db 'plot', 0 
  MainWindowClass db 'plot32', 0 
  _error TCHAR 'Startup failed.',0 

align 8 
  a dq 2.0 

align 4 
  hMainWindow     dd ? 
  hStatusWindow   dd ? 
  hMainMenu       dd ? 
  hInstance       dd ? 
  ppvBits         dd ? 
  hMemDC          dd ? 
  hdc             dd ? 
  hBitmap         dd ? 
  Bitmap.x        dd ? 
  Bitmap.y        dd ? 
  TickCount       dd ? 
  bmiheader     BITMAPINFOHEADER 

  ps            PAINTSTRUCT 
  rc RECT 
  rect          RECT 
  msg           MSG 

align 1 
  PaintTimer db ?     
Post 10 Nov 2013, 21:32
View user's profile Send private message Reply with quote
schlukhash



Joined: 07 Nov 2013
Posts: 9
schlukhash
An intermediate thanks for this! Have to find the time to pick it apart for a bit. So ill get back at it as soon as possible.
Post 13 Nov 2013, 13:14
View user's profile Send private message Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
Oops, apparently I wrote that example before I learned about the red zone.
Code:
                        mov  dword[esp-4],eax  
                       fild  dword[esp-4]  
                        mov  dword[esp-4],500  
                       fild  dword[esp-4]     

Maybe this is not safe. Hopefully someone who knows better can comment...
Code:
                       push  eax
                       fild  dword[esp]
                       push  500    
                       fild  dword[esp] 
                        pop  eax    
                        pop  eax      
Post 13 Nov 2013, 15:46
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
tthsqe wrote:
Maybe this is not safe.
Well, in generic environment (we're in Main section) it's probably not. Talking of Windows, it's pretty safe to assume that user-space memory (and stack thereof) is almost completely under your control (interrupts will cause stack switch as a part of their handling; exceptions may occur too, but you perhaps are preventing them from doing so).
Code:
        push    eax
        fild    dword[esp]
        mov     dword[esp], 500
        fild    dword[esp]
        pop     eax    
One byte longer, but it can exempt OS from committing another stack page. Wink

As a rule of thumb, better be safe than sorry.

P.S. Your (new) indenting style is quite interesting (not tab-friendly though), it worth some research.
Post 14 Nov 2013, 15:26
View user's profile Send private message Reply with quote
Alessio



Joined: 26 Sep 2003
Posts: 35
Location: Viterbo, Italy
Alessio
Hi tthsqe,

I've some questions rearding you code.

Why you add 16 on bmiheader.biSizeImage ? it is related to lea opcode ?
Why you use hMainWindow in wmpaint ? You can use hwnd instead.
Can this code be easily adapted to plot curves ? Any hint ?

Thank you.
Post 15 Nov 2013, 08:35
View user's profile Send private message MSN Messenger Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
Quote:
Why you add 16 on bmiheader.biSizeImage ? it is related to lea opcode ?

While only 4*[Bitmap.x]*[Bitmap.y] bytes are taken up by the pixels, I leave an extra 16 bytes because I might be using simd instructions to fill in the array and it might go over a little bit as a matter of convenience. You never know what could happen if you write past your allotted space.

Quote:
Why you use hMainWindow in wmpaint ? You can use hwnd instead.

I guess so - I could try to correct this issue.

Quote:
Can this code be easily adapted to plot curves ? Any hint ?

yup, you need to change SquaredDistanceEstimate. x is in xmm0, and y is in xmm1 (scalar doubles). The squared distance estimate should be returned in xmm0 (scalar single). if you are plotting f(x,y) = 0 then
Code:
SquaredDistanceEstimate = f(x,y)^2 / (fx(x,y)^2+fy(x,y)^2)    

where fx and fy are the partial derivatives. This formula will not work well for curves with singularities, so maybe an epsilon should be added in to the denominator.
Post 15 Nov 2013, 08:55
View user's profile Send private message Reply with quote
schlukhash



Joined: 07 Nov 2013
Posts: 9
schlukhash
Ok.. Back at it, As you see my asm budget is very limited Smile
Got a bit confused about the double logarithm on what seems to be an empty register? But im not at all familiar with the ret command. Is this the same as a loop written like this?

Code:
                
movaps  xmm1,train
                
addps xmm1,F1
movaps  xmm2,xmm1
cmpps  xmm1,MEM,5
andps  xmm1,train
subps  xmm2,xmm1
                
movaps train,xmm2
    


Edit: Not that I am confused about "what I presume to be processing the circle" well a little Smile But more the moving nothing to multiply with nothing. Which should be something multpilied by [A].
Post 25 Nov 2013, 15:30
View user's profile Send private message Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
I don't know what you are talking about with the double log, though, I do have some comments on the code you posted:
Code:
if !(a + b < c)
 a = b
else
 a = a + b
end if

; the subraction introduces an unnecessary loss of precision
movaps   xmm1,a
addps    xmm1,b
movaps   xmm2,xmm1
cmpnltps xmm1,c
andps    xmm1,a
subps    xmm2,xmm1
movaps   a,xmm2

; better solution
movaps   xmm0,a
addps    xmm0,b
cmpltps  xmm0,c
andps    xmm0,a
addps    xmm0,b
movaps   a,xmm0    
Post 26 Nov 2013, 11:45
View user's profile Send private message Reply with quote
schlukhash



Joined: 07 Nov 2013
Posts: 9
schlukhash
In your code you say.
Code:
mulsd  xmm0,xmm0 //but this register seems to be empty
mulsd  xmm1,xmm1 // as well as this one
mulsd  xmm1,[a]  // Atleast i dont find where these numbers come from
movsd xmm2,xmm1 
mulsd  xmm2,[a] 
    


But.. Noted, would be cmpleps then.

A=(A+1)-(A>=B)&A
Post 26 Nov 2013, 15:13
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
schlukhash wrote:
Hello all!
I can further my hobby as a vst programmer. And be platform independent at the same time.
All the best.


You will not be platform independant if you code in assembler.

i am interrested in vst also, can you post an example of vst synth to see how it looks like?

All the best to you too Smile
Post 26 Nov 2013, 18:31
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.