assemblyx86-64nasm

In x86_64 Linux NASM, is it possible to have multiple opcodes on one line?


In C, you are able to have multiple things on one line, like this:

int a; int b = 1; a = b;

Could you do this in assembly?

I tried searching for this on many websites and asked for help on some discord servers, but no luck.


Solution

  • Multiple opcodes sure, if you're writing the machine code by hand. db 0xff, 0xc0, 0xff, 0xc9 are the opcode + ModRM bytes that encode inc eax / dec ecx.

    Multiple asm source instructions, no. Unlike the GNU assembler, there's no statement-separator character other than newline.

    In GAS you can write inc eax; dec ecx if you want. The x86 GAS comment character is #, the statement separator is ; - it's a very different assembler from NASM.


    @ecm points out one trick, though: you can define a NASM macro that puts each arg on a separate line, and use it like this:

    foo   inc eax, {mov ebx, eax}, push rax
    

    See the manual for the macro directives for iterating over macro parameters for a macro that takes an arbitrary number of them:

    %macro foo 1-*      ; require 1 or more params
      %rep  %0          ; repeat for number of params
        %1              ; expand the first param on a line by itself
        %rotate 1       ; rotate the params so the second one is now first
      %endrep
    %endmacro