Search code examples
cassemblyx86-64

Can I directly run compiled code in windows?


In this site, wrote a simple C code and got its assembly code: (x86-64 gcc 10.2)

#include<stdio.h>
// Type your code here, or load an example.
int main() {
    int a=5, b=5;
    int c = a+b;
    return 0;
}
main:
        push    rbp
        mov     rbp, rsp
        mov     DWORD PTR [rbp-4], 5
        mov     DWORD PTR [rbp-8], 5
        mov     edx, DWORD PTR [rbp-4]
        mov     eax, DWORD PTR [rbp-8]
        add     eax, edx
        mov     DWORD PTR [rbp-12], eax
        mov     eax, 0
        pop     rbp
        ret

If I copy the above assembly code and paste it in notepad, can I somehow run it in windows to get the same expected output as if I ran the above C code? If I can, how should I do it.


Solution

  • You can't use a text editor like notepad to write a binary file, since text editors normally strip out bytes that do not represent printable characters. You would have to use a hex editor, or some other editor that is intended for binary files.

    Secondly, unless you're writing a very basic executable file format (like the .COM file format for DOS), you would have to do more than just write the machine code for the intended entry point of your program. EXE and ELF executables consist of a header, relocations, text, data, resources, etc. not just machine code.