Assembly: printf not printing new line

后端 未结 2 1299
长发绾君心
长发绾君心 2020-12-04 04:18

I have the following code that prints the number of parameters passed to ./main. Notice the fmt in the rodata section. I\'ve included

相关标签:
2条回答
  • 2020-12-04 04:30

    The assembler is not C. The C compiler understands \n as an escape code for ASCII 10. The assembler does not and treats it as two characters. Add the 10 as you describe.

    0 讨论(0)
  • 2020-12-04 04:41

    When you use quotes or double quotes around a string in NASM, it doesn't accept C style escape sequences. On Linux you can encode \n as ASCII 10 like this:

    fmt db "Number of parameters: %d", 10, 0 
    

    There is an alternative. NASM supports backquotes (backticks) which will allow NASM to process the characters between them as C style escape sequences. This should work as well:

    fmt db `Number of parameters: %d \n`, 0
    

    Please note: Those are not single quotes, but backticks. This is described in the NASM documentation:

    3.4.2 Character Strings

    A character string consists of up to eight characters enclosed in either single quotes ('...'), double quotes ("...") or backquotes (...). Single or double quotes are equivalent to NASM (except of course that surrounding the constant with single quotes allows double quotes to appear within it and vice versa); the contents of those are represented verbatim. Strings enclosed in backquotes support C-style -escapes for special characters.

    0 讨论(0)
提交回复
热议问题