Author Topic: Other simple simple program...but funny =)  (Read 531 times)

felipe

  • Member
  • ***
  • Posts: 280
  • I love assembly language programming.
Other simple simple program...but funny =)
« 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 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.

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

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4807
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Other simple simple program...but funny =)
« Reply #1 on: May 11, 2017, 03:43:52 PM »
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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

jj2007

  • Member
  • *****
  • Posts: 7542
  • Assembler is fun ;-)
    • MasmBasic
Re: Other simple simple program...but funny =)
« Reply #2 on: May 11, 2017, 08:37:56 PM »
> call    ExitProcess

Slightly unorthodox: You might consider using the invoke macro.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4807
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Other simple simple program...but funny =)
« Reply #3 on: May 11, 2017, 08:52:48 PM »
Probably better with,

push 0               ; should have the extra arg
call ExitProcess
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

aw27

  • Member
  • ****
  • Posts: 697
Re: Other simple simple program...but funny =)
« Reply #4 on: May 12, 2017, 12:01:42 AM »
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

  • Member
  • ***
  • Posts: 280
  • I love assembly language programming.
Re: Other simple simple program...but funny =)
« Reply #5 on: May 12, 2017, 10:56:51 AM »
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.
Felipe.

aw27

  • Member
  • ****
  • Posts: 697
Re: Other simple simple program...but funny =)
« Reply #6 on: May 12, 2017, 01:34:07 PM »
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

  • Member
  • *****
  • Posts: 7542
  • Assembler is fun ;-)
    • MasmBasic
Re: Other simple simple program...but funny =)
« Reply #7 on: May 12, 2017, 02:01:32 PM »
You also forgot all required parameters a couple of times, namely with ExitWindowsEx and ExitProcess, so is better to use INVOKE

Such 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

  • Member
  • ***
  • Posts: 280
  • I love assembly language programming.
Re: Other simple simple program...but funny =)
« Reply #8 on: May 12, 2017, 02:25:47 PM »
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.
Felipe.

aw27

  • Member
  • ****
  • Posts: 697
Re: Other simple simple program...but funny =)
« Reply #9 on: May 12, 2017, 02:48:11 PM »
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

  • Member
  • *****
  • Posts: 7542
  • Assembler is fun ;-)
    • MasmBasic
Re: Other simple simple program...but funny =)
« Reply #10 on: May 12, 2017, 06:27:01 PM »
Code: [Select]
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:
Code: [Select]
  jinvoke ExitWindowsEx, EWX_POWEROFF ; , SHTDN_REASON_MAJOR_APPLICATION
Result:
Code: [Select]
\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

  • Member
  • ****
  • Posts: 697
Re: Other simple simple program...but funny =)
« Reply #11 on: May 12, 2017, 06:43:37 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

  • Member
  • ***
  • Posts: 280
  • I love assembly language programming.
Re: Other simple simple program...but funny =)
« Reply #12 on: May 12, 2017, 11:16:50 PM »
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...
Felipe.

aw27

  • Member
  • ****
  • Posts: 697
Re: Other simple simple program...but funny =)
« Reply #13 on: May 12, 2017, 11:33:35 PM »
i'm not confused at all, my friend...

 :icon_confused:

felipe

  • Member
  • ***
  • Posts: 280
  • I love assembly language programming.
Re: Other simple simple program...but funny =)
« Reply #14 on: May 12, 2017, 11:35:55 PM »
Felipe.