1

I have looked all over but can not find a comprehensive guide to the most simple input/output technique in x64 Assembly, Intel syntax using MASM in Visual Studio. I am simply trying to receive two 32-bit integers as input, store them, perform a couple of operations with them, and output my answer to the console. Could someone provide an explanation of the easiest why to do this with a code example? Thanks.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Look [here](http://masm32.com/board/index.php?topic=6486.0) for an introduction to x64 assembly. – zx485 Apr 26 '18 at 22:11
  • Do you want to write code to convert strings to integers yourself, or do you want to call a C library function like `scanf`? Assembly language doesn't have string->int built in, and neither does the system-call API of any normal OS. (MIPS simulators like MARS/SPIM do have read-integer system calls exactly because they're for beginners who don't want to process strings as the first thing they have to learn). Anyway, what OS/library API do you want to use for input/output? – Peter Cordes Apr 27 '18 at 00:29
  • https://software.intel.com/en-us/articles/introduction-to-x64-assembly is a pretty good guide for x86-64 on Windows in general, but I forget if they talk about console I/O functions. See also https://stackoverflow.com/tags/x86/info. – Peter Cordes Apr 27 '18 at 00:31
  • For integer->string, see [How do I print an integer in Assembly Level Programming without printf from the c library?](//stackoverflow.com/a/46301894). Replace the Linux system call to write the string to stdout with your choice of whatever. – Peter Cordes Apr 27 '18 at 00:32
  • I haven't finished my beginners guide for 64-bit Windows assembly, but in the meantime, [I have a couple of small snippets you can use to get you started](https://github.com/simon-whitehead/assembly-fun/tree/master/windows-x64). The more relevant ones to you would be [writing to the console/StdOut](https://github.com/simon-whitehead/assembly-fun/blob/master/windows-x64/1.hello-world/1.hello-world.s) and [an example of printing numbers to the console](https://github.com/simon-whitehead/assembly-fun/blob/master/windows-x64/5.itoa/5.itoa.s). These are NASM though.. not MASM. – Simon Whitehead Apr 27 '18 at 03:09
  • As an aside: what you're trying to do can be accomplished in 2-3 lines of any other language (and MASM likely has lots of nice macros to help with this).... not so much with Assembly. Especially 64-bit Windows .. as it requires a little more digging into the ABI before you can even call a simple Windows API without a segfault. – Simon Whitehead Apr 27 '18 at 03:11

1 Answers1

0

Simon Whitehead gave me the idea by reading his code. Here's the solution basically:

extern GetStdHandle: proc
extern WriteFile: proc

.data?
    written dq ?
.data
    string db "Hello World!",0
    len equ $-string
.code
    mov rcx, -11
    call    GetStdHandle    ; get stdout file handle
    mov rcx, rax            ; set as first parameter
    mov rdx, offset string  ; set message address as second argument
    mov r8, len             ; length as third
    mov r9, written         ; variable to write # bytes written
    call    WriteFile       ; prints message to console

This won't work for unicode though, and i'm not sure how that would be done

Lorenzo
  • 591
  • 7
  • 18
  • WriteFile is just outputting raw binary data (including the terminating `0` byte, which is probably a bug: pipe it into a hexdump or something) If you want UTF-16, you'd want to assemble that into your `.rdata` or `.data` section, e.g. `dw 'H', 'e', ...` or `db 'H',0, 'e',0, ...`. Both of those are rather inconvenient; IDK if MASM has a nice way to assemble source-code strings into UTF-16. `dw "Hello"` doesn't work: [When using the MOV mnemonic to load/copy a string to a memory register in MASM, are the characters stored in reverse order?](https://stackoverflow.com/q/57427904)) – Peter Cordes Jul 07 '22 at 23:18
  • Yeah i saw that, I even tried to do it the exact same way you said with the `dw 'h','e',...` but it only worked when i used `WriteConsoleW` but then the output cannot be used in redirection or piping with other commands, i even put a question on it : https://stackoverflow.com/questions/72903403/write-to-console-unicode-text-masm-64-bits – Lorenzo Jul 08 '22 at 00:15
  • 1
    Maybe I guessed wrong about what would work for Windows; I don't use that OS. Yeah, asking a new question is best – Peter Cordes Jul 08 '22 at 02:20