0
bits 64
default rel

segment .data
    grs db "name: "
    inpt db "%s"

segment .text
    global main


extern printf
extern scanf
extern _CRT_INIT
extern ExitProcess

global main
    
exit:
    xor rax, rax
    call ExitProcess

main:
    push rbp
    mov rbp,rsp
    sub rsp, 32

    lea rcx, [grs]
    mov rax, rcx
    call printf ;printing the grs variable

    lea rcx, [inpt];the problem starts here

    
    jmp exit

i am very new to assembly, I push %s into scanf so I type something on the console (like hello world) and press enter. Logically, the text I wrote needs to be saved somewhere, but I couldn't find how to do it.

I use these two commands to build

  1. nasm -f win64 -o "main.obj" "main.asm"

  2. link "C:\Users\xx\Desktop\assembly_x64\get_input\main.obj" /subsystem:console /entry:main /defaultlib:ucrt.lib /defaultlib:msvcrt.lib /defaultlib:legacy_stdio_definitions.lib /defaultlib:Kernel32.lib /nologo /incremental:no /opt:ref /out:"main.exe"

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
DertliSelo
  • 17
  • 3
  • Just like in C, you have to reserve space somewhere (e.g. on the stack or BSS) and pass a pointer to that space as the second arg for scanf. Your code isn't even calling scanf, so that's the problem. Also, you forgot to terminate the format string, like `db "%s", 0`. [How do I printf and scanf in x86\_64 assembly?](https://stackoverflow.com/q/64627005) shows an example, but it's a bit over-complicated, not just scanning one string, and it's doing it in a function that takes args. – Peter Cordes Sep 14 '22 at 20:15
  • Note that `scanf` tokenizes on whitespace, so one `%s` conversion will only get the `hello` part of `hello world`. If that's not what you want, use a simpler input function like `fgets`, not scanf formatted input. [How do you allow spaces to be entered using scanf?](https://stackoverflow.com/q/1247989) shows a C example - you can look at compiler-generated asm for it on https://godbolt.org/. It uses `malloc` instead of just a local array, though. – Peter Cordes Sep 14 '22 at 20:19
  • [NASM ReadConsoleA or WriteConsoleA Buffer Debugging Issue](https://stackoverflow.com/q/29081694) shows how to use lower-level WinAPI functions (essentially system calls), instead of C stdio, in case you want to do it that way. – Peter Cordes Sep 14 '22 at 20:24

0 Answers0