0

(Console API I'm referring to is the one here: https://learn.microsoft.com/en-us/windows/console/console-functions)

I am currently trying to write a Hello World program in MASM, and I need to use a function from that API (Specifically, the "WriteFunction" function). I looked around to find how I can call one of these functions from MASM, but I can't find an answer that works in Visual Studio 2019.

The solution I found that doesn't work uses include statements, but when I try to use them, the program says it can't open either of the files. The solution is from here: Outputting Hello World in MASM using WIN32 Functions

.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 

When I try this solution, it says "cannot open file "\masm32\include\windows.inc" I am working from Microsoft Visual Studio 2019, with MASM included as a build dependency.

EDIT: the two .inc files do not successfully import, but the .lib file works just fine.

EDIT2: The other error I'm receiving is that WriteConsole (a method from the API) is an undefined symbol

Dory L.
  • 133
  • 8
  • 1
    Well, do you have that file there? If not, search for it. If you don't have it, search the internet for it. If all else fails, heck, you don't really need it. The only thing it has is the `STD_OUTPUT_HANDLE` being `-11` I think. – Jester Feb 17 '20 at 00:30
  • so it seems that the file "windows.inc" does not seem to exist on my computer, so I just inputted -11 in the argument. the other 2 files are included with no problem, just had to remove the path before. however, the WriteConsole is still not working – Dory L. Feb 17 '20 at 00:52
  • How is it not working? Did you manage to create the program? – Jester Feb 17 '20 at 00:53
  • it says "Undefined Symbol: WriteConsole" – Dory L. Feb 17 '20 at 00:54
  • 2
    That means you did not link with `kernel32` or you might need to use `WriteConsoleA` explicitly. PS: you should not pass `ebx` uninitialized. Just pass a 0 (`NULL`). – Jester Feb 17 '20 at 00:56
  • I've checked the linker's depedancies, `kernel32.lib` is included in there. I've also tried using WriteConsoleA, that one also is an undefined symbol. – Dory L. Feb 17 '20 at 01:09
  • I've provided at the end of https://stackoverflow.com/a/52803496/3512216 an example for Win32. If you have understood it you can change `WriteFile` to `WriteConsole`. – rkhb Feb 17 '20 at 06:30
  • @rkhb I understand the 32-bit example, but the 64-bit one doesn't make sense for me. Where did you find the calling conventions for each of the functions (i.e which register to put the arguments into)? – Dory L. Feb 17 '20 at 16:52
  • @DoryL.: Win64: https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention. In a nutshell: https://en.wikipedia.org/wiki/X86_calling_conventions#Microsoft_x64_calling_convention. `WriteFile` is a good example to show the use of the registers and the stack. – rkhb Feb 17 '20 at 17:16
  • _"the two .inc files do not successfully import, but the .lib file works just fine"_ If assembling failed due to missing include-files then you don't know whether the library-files work because you're not reaching the linking phase. In any case, the code in your question is for MASM32 specifically, which is a separate software package. – Michael Feb 18 '20 at 11:13

0 Answers0