Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do Input/Output on a console with MASM? [closed]

Tags:

assembly

masm

I've googled and googled, and I've not found anything useful. How can I send output to the console, and accept user input from the console with assembly?

I'm using MASM32

like image 993
Malfist Avatar asked Dec 03 '22 12:12

Malfist


2 Answers

As filofel says, use the Win32 API. Here's a small hello world 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 assemble:

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

Now to capture input, you'd proceed similarly, using API functions such as ReadConsoleInput. I leave that as an exercise to you.

like image 51
PhiS Avatar answered Jan 07 '23 21:01

PhiS


Just by using the Win32 API: By writing to STD_OUTPUT_HANDLE (and reading from STD_INPUT_HANDLE). See GetStdHandle() in MSDN as a starting point... Use the MASM HLL constructs to help you (INVOKE is your friend for calling Win32 functions and passing parms).

like image 42
filofel Avatar answered Jan 07 '23 20:01

filofel