News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Quick Hello World questions

Started by Alek, November 29, 2018, 01:12:57 AM

Previous topic - Next topic

Alek

Currently using MASM32 (hence why Im here) so I need to use the Windows API and not the 21h interrupt. I see code similar to this fairly often, and I believe it originated from some Microsoft source:
https://stackoverflow.com/questions/2718332/how-can-i-do-input-output-on-a-console-with-masm

Question 1: Why is STD_OUTPUT_HANDLE being set to -11? What's the significance of this number?

Question 2: " message db "Hello World",13,10", the 13, 10 I'm assuming is the null terminator, however I've also seen something like "Hello World", '$'. Whats the background/"proper" way for this?

Question 3: (General): Why does the main need to return/exit before the end is "reached"? We have both "main ENDP" and "END main" that must be called.

Question 4: My IDE is VS 2017 (using ASM Dude as well), I haven't tested it out so I should probably have done this first but - Will I need to "import" anything to use the Windows API calls, or will VS/ml.exe assume this for me as well to call WriteConsoleA?

Thanks for your time and help!

felipe

Welcome to the forum.  :icon14:
Quote from: Alek on November 29, 2018, 01:12:57 AM
Question 1: Why is STD_OUTPUT_HANDLE being set to -11? What's the significance of this number?
That's a constant definition from the win api. Look for documentation in a microsoft win api web page.

Quote from: Alek on November 29, 2018, 01:12:57 AM
Question 2: " message db "Hello World",13,10", the 13, 10 I'm assuming is the null terminator, however I've also seen something like "Hello World", '$'. Whats the background/"proper" way for this?
No, those are the CR and LF respectivily. That win api function don't need the null terminator because one of its parameters is the lenght of the string. See that eax is a param of the function and has a value of 13, then count the lenght of the string defined in the .data section. The $ symbol is the termination string mark for 16 bit code (ms-dos code) you can ignore that one.

Quote from: Alek on November 29, 2018, 01:12:57 AM
Question 3: (General): Why does the main need to return/exit before the end is "reached"? We have both "main ENDP" and "END main" that must be called.
You can have a lot of procedures in your code. So  endp will finish a procedure, while end will tell the assembler where all the code finish.

Quote from: Alek on November 29, 2018, 01:12:57 AM
Question 4: My IDE is VS 2017 (using ASM Dude as well), I haven't tested it out so I should probably have done this first but - Will I need to "import" anything to use the Windows API calls, or will VS/ml.exe assume this for me as well to call WriteConsoleA?
Change to masm32sdk. You will have all the includes files you will want  :icon_mrgreen:

Alek

Thanks for the quick follow up, and Im excited to join the community :)

Back to Question 1, I can't find anything from an official source - there are a few links floating around but leads to 404 pages on Microsoft. Thanks for the clarification on all my points though!

felipe

Look here for win api in general:https://docs.microsoft.com/es-es/windows/desktop/apiindex/windows-api-list
and here for the constant definition in question 1:https://docs.microsoft.com/en-us/windows/console/getstdhandle  :icon14:

This community is the best, really  :bgrin:

hutch--

If you can still find a copy, get hold of the ancient "win32.hlp" file as it has all of the old core API functions. If you can't, you will have to work with MSDN.

Alek

Thanks hutch, Ill dig around ;)

Hey off topic question, have you guys considered doing a little upgrade/redesign on your website? When I first came I thought this place wouldn't be active based on the "curb appeal". Glad I actually checked out the forums though. Maybe just rip some bootstrap, or is the site the aesthetic you're going for? :s

hutch--

The forum software is a simple SMF php model and its safer to keep it that way for updates and changes. We try to make it in content, not interface design and we tend to get people who are interested in writing decent code without the script kiddies so there is no great motivation for change.

TimoVJL

May the source be with you

Alek

Hey going back to the copy pasta here: https://stackoverflow.com/questions/2718332/how-can-i-do-input-output-on-a-console-with-masm

Edit 3:
In regards to pushad/popad instructions, why are they there? Both GetStdCall and WriteConsoleA are WINAPI functions, which are _stdcall so the callee cleans the stack. I checked and it seems the stack is in fact cleared after making the calls/invoke


felipe

Quote from: Alek on November 29, 2018, 08:26:58 AM
Unless I'm missing something?

Yes. Actually invoke will push the parameters too. As regard to the edit 1 question (i think you shouldn't delete what you post, specially if you are trying to learn from the forum): those pushad and popad are unnecessary. The thingh you should know well is that the stdcall calling convention (for windows in 32 bit) in general (there are 1 or maybe a few more exceptions) states in one of its rules than the callee would balance the stack. So after calling this win api function, using invoke or the instruction mnemonic call, esp at the return of this function would remain unchanged.  :idea:

felipe

EDIT 3 was before i could post my answer to edit 1...Please don't erase what you post. Just make a new reply  :exclaim:

Alek

Gotcha, thanks Felipe - I'll be sure to keep my edits in the future. Just wanted to make sure that those push/pops were unnecessary because those functions are stdcalls.

felipe

Quote from: Alek on November 29, 2018, 08:51:23 AM
Just wanted to make sure that those push/pops were unnecessary because those functions are stdcalls.
That's correct.  :icon14:

Quote from: Alek on November 29, 2018, 08:51:23 AM
I'll be sure to keep my edits in the future.
This is very important, because can help others to learn, or others to say something else.  :icon14:

You are welcome. I'm glad to help.  :icon14: