How can I do I / O on the console using MASM?

I googled and googled and I did not find anything useful. How to send output to the console and accept user input from the console using assembly?

I am using MASM32

+3
source share
4 answers

As filofel reports, use the Win32 API. Here is a small greeting example:

.386
.MODEL flat, stdcall
 STD_OUTPUT_HANDLE EQU -11 
 GetStdHandle PROTO, nStdHandle: DWORD 
 WriteConsoleA PROTO, handle: DWORD, lpBuffer:PTR BYTE, nNumberOfBytesToWrite:DWORD, lpNumberOfBytesWritten:PTR DWORD, lpReserved:DWORD
 ExitProcess PROTO, dwExitCode: DWORD 

 .data
 consoleOutHandle dd ? 
 bytesWritten dd ? 
 message db "Hello World",13,10
 lmessage dd 13

 .code
 main PROC
  INVOKE GetStdHandle, STD_OUTPUT_HANDLE
  mov consoleOutHandle, eax 
  mov edx,offset message 
  pushad    
  mov eax, lmessage
  INVOKE WriteConsoleA, consoleOutHandle, edx, eax, offset bytesWritten, 0
  popad
  INVOKE ExitProcess,0 
 main ENDP
END main

To collect:

ml.exe helloworld.asm /link /subsystem:console /defaultlib:kernel32.lib /entry:main

Now, in order to capture the input, you will act in a similar manner using API functions such as ReadConsoleInput. I leave this as an exercise for you.

+7
source

API Win32: STD_OUTPUT_HANDLE ( STD_INPUT_HANDLE). . GetStdHandle() MSDN ... MASM HLL, (INVOKE - Win32 ).

+2

"" Windows. DOS, API DOS INT 21, , Win32. MASM, , . . this DOS.

MOV AH,1      ; code for "read a character"
INT 21H        ; character gets put in AL

MOV AH,2       ; code for "write a character"
MOV DL,'A'     ; ascii code goes in DL
INT 21H
+2

Download and connect to the Irvine32 libraries , they will provide you with input and output functions that are very user-friendly.

0
source

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


All Articles