0

I have the following code that prints the number of parameters passed to ./main. Notice the fmt in the rodata section. I've included the new line \n, just like in C, but instead of printing the new line, it prints:

Number of parameters: 1 \n

My code is:

;main.asm
GLOBAL main
EXTERN printf

section .rodata:
fmt db "Number of parameters: %d \n", 0 

section .text:

main:

    push ebp
    mov ebp, esp    ;stackframe

    push dword[ebp+8]       ;prepara los parametros para printf
    push fmt
    call printf
    add esp, 2*4

    mov eax, 0      ;return value

    leave           ;desarmado del stack frame
    ret

I know that including a 10 before the 0 and after the "Number..." in fmt will print it, but I want printf to do it. I assemble the code with NASM and then link it via GCC to create my executable.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
j1nma
  • 457
  • 3
  • 9
  • 24
  • 1
    You don't "compile" Assembler code like that, but use an _assembler_. And you don't "compile" the resulting object file, but _link_ it. Anyway, this is not related to gcc and definitively not the C language. Don't add unrelated tags. – too honest for this site Apr 19 '16 at 12:16

2 Answers2

5

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.

Community
  • 1
  • 1
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • Note that YASM's version of NASM syntax doesn't support backtick-quoted string literals. I don't think YASM has a way to write string constants with escape sequences. This only matters if you care about yasm as well as official nasm itself. – Peter Cordes Apr 19 '16 at 17:23
  • @PeterCordes : The original question in the last paragraph said _NASM_, my answer is for _NASM_. If I was referring to _YASM_ I would have said such. _YASM_ isn't _NASM_ in the same way _TASM_ isn't _MASM_.. If the question was about GAS, YASM, MASM syntax (and other variants, some of which share some features and not others) I would have provided an answer that would have covered them. If you would like to produce an answer that covers every assembler on the market and variants be my guest. – Michael Petch Apr 19 '16 at 17:32
  • I wasn't claiming your answer was incomplete or that you should have mentioned it. I just thought it was an interesting addition since YASM is almost fully compatible with NASM syntax. – Peter Cordes Apr 19 '16 at 17:39
3

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.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251