The MASM Forum

General => The Campus => Topic started by: Ravi Kiran on October 11, 2020, 09:38:27 PM

Title: Console l/O in masm32
Post by: Ravi Kiran on October 11, 2020, 09:38:27 PM
Anybody Please tell me how to pass strings and register values to
Win32 console just like formatted printf and scanf functions defined in C standard library. All I know about is to print a string located at particular address using



  mystring dB "Hello World",0

  INVOKE stdout addr mystring ;


statement in masm32. Also plz explain me how to accept strings   
and integer values from user input just like scanf function in C.

To be clear are there any functions in masm32 SDK which work exactly like printf and scanf in C. If so how are they defined. It may sound silly to many senior members of this forum but Im interested to know the assembly version of these functions.
Title: Re: Console l/O in masm32
Post by: Mikl__ on October 11, 2020, 09:57:08 PM
Hi, Ravi Kiran!
Will look Examples for Win64 Iczelion tutorial (http://masm32.com/board/index.php?topic=4190.0)
Title: Re: Console l/O in masm32
Post by: Ravi Kiran on October 11, 2020, 10:02:01 PM
Quote from: Mikl__ on October 11, 2020, 09:57:08 PM
Hi, Ravi Kiran!
Will see Examples for Win64 Iczelion tutorial (http://masm32.com/board/index.php?topic=4190.0)
  • Win x64 Tutorial #38: Simple console application
  • Win x64 Tutorial #38a: Input and output of line of symbols in console application
  • Win x64 Tutorial #38b: Installation of color of symbols and background of the text of the  console application
  • Win x64 Tutorial #38c: Processing of mouse messages
  • Win x64 Tutorial #38d: Installation of color of symbols and background of the text of the  console application (yet another variant)
  • Win x64 Tutorial #38e: Simple  console application (yet another variant)
  • etc.
Sir I'm beginning 32-bit MASM Assembly and I'm very far from learning x64 Assembly. Let me learn this x86 assembly first then after
a long time we can think of x64. Please help me with x86 assembly examples
Title: Re: Console l/O in masm32
Post by: Mikl__ on October 11, 2020, 10:06:34 PM
Just look at the program text, it is also suitable for 32-bit programming
Title: Re: Console l/O in masm32
Post by: Vortex on October 11, 2020, 10:14:32 PM
Hi Ravi,

Here is an example :

include     \masm32\include\masm32rt.inc

.data

str1        db 'What is your name?',13,10,0
str2        db '%s',0
str3        db 13,10,'Hello %s , nice to meet you.',0

.data?

buffer      db 64 dup(?)

.code

start:

    invoke  crt_printf,ADDR str1
    invoke  crt_scanf,ADDR str2,ADDR buffer
    invoke  crt_printf,ADDR str3,ADDR buffer
    invoke  ExitProcess,0

END start
Title: Re: Console l/O in masm32
Post by: jj2007 on October 11, 2020, 10:19:05 PM
Vortex already gave you a good example, here is another one:
include \masm32\include\masm32rt.inc

.data
HelloW$ db "Hello World", 0
buffer db 1000 dup(?)
myInteger dd ?
myDouble REAL8 ?
.code
start:
  cls
  print addr HelloW$, 13, 10 ; CrLf
  print "type any text: "
  invoke crt_scanf, chr$("%s"), addr buffer
  print "this is what you typed: "
  print addr buffer, 13, 10
  print "type an integer: "
  invoke crt_scanf, chr$("%d"), addr myInteger
  print str$(myInteger), 9, "is your integer", 13, 10
  print "type a double: "
  invoke crt_scanf, chr$("%lf"), addr myDouble
  print real8$(myDouble), 9, "is your double", 13, 10
  inkey "That was easy, right?"
  exit

end start


\Masm32\help\*.chm is highly recommended, fascinating lecture :cool:
Title: Re: Console l/O in masm32
Post by: Ravi Kiran on October 12, 2020, 12:07:11 AM
Quote from: jj2007 on October 11, 2020, 10:19:05 PM
Vortex already gave you a good example, here is another one:
include \masm32\include\masm32rt.inc

.data
HelloW$ db "Hello World", 0
buffer db 1000 dup(?)
myInteger dd ?
myDouble REAL8 ?
.code
start:
  cls
  print addr HelloW$, 13, 10 ; CrLf
  print "type any text: "
  invoke crt_scanf, chr$("%s"), addr buffer
  print "this is what you typed: "
  print addr buffer, 13, 10
  print "type an integer: "
  invoke crt_scanf, chr$("%d"), addr myInteger
  print str$(myInteger), 9, "is your integer", 13, 10
  print "type a double: "
  invoke crt_scanf, chr$("%lf"), addr myDouble
  print real8$(myDouble), 9, "is your double", 13, 10
  inkey "That was easy, right?"
  exit

end start


\Masm32\help\*.chm is highly recommended, fascinating lecture :cool:

Thank you sir it worked in winasm IDE. But when I type
.model flat, stdcall assembler flags an error saying .model directive
used twice. It assembled w/o errors when I omit the .model directive.

Also why can't we use [stdout] and [ stdin] functions instead of crt_printf and crt_scanf functions you have said. This looks more like
HLL to me.
Title: Re: Console l/O in masm32
Post by: hutch-- on October 12, 2020, 12:28:56 AM
Its obvious that you don't read the help files, StdOut and StdIn are modules in the masm32 library.
Title: Re: Console l/O in masm32
Post by: Vortex on October 14, 2020, 04:07:16 AM
Hi Ravi,

    invoke  [StdOut],ADDR str1

The brackets does not have any effects here.
Title: Re: Console l/O in masm32
Post by: guga on October 14, 2020, 09:02:41 AM
Quote from: Ravi Kiran on October 11, 2020, 10:02:01 PM
Sir I'm beginning 32-bit MASM Assembly and I'm very far from learning x64 Assembly. Let me learn this x86 assembly first then after
a long time we can think of x64. Please help me with x86 assembly examples

Hi Ravi

Here is the Iczelion tutorials for 32 bits (CHM made by  Edgar Hanson - aka: Donkey)

http://www.website.masmforum.com/tutorials/index.htm