News:

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

Main Menu

"Hello masm32", not a BOT, new member

Started by LordAdef, January 22, 2017, 09:42:24 AM

Previous topic - Next topic

jj2007

Quote from: hutch-- on February 17, 2017, 11:52:37 AMI have warned you for a long time that your timing technique is unsound

Thanks for the warning. I am so sorry that you consider my coding garbage, Hutch, but please explain to me how I could improve the loops below to make them "sound real timing"?

From attachment to Reply #87, MovAh_JJ_DualAssembly.asm (plain text, opens even in qEditor.exe):
MainLoop:
  mov ebx, AlgoLoops-1 ; OxPT_Assembler ML ; delete the x to pick another assembler
  push rax ; keep the stack aligned
  push rv(GetTickCount)
  align 8
@@:
mov ah, byte ptr somestring
mov ch, ah
inc ah
movzx eax, ah
dec ebx
jns @B
  jinvoke GetTickCount
  pop rdx
  pop rcx
  sub rax, rdx
  Print Str$("%i ms for AH\n", rax)

  mov ebx, AlgoLoops-1
  push rax ; keep the stack aligned
  push rv(GetTickCount)
  align 8
@@:
mov al, byte ptr somestring
mov cl, al
inc al
movzx eax, al
dec ebx
jns @B
  jinvoke GetTickCount
  pop rdx
  pop rcx
  sub rax, rdx
  Print Str$("%i ms for AL\n\n", rax)
  dec esi
  jns MainLoop

hutch--

There is not enough in your snippet to make sense of it. Missing bits and limited commenting.

hutch--

Here is an even less interpreted benchmark, 2 x 3 instruction loops ran 8 times sequentially and displayed on the screen. I have done everything I could think of to try and stabilise the timings of the high byte to high byte register copies, aligned each label by 16, set a higher priority class, untangled the mixed 32/64 bit instructions but the low byte register copies are clearly faster on this Haswell I am using. Still, I don't understand why you want to use legacy code like this when you have a lot more registers to work with.

984 ms high byte load
672 ms low byte load
1031 ms high byte load
687 ms low byte load
1016 ms high byte load
672 ms low byte load
1016 ms high byte load
657 ms low byte load
1000 ms high byte load
625 ms low byte load
984 ms high byte load
656 ms low byte load
1000 ms high byte load
640 ms low byte load
1000 ms high byte load
672 ms low byte load
Press any key to continue...

This is the test piece.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

entry_point proc

    LOCAL .rsi  :QWORD
    LOCAL .rdi  :QWORD
    LOCAL .rbx  :QWORD
    LOCAL .r15  :QWORD

    mov .r15, r15
    mov .rbx, rbx
    mov .rdi, rdi

    invoke SetPriorityClass,rv(GetCurrentProcess),HIGH_PRIORITY_CLASS

    chnm equ <255>              ; single number to write
    lcnt equ <1024*1024*1024*2> ; loop iteration count
    algn equ <16>               ; alignment
    dlay equ <100>              ; delay for SleepEx

    mov rdi, 8

  startlabel:

; ----------------------------

    invoke SleepEx,dlay,0

    mov rbx, lcnt
    invoke GetTickCount
    mov r15, rax

    xor rax, rax
    xor rdx, rdx

    mov dh, chnm

  align algn
  @@:
    mov ah, dh                  ; load HIGH BYTE to HIGH BYTE
    sub rbx, 1
    jns @B

    invoke GetTickCount
    sub rax, r15
    conout str$(rax)," ms high byte load",lf

; ----------------------------

    invoke SleepEx,dlay,0

    mov rbx, lcnt
    invoke GetTickCount
    mov r15, rax

    xor rax, rax
    xor rdx, rdx

    mov dl, chnm

  align algn
  @@:
    mov al, dl                  ; load LOW BYTE to LOW BYTE
    sub rbx, 1
    jns @B

    invoke GetTickCount
    sub rax, r15
    conout str$(rax)," ms low byte load",lf

; ----------------------------

    sub rdi, 1
    jnz startlabel

    invoke SetPriorityClass,rv(GetCurrentProcess),NORMAL_PRIORITY_CLASS

    waitkey

    mov r15, .r15
    mov rbx, .rbx
    mov rdi, .rdi

    invoke ExitProcess,0

    ret

entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end

LordAdef

Hi there, I´m still doing my things and I believe I´m making some progress....

I´m attaching below 2 little progs for you.

The first one is a new algo I wrote for my scrolling map. It´s smoother, and can be controlled by key for better tests.
The other is an idea I coded for the "splash screen texts" in between game levels. Just an idea for a non expensive solution.

LordAdef

UPDATE 1: New algo for the scrolling map.

I´m still soly tasting the waters. But I´m introducing some structures and so on.

Size is about 4k. Memory steady at 3.616

1. Ok, I tried many things and read a lot from you here in the forum. Hard coding the scroll map seems the best way to go I guess. I tried ScrollWindowsEx but I loose control of things (although it´s a lot smoother).

2. I definitely need a better mouse solution, this one doesn´t do and I know why. But it will do for now.
You can use the arrows:
Left/right: move our guy sideways
UP/Down: accelerates/decelerates the map

3. I got rid of all RGB macros since I don´t need them at runtime. I rather got prefixed hex values in a struct. But I think they would be better as const. (is it possible to make a const. struct.?? It bothers me to have all those variables floating around)

4. I´m slowly implementing a gameState control for the game design.

Any feedback will be more than welcome!
Alex



LordAdef

UPDATE 2: Splash screen prototype

I wrote this as an idea for inter levels splash screens. It emulates someone typewriting, then it fades the text out and turn it into black. I think it´s elegant and minimalistic for such a simple game.

1. Warning: I´m using a 100% free typewriter font. You need to install it to run the prog. I could´ve used AddFontResourceEx/RemoveFontResourceEx but that would temporarily install the font, and I´m not sure if this is consider "ok". So I didn´t, please let me know how to go about this? (font attached in a folder plus copyrights clearance)

2. This is interesting:

qEditor complains about this line:
Quoteinvoke CreateFont, 17,0,0,0,400,0,0,0, ANSI_CHARSET, 0, 0,0,0, ADDR chr$("Sears Tower") ;NOT good in qEditor

RichMasm doesn´t!

However... qEditor is fine with these ones in my other prog:
Quoteinvoke CreateFont,12,0,0,0,400,0,0,0,OEM_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,\
                                           DEFAULT_QUALITY,DEFAULT_PITCH or FF_SCRIPT, chr$("Courier New")

What´s happening with this?



3. Can we play mp3 instead of a .wav file? It´s tremendously big for a simple typewriter sound (37kb), unless I really bring the sample rate down. What´s the suggestion?

Cheers
Alex


hutch--

Alex,

ADDR chr$("Sears Tower")

This will not work in MASM32, try it without the ADDR as this looks like its a pointer to the pointer that "chr$" returns.

Now I tried your last post, left to right works but a bit sluggish, the forward could be slowed up but I could not get it to run in reverse.

LordAdef

Quote from: hutch-- on February 20, 2017, 05:40:14 PM
Alex,

ADDR chr$("Sears Tower")

This will not work in MASM32, try it without the ADDR as this looks like its a pointer to the pointer that "chr$" returns.

Yes! it´s working as you said, cheers.

QuoteNow I tried your last post, left to right works but a bit sluggish,
Possibly 2 things Hutch:
a. I´m incrementing its y coord by 8
b. I´m intentionally not invalidateRect there. I get some memory leak when I do it, I still need to work on this
Could it be this what you meant or something else?
Quote.ELSEIF uMsg == WM_KEYDOWN
               .IF wParam == VK_LEFT
                   add man.x, -8           

              .ELSEIF wParam == VK_RIGHT
                    add man.x, 8
                   
              .ELSEIF  wParam == VK_UP

Quotethe forward could be slowed up but I could not get it to run in reverse.
I´m not allowing it to reverse.  When you reach speed 8 there´s a jump in speed. Same when speed= 2, and 0. It´s me trying to change different variables in there



LordAdef

I will work on the key code Hutch. But there is this problem with WM_KEYDOWN. There is this auto repeat feature I don´t know how to change or deal with. So KEYDOWN is really unusable as of now:

msdn:
QuoteThe repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.

QuoteBecause of the autorepeat feature, more than one WM_KEYDOWN message may be posted before a WM_KEYUP message is posted. The previous key state (bit 30) can be used to determine whether the WM_KEYDOWN message indicates the first down transition or a repeated down transition.

jj2007

Quote from: LordAdef on February 20, 2017, 05:33:11 PM
qEditor complains about this line:
Quoteinvoke CreateFont, 17,0,0,0,400,0,0,0, ANSI_CHARSET, 0, 0,0,0, ADDR chr$("Sears Tower") ;NOT good in qEditor

RichMasm doesn´t!

Alex,
Strictly speaking this is not RichMasm's merit, but rather due to the fact that RM uses HJWasm32 by default. Try to add this under your code:OPT_Assembler ML

and voilà: error A2033:invalid INVOKE argument : 14

One could argue that all assemblers should throw an error here. Of course, your intention is clear: "addr chr$()". But it's incorrect syntax from a MASM point of view. @HJWasm+AsmC teams: what about a little warning?

Re RGB macros, they can be very compact, i.e. they do not add much code. Have a look at this snippet through the eyes of Olly, it uses RGB (original Masm32 macro) and RgbCol():
include \masm32\MasmBasic\MasmBasic.inc
  Init
  int 3
  RGB 12h, 34h, 56h ; Masm32 macro
  print hex$(eax), 13, 10

  PrintLine Hex$(RgbCol(12h, 34h, 56h)) ; MasmBasic equivalent

  mov eax, 12h
  mov ecx, 34h
  mov edx, 56h
  PrintLine Hex$(RgbCol(eax, ecx, edx))

  mov eax, 12h
  mov ecx, 34h
  mov edx, 56h
  if 0 ; not possible:
RGB eax, ecx, edx
  else ; but this works:
mov eax, RgbCol(eax, ecx, edx)
  endif
  print hex$(eax), 13, 10
  inkey "ok?"
EndOfCode


Even this works, btw:

  PrintLine "Gradual colour change:"
  For_ ecx=0 To 255 Step 11h
      PrintLine Hex$(RgbCol(22h, ecx, 44h))
  Next


Output:Gradual colour change:
00440022
00441122
00442222
00443322
00444422
00445522
00446622
00447722
00448822
00449922
0044AA22
0044BB22
0044CC22
0044DD22
0044EE22
0044FF22


Quote from: LordAdef on February 20, 2017, 06:04:34 PMThere is this auto repeat feature I don´t know how to change or deal with. So KEYDOWN is really unusable as of now:

What exactly is the problem there?

LordAdef

Hi JJ,

I was using the RGB macro. The thing is, there´s no need to waste processor time when I know the colours I need. The code and debugger gets cleaner. What I will chance is forget the colour structure and make them const.

Re Keyboard, I get back to you later today. I need either getKeyboardState or ASync.... (I know getKeyboard.. is slower, so I might try the other one). This is how I implemented my game in C. Although I used the SDL library, the principle worked.



But here is a quicky one:

I decided to clean up my asm file. I created an inc called "stuff.inc" with protos, structs. I also put the Windows shite as a macro so I don´t need to stare at it all the time:

Quote


;------------------- PROTOTYPES ---------------------------------------------------------------------------

    WinMain PROTO :DWORD,:DWORD,:DWORD,:DWORD
    pLoad   PROTO :DWORD
    pIni    PROTO
    pIniFonts PROTO :DWORD
    pSetGameState   PROTO   :DWORD

;------------------------- STRUCTS ----------------------------------------------------------------------

RGB macro red,green,blue
            xor eax,eax
            mov ah,blue
            shl eax,8
            mov ah,green
            mov al,red
    endm
        ;---------------------
        ;--- STRUCTURES ------
        ;---------------------
        Game STRUCT
               lives    dd  ?
               level    dd  ?
               state    dd  ?
               speed  dd  ?
               dispRect  RECT <>
        Game ENDS
       
        Map  STRUCT
           scroll      dd   ?
      linevstart   dd   ?
      line      dd   ?
      yspace      dd   ?
        Map ENDS
       
        Player STRUCT
               speed      dd  ?
               x          dd  ?
               y          dd  ?
               angle      dd   ?
               tx      dd   ?
               ty      dd   ?
        Player ENDS
   
        Enemy STRUCT
               speed   dd  ?
        Enemy ENDS
   
        Bullet STRUCT
               speed   dd  ?
        Bullet ENDS
       
        Colours STRUCT
              white   dd   ?
              yellow   dd   ?
                 bk      dd   ?
              display   dd   ?
        Colours   ENDS

;------------------- MACROS -----------------------------------------------------------------------------------


MacroDisplay macro
                    ; ------------------ TEMPORARY HERE FOR TESTS ----------------------
                    ; Debug display:
                    invoke TextOut,hdc, 0, 600, chr$("Speed:"), 6      
         invoke TextOut,hdc, 30, 600, str$(gm.speed), 3

                    invoke TextOut,hdc, 0, 620, chr$("line:"), 6      
         invoke TextOut,hdc, 30, 620, str$(map.line), 4
                     
                   ; --------- print DISPLAY ------------------------------------
                    invoke SelectObject, hdc, Font4               ; display labels
                    invoke SetTextColor,hdc, colours.display

                    invoke SetBkMode, hdc, TRANSPARENT
                    invoke TextOut,hdc,10,60, chr$("Level"), 5 ;ADDR Label1, 5
                    invoke TextOut,hdc,10,300, chr$("Lives"), 5 ;ADDR Label1, 5

                    invoke SelectObject, hdc, Font3
                    invoke SetTextColor,hdc, colours.display

                    invoke SetBkMode, hdc, TRANSPARENT         ; display values
                    invoke TextOut,hdc,20,90, str$(gm.level), 1 ;ADDR Label1, 5   
                    invoke TextOut,hdc,20,330, str$(gm.lives), 1 ;ADDR Label1, 5
                    ; -----------------------------------------------------------
    endm
   
    macroWinMain macro
     
       mov   wc.cbSize,SIZEOF WNDCLASSEX
       mov   wc.style, CS_HREDRAW or CS_VREDRAW
       mov   wc.lpfnWndProc, OFFSET WndProc
       mov   wc.cbClsExtra,NULL
       mov   wc.cbWndExtra,NULL
       push  hInst
       pop   wc.hInstance
       mov   wc.hbrBackground,COLOR_WINDOW+2
       mov   wc.lpszMenuName,NULL
       mov   wc.lpszClassName,OFFSET ClassName
       invoke LoadIcon,NULL,IDI_APPLICATION           
       mov    wc.hIcon,eax
       mov    wc.hIconSm,eax
       invoke LoadCursor,NULL,IDC_ARROW               
       mov    wc.hCursor, eax
       invoke RegisterClassEx, addr wc
       invoke CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,WS_OVERLAPPEDWINDOW,\
                                            500,10, 900,850,NULL,NULL,hInst,NULL 
       mov    hwnd,eax
      invoke ShowWindow, hwnd,SW_SHOWNORMAL
       invoke UpdateWindow, hwnd

    endm


The question is.....
Before this move my prog was running steady at 3.610 kb.
After this move I´m getting a steady 1.496 kb.... Where is the remaing kbs?? I´m sure they are somewhere..everything is working as it should

hutch--

Alex,

I would be inclined to only use the Windows keyboard messages to start and finish the key operations but to get an adjustable repeat rate, I would set up a thread with a timer where you set the repeat rate yourself and trigger the screen movement with your own delay rate.

LordAdef

Quote from: hutch-- on February 21, 2017, 05:48:28 AM
Alex,

I would be inclined to only use the Windows keyboard messages to start and finish the key operations but to get an adjustable repeat rate, I would set up a thread with a timer where you set the repeat rate yourself and trigger the screen movement with your own delay rate.

Well, I know I can set more than one timer, but I´m not sure how I could implement that in my actual state of personal development :)

The main issue is windows have this repeat count for KeyDown and apparently it shows it at bit 30. I have no bloody clue how to change that ::)

Hutch, how about this size change in my device manager after I´ve created the inc file. Is this normal?

LordAdef

Hutch. I BLOODY GOT IT. Keys working like a charm now

QuotepKeyEvents    proc   hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

   invoke GetAsyncKeyState, VK_RIGHT
   AND   ax, 8000h
   cmp ax, 32768
   jz Right
   
   invoke GetAsyncKeyState, VK_LEFT
   AND   ax, 8000h
   cmp ax, 32768
   jz Left
   
   invoke GetAsyncKeyState, VK_UP
   AND   ax, 8000h
   cmp ax, 32768
   jz Up
   
   invoke GetAsyncKeyState, VK_DOWN
   AND   ax, 8000h
   cmp ax, 32768
   jz Down
   jmp @F

   Right:
        add man.x, 2 ;8
        jmp @F
   
   Left:
        sub man.x, 2
        jmp @F   
   Up:
       macroSpeedUP
       jmp @F
   Down:
        macroSpeedDown
   @@:
   ret
pKeyEvents endp

hutch--

 :biggrin:

> Hutch. I BLOODY GOT IT. Keys working like a charm now  :t