0

How do I get an input and output I Don't understand at all and haven't found a single simple example anywhere the only thing i have is:

I am using masm in visual studio

; get a write handle
MOV rcx, STD_OUTPUT_HANDLE
CALL    GetStdHandle
MOV whandle, eax            ; write handle

; get a read handle
MOV rcx, STD_INPUT_HANDLE
CALL    GetStdHandle
MOV rhandle, eax            ; read handle`
Ryan
  • 433
  • 3
  • 8
  • 14

1 Answers1

0

This is highly operating system-specific. You need to call the API functions provided by the OS in order to do this. Clearly, from the code in the question, as well as from your comment, you are targeting Windows, so you would need to look in Microsoft's documentation for the Windows API functions that allow you to read from and/or write to the console. You already found the GetStdHandle function; the other required functions would be documented in the same place.

Specifically, you are probably looking for ReadConsole and WriteConsole, although there are plenty more specialized functions for console I/O. You can find a brief tutorial on how to call these functions from C on this website.

You should be able to translate that C code into assembly language. Any Windows API function would be called from assembly language the same way as you do GetStdHandle, following the standard Windows 64-bit calling convention. If you get stuck in the translation of C to assembly, you can always write the code in C, run it through the compiler, and look at a disassembly listing to see how it should be done.

Here is a simple example. First, it writes some instruction text to the console using WriteConsole, with an output handle obtained by calling GetStdHandle. Then, it reads some text into a buffer using ReadConsole with an input handle obtained by calling GetStdHandle. Finally, it repeats that text in the console by calling WriteConsole.

; L"Type some text:\n"
szInstruction DB 'T', 00H, 'y', 00H, 'p', 00H, 'e', 00H, ' ', 00H,
              DB 's', 00H, 'o', 00H, 'm', 00H, 'e', 00H, ' ', 00H,
              DB 't', 00H, 'e', 00H, 'x', 00H, 't', 00H, ':', 00H,
              DB 0AH, 00H, 00H, 00H


MyFunction PROC
    ; Allocate space on the stack
    sub  rsp, 264

    ; WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
    ;               L"Type some text:\n",
    ;               16,
    ;               address of DWORD-sized variable on stack,
    ;               NULL);
    mov  ecx, -11                     ; STD_OUTPUT_HANDLE
    call GetStdHandle

    mov  rcx, rax                     ; handle was returned in RAX
    lea  rdx, OFFSET szInstruction
    mov  r8d, 16
    lea  r9, QWORD PTR [rsp+272]
    mov  QWORD PTR [rsp+32], 0
    call WriteConsoleW

    ; ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
    ;              address of string buffer on stack,
    ;              100,
    ;              address of DWORD-sized variable on stack,
    ;              NULL);
    mov  ecx, -10                     ; STD_INPUT_HANDLE
    call GetStdHandle

    mov  rcx, rax                     ; handle was returned in RAX
    lea  rdx, QWORD PTR [rsp+48]
    mov  r8d, 100
    lea  r9, QWORD PTR [rsp+272]
    mov  QWORD PTR [rsp+32], 0
    call ReadConsoleW

    ; WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
    ;               address of string buffer on stack,
    ;               number of chars read (from DWORD-sized variable written into by ReadConsoleW),
    ;               address of DWORD-sized variable on stack,
    ;               NULL);
    mov  ecx, -11                     ; STD_OUTPUT_HANDLE
    call GetStdHandle

    mov  rcx, rax                     ; handle was returned in RAX
    lea  rdx, QWORD PTR [rsp+48]
    mov  r8d, DWORD PTR [rsp+272]
    lea  r9, QWORD PTR [rsp+272]
    mov  QWORD PTR [rsp+32], 0
    call WriteConsoleW

    ; Clean up space allocated on stack
    add  rsp, 264

    ret
MyFunction ENDP

Which is equivalent to the following C code:

void MyFunction()
{
   TCHAR szBuffer[100];
   DWORD dwCount;

   WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
                 L"Type some text:\n",
                 16,
                 &dwCount,
                 NULL);

   ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
                szBuffer,
                100,
                &dwCount,
                NULL);

   WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
                 szBuffer,
                 dwCount,
                 &dwCount,
                 NULL);
}

Note: error-checking has been omitted for clarity! This is not a good way to actually write code!


Alternatively, you could avoid all the complicated, OS-specific stuff by leveraging the power of the C standard library, which encapsulates all of this for you already. You would just link to the appropriate object file, and then call a function like printf or scanf. See the answers to this question for help on getting started.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574