x86-64 在线汇编,带有 IDE 例如 https://www.mycompiler.io/new/asm-x86_64

x86-64 assembly online, with an IDE such as https://www.mycompiler.io/new/asm-x86_64

任何人都可以帮助我编写与下面的程序等效的程序,以便它可以在 IDE https://www.mycompiler.io/new/asm-x86_64 上在线运行吗?我是一名教师,我想向我的学生展示如此真实的工作集,让他们不再认为 Little Man Computer 是真实的。我只是现在没有时间自己解决!

section .text
    global main
    extern printf           ; the C function to be called
main:
 
    mov eax, 45
    mov ebx, 55
    add eax,ebx
    push eax
    push message
    call printf
    add esp, 8
    ret
 
message db "Value = %d", 10, 0

https://www.mycompiler.io/new/asm-x86_64 显然没有 link libc for asm,所以你可以 直接使用 Linux 系统调用(通过 syscall), 而不是像 printf 这样的 libc 函数。

https://www.onlinegdb.com/ 有 asm,但只有 GCC (GAS),没有 NASM。不过,您可以使用 .intel_syntax noprefix 来获取 GAS 的类似 MASM 的 Intel 语法。 重要的是,它确实有一个有效的 GDB 设置,可以让您单步执行您的 asm 并查看寄存器值。这对于学习 asm 来说几乎是必不可少的:多种错误导致完全相同的错误(段错误,或无输出),这些是 assembler 不会诊断的错误。在没有调试器的情况下学习 asm 就像试图在蒙住眼睛的情况下构建机器人(或其他丰富多彩的类比)。

确切地了解每条指令的作用是确切地您应该如何思考 asm,尤其是在试图理解为什么您的程序没有按照您希望的方式执行时。


如果你做了想要使用没有libc的特定IDE(mycompiler.io),How do I print an integer in Assembly Level Programming without printf from the c library?有代码可以转换一个无符号整数转换为十进制 ASCII 字符串并将其提供给 x86-64 Linux write 系统调用 rax=1 / syscall.

extern exitcall exit assemble 而不是 link,作为快速测试:undefined reference to 'exit'.

还有 https://tio.run/ which has NASM and FASM, as well as GAS. FASM allows making 32-bit code via having FASM make an executable directly (format ELF executable 3) instead of a .o which TIO.run expects to be able to link into a 64-bit executable. I've used that to test some code-golf answers (like this) 我特别想要 32 位模式,而不是 64 位模式,所以我可以在指针上使用 dec edi 而不需要 REX 前缀。


我注意到您在尝试调用 printf 时使用了 32 位堆栈参数调用约定。这不是 x86-64 System V ABI 的工作方式 (What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64),所以请决定是否要教授 32 位代码(它有一个更简单但更糟糕的调用约定,几乎所有东西都可以是 32-位)或 x86-64,其中普通代码通常利用 32 位操作数大小和隐式零扩展到 64 位。 args 在寄存器中传递给具有与函数调用类似的调用约定的系统调用。

相关:32 位 带有 int 0x80 系统调用和大量解释。

此外,您不想在 64 位代码中使用 int 0x80