Assembling 32-bit binaries on a 64-bit system (GNU toolchain)

后端 未结 2 760
后悔当初
后悔当初 2020-11-21 11:45

I wrote assembly code that successfully compiles:

as power.s -o power.o

However, it fails when I try to link the object file:



        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 12:15

    I'm learning x86 assembly (on 64-bit Ubuntu 18.04) and had a similar problem with the exact same example (it's from Programming From the Ground Up, in chapter 4 [http://savannah.nongnu.org/projects/pgubook/ ]).

    After poking around I found the following two lines assembled and linked this:

    as power.s -o power.o --32  
    ld power.o -o power -m elf_i386
    

    These tell the computer that you're only working in 32-bit (despite the 64-bit architecture).

    If you want to use gdb debugging, then use the assembler line:

    as --gstabs power.s -o power.o --32.
    

    The .code32 seems to be unnecessary.

    I haven't tried it your way, but the gnu assembler (gas) also seems okay with:
    .globl start
    # (that is, no 'a' in global).

    Moreover, I'd suggest you probably want to keep the comments from the original code as it seems to be recommended to comment profusely in assembly. (Even if you are the only one to look at the code, it'll make it easier to figure out what you were doing if you look at it months or years later.)

    It would be nice to know how to alter this to use the 64-bit R*X and RBP, RSP registers though.

提交回复
热议问题