Hello World output in MASM using WIN32 functions

Content

  • Introduction
  • Code
  • Assembly and launch
  • Miscellaneous
  • Question

1. Introduction

This is not a question in itself (although there is one below), but the HelloWorld application for people on whom StackOverflow is experimenting.

When I first tried to program in MASM, I tried to find a working HelloWorld application that used WIN32 API calls (so without contacting the C libraries), but could not find it (in the MASM syntax). So, now that I have some experience, I wrote one for others who want to learn the assembly in order to play with it.

2. Code

.386 ; 386 Processor Instruction Set .model flat,stdcall ; Flat memory model and stdcall method option casemap:none ; Case Sensitive ;Libaries and Include files used in this project ; Windows.inc defines alias (such as NULL and STD_OUTPUT_HANDLE in this code include \masm32\include\windows.inc ; Functions that we use (GetStdHandle, WriteConsole, and ExitProcess) ; Listing of all available functions in kernel32.lib include \masm32\include\kernel32.inc ; Actuall byte code available of the functions includelib \masm32\lib\kernel32.lib .data ; Labels that with the allocated data (in this case Hello World!...) that are aliases to memory. output db "Hello World!", 0ah, 0h; This String Hello World! and then a the newline character \n (0ah) and then the null character 0h .code start: ; -------------------------------------------------------------------------------------------------------------------------------------- ; Retrieves that handle to the output console ; ; ====Arguments=== ; ; STD_OUTPUT_HANDLE - alias for -11 and indicates that we want the handle to ; write to console output ; invoke GetStdHandle, STD_OUTPUT_HANDLE ; -------------------------------------------------------------------------------------------------------------------------------------- ; -------------------------------------------------------------------------------------------------------------------------------------- ; Writes the text in output (.data section) to the console ; ; ====Arguments=== ; ; eax - the handle to the console buffer ; ; addr output - pass by reference the text of output (Hello World!) ; ; sizeof output - the size of the string so that the WriteConsole knows when to ; stop (doesn't support NULL terminated strings I guess); ; ; ebx - secondary "return" value that contains the number of bytes written (eax ; is used for an error code) ; ; NULL - this is reserved and MSDN says just to pass NULL ; ; MSDN Link: http://msdn.microsoft.com/en-us/library/ms687401(v=VS.85).aspx ; invoke WriteConsole, eax, addr output, sizeof output, ebx, NULL ; -------------------------------------------------------------------------------------------------------------------------------------- ; -------------------------------------------------------------------------------------------------------------------------------------- ; Exits the program with return code 0 (default one that usually is used to ; indicate that the program did not error ; ; ====Arguments=== ; ; 0 - the exit code ; ; MSDN Link: http://msdn.microsoft.com/en-us/library/ms682658(VS.85).aspx ; invoke ExitProcess, 0 ; -------------------------------------------------------------------------------------------------------------------------------------- end start 

3. Assembly and launch

I assume that you have MASM32 installed in the C: \ MASM32 directory.

  • If you do not have MASM installed, go to http://masm32.com/install.htm and follow the instructions.

  • If MASM32 is installed in a different directory, please change accordingly.

    • Open the MASM32 editor (QEditor) by clicking on the desktop shortcut or if there is no shortcut, go to C: \ MASM32 \ and double-click qeditor.exe

    • Copy the code in the code section (only text with a gray background) and paste it into the MASM32 editor (QEditor) and save it.

    • After saving the code, click the Project menu and select “Assembly and Connection Console” ( NOT “Assemble and Link” (see the “Miscellaneous” section))

    • Scroll to START and click Run, then type cmd and press Enter. A black box appears with gray text.

    • Go, using Explorer, to the place where you saved the code in step 3. Now there should be a file with the same name as the source file (step 3), but be exe. Drag the exe file from the Explorer window into the cmd field (step 4 - black box)

    • Select the black box and press ENTER, the text "Hello World!". should appear.

4. Miscellaneous

Why do I need to click the Build and Run console and not the Build and Run console in the Project menu?

The reason you should click Build and Run Console is because there are two types of applications: graphical interfaces, and then there are text-based applications (DOS). The Hello application will be text-based, and therefore the console-based applications should be configured during the build, not the graphical interface.

For a more detailed explanation, see the third paragraph in the Notes section of this link .

5. Question

Ok, now the question is, does anyone here see any problems, errors or general problems with this code or any suggestions

+4
source share
3 answers

The program is in order. This is really a version of "Hello World" Win32. However, remember its console program. In Win32, you will mainly deal with Windows, dialog boxes, and even more so with the console (Incase, you want to deal specifically with the console, that's another story).

If you want to rely on the Win32 build, I highly recommend that you read the Iczelion tutorials.

Here is "Hello World" to start with his tutorials:

http://win32assembly.online.fr/tut2.html

+2
source

This sample code is simpler and easier to understand.

 .386 .model flat, stdcall option casemap: none include windows.inc include user32.inc include kernel32.inc includelib user32.lib includelib kernel32.lib .data szCaption db 'Hello', 0 szText db 'Hello, World!', 0 .code start: invoke MessageBox, NULL, offset szText, offset szCaption, MB_OK invoke ExitProcess, NULL end start 
+1
source

StdOut is a console function

You can use the MessageBox function ...

 .model small,pascal,nearstack .386 ?WINPROLOGUE=1 include win.inc includelib libw.lib extern __astart:proc .data text sbyte "Hello f*** World!",0 title sbyte "Win",0 .code WinMain PROC, hInstance:HANDLE, hPrevInstance:HANDLE, lpszCmdLine:LPSTR, nCmdShow,WORD LOCAL msg:MSG invoke MessageBox, NULL, addr text, addr title, 0 invoke PostQuitMessage,0 .while TRUE invoke GetMessage,addr msg,NULL,0,0 .break .if (ax == 0) invoke TranslateMessage,addr msg invoke DispatchMessage,addr msg .endw WinMain ENDP END __astart 
0
source

Source: https://habr.com/ru/post/1333872/


All Articles