gpt4 book ai didi

assembly - 我可以在同一行上写多个汇编指令吗?

转载 作者:行者123 更新时间:2023-12-04 06:47:52 29 4
gpt4 key购买 nike

我可以像使用大多数高级语言一样在同一行上编写多个汇编指令吗?还是每个汇编程序都不同地对待此功能?

最佳答案

每个汇编器都有自己的语法 .甚至 x86 上的 Intel 语法实际上在不同的汇编程序中也不相同。一些例子

  • 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, ...)?

  • 大多数组装商接受 换行作为指令分隔符,但一些汇编程序也可能定义另一个分隔符,这样您就不必在自己的行上编写每条指令。 GAS挂牌 @$作为其他语句结束字符,但我不确定它们是否允许由这些字符分隔多个指令。它还支持使用 ; 分隔指令所以你可以把它们写在一行中,比如 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 不支持同一行上的多条指令,但您可以创建一个 VARARG 宏来实现类似的效果
    _ 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

    然后像这样使用它 _ mov eax, -1 \ mov ebx, 2 .更多信息在这里: Multiple instructions on one line?

    对于 内联汇编 ,特别是在 gcc 中,当换行结束前一条指令时,您可以通过发出 '\n' 来获得在一行中工作的指令。它们之间

    // 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");

    或者您可以使用多个 asm 语句(编辑: 不推荐 正如 Peter Cordes 在评论中所说的那样)

    // 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");

    然而,因为 gasas支持使用 ;将上述说明分开, you can also use it in gcc like __asm__("xor eax, eax; inc eax") 因为

    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 还有 ;作为指令分隔符,因为它的语法是
    __asm assembly-instruction [ ; ]
    __asm { assembly-instruction-list } [ ; ]

    然而, you can even used the keyword __asm itself分开指令

    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也支持 ;作为指令分隔符,如:

    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 (").

    关于assembly - 我可以在同一行上写多个汇编指令吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27685548/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com