News:

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

Main Menu

Weird behaviour of console print functions

Started by jj2007, March 22, 2016, 06:29:33 AM

Previous topic - Next topic

jj2007

Quote from: nidud on March 22, 2016, 05:59:08 AM
QuoteBtw your printf "\n" puts out a LF only.

The printf function is written by Vortex so I guess he skipped that bit. The data storage of "\n" is always 10 (LF) and it is the _write() function that normally adds a "\r" (CR) in front if needed before calling WriteFile().


printf proc c USES esi edi format:dword, arglist:VARARG
LOCAL buf1[512]:BYTE
LOCAL buf2[256]:BYTE
LOCAL hHandle:DWORD
LOCAL BytesWritten:DWORD

invoke  GetStdHandle,STD_OUTPUT_HANDLE
mov hHandle,eax
invoke wvsprintfA,addr buf1,format,addr arglist
lea esi,buf1
lea edi,buf2
mov edx,edi
.if byte ptr [esi] == 10
mov word ptr [edi],0A0Dh
add edi,2
add esi,1
.endif
lodsb
.while al
.if al == 10 && byte ptr [esi-1] != 13
mov byte ptr [edi],0Dh
add edi,1
.endif
stosb
lodsb
.endw
mov esi,edi
sub esi,edx
lea ecx,BytesWritten
invoke WriteFile,hHandle,edx,esi,ecx,0
mov eax,esi
ret
printf  endp

Using a simple...
start:
  xor ebx, ebx
  .Repeat
print "M32 "
print str$(ebx), 13, 10
inc ebx
  .Until ebx>=3

  xor ebx, ebx
  .Repeat
printf("Crt %d\n", ebx)
inc ebx
  .Until ebx>=3

... you get:
M32 0
M32 1
M32 2
Crt 0
Crt 1
Crt 2


So far, so good. It gets weird when (under Win7-64) you redirect output to a file (see attachment):
pf.exe >pf.txt

M32 0
M32 1
M32 2
Crt 0 Crt 1 Crt 2


In a hex editor, you'll discover 0D0D0A sequences for the Crt prints.

It gets much weirder with this code:
start:
  xor ebx, ebx
  .Repeat
print "M32 "
print str$(ebx), 13, 10
inc ebx
  .Until ebx>=3

  xor ebx, ebx
  .Repeat
printf("Crt %d\n", ebx)
inc ebx
  .Until ebx>=3

  xor ebx, ebx
  .Repeat
print "M32 "
print str$(ebx), 13, 10
inc ebx
  .Until ebx>=3


Console output:
M32 0
M32 1
M32 2
Crt 0
Crt 1
Crt 2
M32 0
M32 1
M32 2


No surprise. But with redirection to a text file, the three Crt prints appear at the end of the text file ::)

nidud

#1
deleted

jj2007

Even with invoke crt_printf, formatstring, number you get the same effect: the crt part appears at the end of the file:
M32 0
M32 1
M32 2
<<< ---- should be here
M32 0
M32 1
M32 2
MB 0
MB 1
MB 2
Crt 0 Crt 1 Crt 2 <<< ---- but gets moved here, with 0d 0d 0a sequences

nidud

#3
deleted

jj2007


nidud

#5
deleted

nidud

#6
deleted

RuiLoureiro


jj2007

Hi Rui,
Happy Easter :icon14:
Where have you been all the time??

ragdog

Hello Jochen

Nice found printf replacement
http://masm32.com/board/index.php?topic=5250.msg56234#msg56234

But it support not Floating point wsprintf does not supporting.

I think is better tu use sprintf
Quotea format string that follows the same specifications as format in printf

Greets,

jj2007

Quote from: ragdog on March 29, 2016, 03:43:18 PM
Hello Jochen

Nice found printf replacement
http://masm32.com/board/index.php?topic=5250.msg56234#msg56234

But it support not Floating point wsprintf does not supporting.

I think is better tu use sprintf
Quotea format string that follows the same specifications as format in printf

Great :t
Let me know when sprintf starts supporting simple constructs like e.g.

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  Let edi="\Masm32\include\Windows.inc"
  Let esi=FileRead$(edi)
  Inkey "The file ", edi, Str$(" has %i lines", Count(esi, CrLf$)), Str$(" and %4f kBytes", LastFileSize/1024)
EndOfCode


Output:
The file \Masm32\include\Windows.inc has 26902 lines and 954.5 kBytes

ragdog



QuoteLet me know when sprintf starts supporting simple constructs like e.g.

Why not?


.data
szStr db "The file \Masm32\include\Windows.inc has",0
kBytes real8 954.5

.code

mov ebx,26902
invoke crt_sprintf,addr plainbuf ,chr$ ("%s %i lines and %.2f kBytes",CR,LF),addr szStr,ebx,kBytes
invoke crt_puts,addr plainbuf


Output


The file \Masm32\include\Windows.inc has 26902 lines and 954.50 kBytes

Waiting for keypress to exit....


MfG

jj2007


RuiLoureiro

Quote from: jj2007 on March 29, 2016, 08:07:19 AM
Hi Rui,
Happy Easter :icon14:
Where have you been all the time??
Hi Jochen, my life changed a lot, home, wife,city,etc and i never did anything about the calculator since the last time. My computer is not well too.
Nice to see you again. :t :icon14:
Where is Dave ? He is here, do you know ?

ragdog

Quote
LastFileSize and Count() are not global variables, my friend :P

Ok i understand not MasmBasic  :biggrin: