News:

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

Main Menu

Other simple simple program...but funny =)

Started by felipe, May 11, 2017, 01:32:35 PM

Previous topic - Next topic

felipe

This simple program will ask you if you want to close your actual session. You can say cancel or ok with the corresponding actions after that (not log out, log out). Remember i'm just learning by now. I did a lot of comments and the structure i made it like that for clarity, of course it's not the most optimizing code posible.


                .386
                .model flat,stdcall
                option casemap:none

                include \masm32\include\windows.inc
                include \masm32\include\user32.inc
                include \masm32\include\kernel32.inc

                includelib \masm32\lib\user32.lib
                includelib \masm32\lib\kernel32.lib

                .data
titl            byte    'System message',0                              ; Three message boxes in total (with the same title).
content1        byte    'Log out now?',0
content2        byte    'Windows will close your session now.',0
content3        byte    'Your actual session will continue now.',0

                .data?
indicator       dword   ?                                               ; To indicate if a condition (ok button was pushed) occurs.
value_retorn    dword   ?                                               ; For store the button pushed in the message box (for later compare).

                .code
main            proc
                mov     indicator,0                                     ; Begins as zero (i should done this in the .data).
                call    send_messag1                                    ; Creates the first message box.
                call    compare                                         ; Compares which button was pushed.
                cmp     indicator,0                                     ; Ok button will move 1 to indicator.
                je      not_logoff                                      ; But if is zero user has pushed cancel. So let's jump.
                push    EWX_LOGOFF                                      ; Ok button pushed, so logout now.
                call    ExitWindowsEx

not_logoff:               
                call    ExitProcess                                     ; And we finish here.
main            endp               

send_messag1    proc                                                    ; Makes the first message box.
                push    MB_OKCANCEL
                lea     ebx,titl
                push    ebx
                lea     ebx,content1
                push    ebx
                push    0
                call    MessageBox
                mov     value_retorn,eax                                ; We store here the button pushed.
                ret
send_messag1    endp

compare         proc                                                    ; Compares which button was pushed and sets indicator
                mov     eax,value_retorn                                ;   as 1 if ok button was pushed or continues in zero otherwise.
                cmp     eax,IDOK                                       
                jne     dont_close                                      ; No ok button pushed, so we jump.
                call    final_messag                                    ; Ok button pushed, so we show the according message box.
                mov     indicator,1 ; Here we say ok button was pushed.
                jmp     end_

dont_close:
                call    messag_continu                                  ; Show other message box.

end_:
                ret
compare         endp

final_messag    proc                                                    ; Message box saying goodbye...
                push    MB_OK
                lea     ebx,titl
                push    ebx
                lea     ebx,content2
                push    ebx
                push    0
                call    MessageBox
                ret
final_messag    endp

messag_continu  proc                                                    ; Message box saying go on...
                push    MB_OK
                lea     ebx,titl
                push    ebx
                lea     ebx,content3
                push    ebx
                push    0
                call    MessageBox
                ret
messag_continu  endp

                end     main


hutch--

Works fine here on my Win10 64. Had a look at the code and its clean, tidy and very clear, just the way it should be.

jj2007

> call    ExitProcess

Slightly unorthodox: You might consider using the invoke macro.

hutch--

Probably better with,

push 0               ; should have the extra arg
call ExitProcess

aw27

Quote from: felipe on May 11, 2017, 01:32:35 PM
This simple program will ask you if you want to close your actual session. You can say cancel or ok with
Something I would change is:
replace:
                lea     ebx,titl
                push    ebx
with
       push offset titl

And more 4 or 5 instances of the same or similar.

You also forgot all required parameters a couple of times, namely with ExitWindowsEx and ExitProcess, so is better to use INVOKE which will help you account for that.




felipe

I understand that offset would generate less code (smaller). I like to use call. I will check the requirements for the ExitProcess function because i hasn't done it. But for the ExitWindowsEx i'm sure that's the only parameter to pass.

aw27

Quote from: felipe on May 12, 2017, 10:56:51 AM
But for the ExitWindowsEx i'm sure that's the only parameter to pass.
If you are sure Microsoft will need to review the documentation, my friend.

jj2007

Quote from: aw27 on May 12, 2017, 12:01:42 AMYou also forgot all required parameters a couple of times, namely with ExitWindowsEx and ExitProcess, so is better to use INVOKE

Quote from: hutch-- on July 22, 2016, 12:00:01 AMSuch are the slings and arrows of outrageous fortune when you actually cannot rely on anyone holding you hot little hand and have to write low level assembler.  :P
;)

felipe

Quote from: aw27 on May 12, 2017, 01:34:07 PM
Quote from: felipe on May 12, 2017, 10:56:51 AM
But for the ExitWindowsEx i'm sure that's the only parameter to pass.
If you are sure Microsoft will need to review the documentation, my friend.

I don't get it. This is from the win32.hlp for the api documentation:

"The ExitWindowsEx function either logs off, shuts down, or shuts down and restarts the system.

BOOL ExitWindowsEx(

    UINT uFlags,   // shutdown operation
    DWORD dwReserved    // reserved
   );"

And nothing more my friend... :icon_eek:

Then:

"dwReserved
Reserved; this parameter is ignored. "

Maybe you think it's for shutdown the system. I have used the function just for logoff.

aw27

Quote
"The ExitWindowsEx function either logs off, shuts down, or shuts down and restarts the system.

BOOL ExitWindowsEx(

    UINT uFlags,   // shutdown operation
    DWORD dwReserved    // reserved
   );"

And nothing more my friend... :icon_eek:


My friend, you are using documentation from last century, probably applying in full to Windows 95.
Also.. reserved never meant optional you must indeed put something there. And strangely enough, optional does not mean not required in ASM, you must also put something there. Confused?

jj2007

include \Masm32\MasmBasic\Res\JBasic.inc ; ## console demo, builds in 32- or 64-bit mode with ML, AsmC, JWasm, HJWasm ##
Init                                          ; OPT_64 1 ; put 0 for 32 bit, 1 for 64 bit assembly
  PrintLine Chr$("This code was assembled with ", @AsmUsed$(1), " in ", jbit$, "-bit format")
  MsgBox 0, "Wow, it works!!!!", "Hi", MB_OK or MB_SETFOREGROUND
  jinvoke ExitProcess, 0 ; we exit here, no need to shut down
  jinvoke ExitWindowsEx, EWX_POWEROFF, SHTDN_REASON_MAJOR_APPLICATION
EndOfCode


It's a 64-bit source, but it requires only MasmBasic and nothing else. To build the project, open it in RichMasm and hit F6.

Then try to remove or comment out the SHTDN_ parameter, and hit F6 again to build it. Or add one more parameter, whatever you like. Example:
  jinvoke ExitWindowsEx, EWX_POWEROFF ; , SHTDN_REASON_MAJOR_APPLICATION

Result:
\Masm32\MasmBasic\Res\JBasic.inc(6) : Error A2114: forced error: ## not enough arguments for ExitWindowsEx ##

There is a REASON why Microsoft, 20 years ago, invented the invoke macro. The reason is that even the best programmers are really bad at counting their own fingers. CreateWindowEx, for example, needs eleven (!) arguments, and if you are bad at counting, it might even work but you introduce a bug that will be exceptionally difficult to chase. Most probably it will pop up on your client's site, five years later, and while you are still proud that you are able to pass all these arguments with push eax like a real assembly programmer, you have absolutely no clue why your beautiful application crashes in Windows 15. And your client is really furious :icon_mrgreen:

aw27

Quote from: jj2007 on May 12, 2017, 06:27:01 PM
  jinvoke ExitProcess, 0   ; we exit here, no need to shut down
  jinvoke ExitWindowsEx, EWX_POWEROFF, SHTDN_REASON_MAJOR_APPLICATION

A bit scaring but jinvoke works like a charm  :bgrin:

felipe

so this: "dwReserved
Reserved; this parameter is ignored. "

It's just for high level languages?

And, well i think one should keep looking for the O.S documentation, specially if there are errors. So, no, i'm not confused at all, my friend...

aw27