flat assembler
Message board for the users of flat assembler.

Index > Windows > newline in printf, why \r\n not func as expect?

Author
Thread Post new topic Reply to topic
sleepsleep



Joined: 05 Oct 2006
Posts: 8904
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
hi there,
when we use the msvcrt.dll printf,
why something line new line doesn't works as expected?

-> using win32ax.inc (btw)

cinvoke printf,'a = %s \r\n b = %s', 'apple', 'boy'

outputs

a = apple \r\n b = boy

why?
Post 11 Aug 2009, 21:48
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
Because fasm is not a C compiler! fasm does not "look into" the strings and convert \r things to 0x13 like C does.
Post 11 Aug 2009, 22:51
View user's profile Send private message Visit poster's website Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
Why not define the string manually? You will have much more control over it that way Smile

e.g:

Code:
string db 'a = %s ',13,10,' b = %s'

cinvoke printf,string, 'apple', 'boy'    
I don't use it myself but it should work.

_________________
Previously known as The_Grey_Beast
Post 12 Aug 2009, 00:30
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Also:
Code:
cinvoke printf, <'a = %s', 13, 10, 'b = %s'>, 'apple', 'boy'    


But note that printf will expand 10 (\n) into CRLF when used on Windows so you don't need to do it explicitly and better you don't do it that way so you can reuse the code in Linux without any trouble.
Post 12 Aug 2009, 01:12
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 8904
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
Quote:

Because fasm is not a C compiler! fasm does not "look into" the strings and convert \r things to 0x13 like C does.

i know fasm is not c compiler.
but isn't it the convert task should be done by that particular "function printf()" instead of compiler?

i mean, it is that function job to detects "\n" in the string pointer and do the CRLF itself.. assembler/compiler just pass the string pointer to this function, so.. i don't quite get it when you said compiler does the conversion.
Post 12 Aug 2009, 10:01
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
A C compiler converts the backslash (\) escape codes at compile time.

printf converts the percent (%) escape codes at runtime.
Post 12 Aug 2009, 10:06
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 8904
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
ok, i think i get it.
but somehow i found it weird, why c compiler doesn't let printf handles backslash (\) at runtime.

is there any specific reason or it just ... tradition?
Post 12 Aug 2009, 16:22
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
is there any specific reason or it just ... tradition?

Why should it be handled at runtime, if it can be handled once during compilation?

You don't handle at runtime some thing whose result is same on every run. That is wasting of time.
Post 12 Aug 2009, 16:59
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.