News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How to use cout and cin in iostream library

Started by qq1qq1, November 23, 2012, 01:17:50 PM

Previous topic - Next topic

qq1qq1

Thought I know how to print strings in console window, I want to rewrite this c++ program with masm

#include <iostream>
int main()
{
   int i;
   std::cin >> i;
   std::cout << i << " times, Hello, world!" << std::endl
   return 0;
}


The problem is I can't find any iostream.lib or something like that in the directory of masm.
I don't know how to include it and to make use of it.
Must use cout, cin, operator << and >>. Anyone know how to do? Thanks

hutch--

Hi,

Welcome on board.

> Must use cout, cin, operator << and >>.

The "must"says you write it in C++ as they are specific to C++. If you can work out what you are trying to do without the assumptions of C++ notation, it probably can be done using the Windows API functions.

Ther are pure API versions of STDIN STDERR STDOUT etc ....

dedndave

we don't use c++ standard libraries much in assembler
i guess it's a cumbersome interface
i suppose there is a way to do this using COM or something   :P

to interface with the console, we generally use the file i/o functions

what you might try is to build it with a C++ compiler and generate an assembler listing

jj2007

Quote from: qq1qq1 on November 23, 2012, 01:17:50 PM
Thought I know how to print strings in console window, I want to rewrite this c++ program with masm

Take it easy...

include \masm32\MasmBasic\MasmBasic.inc   ; download
   Init
   For_ ebx=1 To Val(Input$("How many times? ", "3"))
      Print "Hello C coder", CrLf$
   Next
   Inkey
   Exit
end start

MichaelW

Quote from: dedndave on November 23, 2012, 01:58:34 PM
what you might try is to build it with a C++ compiler and generate an assembler listing

Unfortunately, the relevant listing (39 of 11685 lines) is not much help.

_TEXT SEGMENT
_i$ = -4 ; size = 4
_main PROC NEAR ; COMDAT
; File c:\program files\microsoft visual c++ toolkit 2003\my\cpp\cinout\test.cpp
; Line 3
push ebp
mov ebp, esp
push ecx
; Line 5
lea eax, DWORD PTR _i$[ebp]
push eax
mov ecx, OFFSET FLAT:?cin@std@@3V?$basic_istream@DU?$char_traits@D@std@@@1@A
call ??5?$basic_istream@DU?$char_traits@D@std@@@std@@QAEAAV01@AAH@Z ; std::basic_istream<char,std::char_traits<char> >::operator>>
; Line 6
push OFFSET FLAT:??_C@_0BG@CIJOCIOC@?5times?0?5Hello?0?5world?$CB?$AA@
push DWORD PTR _i$[ebp]
mov ecx, OFFSET FLAT:?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A
call ??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@H@Z ; std::basic_ostream<char,std::char_traits<char> >::operator<<
push eax
call ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<<std::char_traits<char> >
push eax
call ?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z ; std::endl
add esp, 12 ; 0000000cH
; Line 7
xor eax, eax
; Line 8
leave
ret 0
_main ENDP
_TEXT ENDS


But the 98304 byte EXE does just what it's supposed to do  :biggrin:
Well Microsoft, here's another nice mess you've gotten us into.

dedndave

my only experience with i/o streams in asm was when using OLE
it involved using OLE to access resource data - not console i/o
i must admit - i didn't understand everything i read on the subject   :lol:

CommonTater

Quote from: dedndave on November 23, 2012, 03:17:34 PM
my only experience with i/o streams in asm was when using OLE
it involved using OLE to access resource data - not console i/o
i must admit - i didn't understand everything i read on the subject   :lol:

fwiw ...  the cout << my name is fred << endl  us roughly equivalent to fprintf(stdout, "My name is fred\n");

I don't know how easily you can invoke a call from the C Run Time (crt) library, but that should work...



TouEnMasm

Quote
I don't know how easily you can invoke a call from the C Run Time (crt) library, but that should work...

easy:
The include files for the crt are in the windows.inc subforum

.NOLIST
.686P
.model flat,stdcall
option casemap:none   
include translate.inc
include windows.sdk
include \vc\stdio.sdk
include \vc\stdlib.sdk
.const
includelib  msvcrt.lib ;libcmt.lib ;
;extern c _FPinit:dword ; to load floating point library with
invalid_parameter PROTO C :DWORD,  :DWORD,  :DWORD,  :DWORD,  :DWORD
;avoid the R6002 floating point not loaded error
.data
invalid db "invalid parameter",0 
.code
;################################################################
invalid_parameter PROC C expression:DWORD,function,file,line,pReserved
Local  retour:DWORD
mov retour,1
;all parameters expression:DWORD,function,file,line,pReserved are NULL
invoke MessageBox,NULL,ADDR invalid,NULL,MB_OK
Findeinvalid_parameter:
mov eax,retour
ret
invalid_parameter endp

;wmain PROC  C  uses esi edi ebx argc:DWORD,argv:DWORD ;dos program
WinMain proc hInst:DWORD,hPrevInst:DWORD,lpCmdLine:DWORD,nShowCmd:DWORD
;for secured functions sprintf_s ......
invoke _set_invalid_parameter_handler,invalid_parameter
;no exitprocess
mov eax,0
ret
WinMain endp
;wmain endp ;dos programme

end  ;not end WinMain,start point is in the msvcrt.lib
Fa is a musical note to play with CL

jj2007

Quote from: CommonTater on November 23, 2012, 03:56:49 PM
fwiw ...  the cout << my name is fred << endl  us roughly equivalent to fprintf(stdout, "My name is fred\n");

I don't know how easily you can invoke a call from the C Run Time (crt) library, but that should work...

   xchg ebx, rv(GetStdHandle, STD_OUTPUT_HANDLE)
   fprintc ebx, "My name is fred\n"

It could be a one-liner:
   fprintc rv(GetStdHandle, STD_OUTPUT_HANDLE), "My name is fred\n"
... but the Masm32 fprintc macro (\Masm32\macros\macros.asm) destroys the handle:
      invoke WriteFile,hFile,pst,len(pst),ADDR var,NULL

Same in MasmBasic, in three variants:
   Print cStyle$("My name is fred\n")
   Print "My name is fred", CrLf$
   PrintLine "My name is fred"

MichaelW

The attachment contains a CPP source that encapsulates the code in a function in a DLL, the DLL and import library, the batch file I used to compile the source, a MASM source that calls the function, and the EXE.

We need a TaDa! smiley.
Well Microsoft, here's another nice mess you've gotten us into.

dedndave



http://messenger.yahoo.com/features/emoticons/
http://messenger.yahoo.com/features/hiddenemoticons/

the closest one to "tada" is probably "party" or "dancing"

a couple of my favorites...


and one they made just for us Arizonans...

howdy, ma'am 

Vortex

.386
.model flat, stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\msvcrt.inc
includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\msvcrt.lib

include     Streams.inc

.data

msg     db 'stdout = %X',0

.data?

stdout  dd ?
stdin   dd ?
stderr  dd ?

.code

start:

    call    crt___p__iob
    mov     stdin,eax           ; #define stdin  (&__iob_func()[0])
    add     eax,SIZEOF(FILE)
    mov     stdout,eax          ; #define stdout (&__iob_func()[1])
    add     eax,SIZEOF(FILE)
    mov     stderr,eax          ; #define stderr (&__iob_func()[2])
    invoke  crt_fprintf,stdout,ADDR msg,stdout       
    invoke  ExitProcess,0

END start