Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I write multiple Assembly instructions on the same line?

Tags:

assembly

Can I write multiple Assembly instructions on the same line the same way I can do with most high level languages. Or does each Assembler treats this feature differently?

like image 373
John Avatar asked Dec 29 '14 08:12

John


1 Answers

Each assembler has its own syntax. Even Intel syntax on x86 is actually not the same across different assemblers. Some examples

  • Does FASM uses Intel Syntax?
  • MASM/NASM Differences
  • ASM: MASM, NASM, FASM?
  • Difference between FASM and MASM/TASM?
  • How to know if an assembly code has particular syntax (emu8086, NASM, TASM, ...)?

Most assemblers accept newline as the instruction separator but some assemblers may also define another separation character so that you don't have to write each instruction on its own line. GAS listed @ and $ as the other statement-ending characters but I'm not sure if they allow multiple instructions to be separated by those characters. It also supports separating instructions with ; so you can write those in one line like inc eax; int 3

A statement ends at a newline character ('\n') or an "at" sign ('@'). [...] A statement ends at a newline character ('\n') or an exclamation point ('!'). [...] A statement ends at a newline character ('\n'); or (for the H8/300) a dollar sign ('$'); or (for the Hitachi-SH or the H8/500) a semicolon (';'). [...] A statement ends at a newline character ('\n') or line separator character. (The line separator is usually ';', unless this conflicts with the comment character; see section Machine Dependent Features.)

MASM doesn't support multiple instructions on the same line but you can create a VARARG macro to achieve similar effect

_ macro args:VARARG
   asm_txt TEXTEQU <>
   FORC char,<&args>
      IFDIF <&char>,<!\>
         STR asm_txt,<&char>
      ELSE
         asm_txt
         asm_txt TEXTEQU <>
      ENDIF
   ENDM
   asm_txt
endm

Then use it like this _ mov eax, -1 \ mov ebx, 2. More information here: Multiple instructions on one line?


For inline assembly, particularly in gcc, as newline ends the previous instruction, you can get instructions to work in one single line by emitting '\n' between them

// unsafe: clobbers the red-zone in the x86-64 System V calling convention.
asm("push rax\n\t" "mov rax, 0\n\t" "pop rax");

or you can use multiple asm statements (edit: not recommended as Peter Cordes said in the comment)

// unsafe: compiler can put its own code between these, or maybe even reorder them
// and without operands + clobbers, you can't mess with the compiler's registers.
asm("mov rbx, rax"); asm("xor rdx, rbx"); asm("mov rcx, 5");

However, because gas and as support the use of ; to separate instructions as mentioned above, you can also use it in gcc like __asm__("xor eax, eax; inc eax") because

The compiler copies the assembler instructions in a basic asm verbatim to the assembly language output file...

AssemblerInstructions

This is a literal string that specifies the assembler code. The string can contain any instructions recognized by the assembler, including directives. GCC does not parse the assembler instructions themselves and does not know what they mean or even whether they are valid assembler input.

You may place multiple assembler instructions together in a single asm string, separated by the characters normally used in assembly code for the system. A combination that works in most places is a newline to break the line, plus a tab character (written as \n\t). Some assemblers allow semicolons as a line separator. However, note that some assembler dialects use semicolons to start a comment.

https://gcc.gnu.org/onlinedocs/gcc-5.4.0/gcc/Basic-Asm.html

MSVC also has ; as instruction separator, because its syntax is

__asm assembly-instruction [ ; ]
__asm { assembly-instruction-list } [ ; ]

However, you can even used the keyword __asm itself to separate instructions

Because the __asm keyword is a statement separator, you can also put assembly instructions on the same line:

__asm mov al, 2   __asm mov dx, 0xD007   __asm out dx, al

ARM RealView Assembler also supports ; as an instruction separator like:

When compiling C++, the ARM compiler supports the asm syntax proposed in the ISO C++ Standard. You can specify inline assembler code using the following formats:

  • On a single line, for example:

    asm("instruction[;instruction]"); // Must be a single string asm{instruction[;instruction]}
    ...

  • If you include multiple instructions on the same line, you must separate them with a semicolon (;). If you use double quotes, you must enclose all the instructions within a single set of double quotes (").

like image 156
phuclv Avatar answered Nov 15 '22 11:11

phuclv