Is it possible to run x86 assembly on a x64 operating system?

前端 未结 3 1229
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 12:08

Recently I decided that it was worth getting a try on basic x86 assembly so that it would be easier to debug programs, etc, etc. So I started (about a week ago) learning x86 ass

3条回答
  •  隐瞒了意图╮
    2021-02-04 12:36

    Linux explicitly implements 32 bit support if the compilation option:

    CONFIG_IA32_EMULATION=y
    

    is set.

    This is done by most sane distros, including Ubuntu 14.04.

    32-bit emulation is of course only possible because x86-64 processors are designed to be backwards compatible with 32-bit executables via a 32-bit emulation mode which the kernel knows how to use.

    Another thing you have to worry about is the libraries: to compile 32-bit programs, you need 32-bit libraries. On Ubuntu 14.04 AMD64:

    sudo apt-get install gcc-multilib
    

    Then we can easily test it out with a hello world:

    #include 
    #include 
    
    int main() {
        puts("hello world");
        return EXIT_SUCCESS;
    }
    

    and:

    gcc -m32 hello_world.c
    ./a.out
    

    Which prints:

    hello world
    

    And:

    file a.out
    

    confirms that it is 32 bit:

    ELF 32-bit LSB  executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=358f7969deeb2f24a8dd932a0d296887af4eae30, not stripped
    

提交回复
热议问题