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

我是研究僧i 提交于 2019-11-27 07:15:14

问题


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


回答1:


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.




回答2:


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).




回答3:


"The console" can be rather ambiguous in the modern Windows world. If by console program, you really mean DOS program, you can use the DOS INT 21 API, which is much simpler than calling Win32. I don't have MASM but here is a plain-jane example of how to read a character and write a character. See this for more DOS functions.

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



回答4:


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



来源:https://stackoverflow.com/questions/2718332/how-can-i-do-input-output-on-a-console-with-masm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!