且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我在学习上的OS X 10.6 x86汇编,我怎么编?

更新时间:2023-09-30 08:42:52

您code是32位​​汇编​​。雪豹编译是64位的默认,但你可以使用的gcc -m32 编译32位code。当然也有也可以传递到选项 LD ,但我发现,只记得该选项 GCC 已经足够了,因为它是一个前端的所有这些事情。

Your code is 32-bit assembly. Compilation on Snow Leopard is 64-bit by default, but you can use gcc -m32 for compiling 32-bit code. There are of course also options that you can pass to as and ld but I found that remembering only the option for gcc was enough, since it is a front-end to all these things.

使用GCC向您展示装配code,工程的一个例子:在文件test.c的键入一个最小的C函数,并使用的gcc -S test.c以生产装配文件test.s。

Use gcc to show you an example of assembly code that works: type a minimal C function in file test.c and use gcc -S test.c to produce the assembly in file test.s.

例如:

int x;

void f(void)
{
  int i;
  for (i = 0; i < 5; i++)  x = x + 1;
}

编译汇编本豹纹Mac上:

is compiled in assembly on this Leopard Mac:

    .text
.globl _f
_f:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $24, %esp
    call    L6
"L00000000001$pb":
L6:
    popl    %ecx
    movl    $0, -12(%ebp)
    jmp L2
L3:
    leal    L_x$non_lazy_ptr-"L00000000001$pb"(%ecx), %eax
    movl    (%eax), %eax
    movl    (%eax), %eax
    leal    1(%eax), %edx
    leal    L_x$non_lazy_ptr-"L00000000001$pb"(%ecx), %eax
    movl    (%eax), %eax
    movl    %edx, (%eax)
    leal    -12(%ebp), %eax
    incl    (%eax)
L2:
    cmpl    $4, -12(%ebp)
    jle L3
    leave
    ret
.comm _x,4,2
    .section __IMPORT,__pointers,non_lazy_symbol_pointers
L_x$non_lazy_ptr:
    .indirect_symbol _x
    .long   0
    .subsections_via_symbols

您可以把它简单的通过gcc选项-fno-PIC:

You can make it simpler by using gcc option -fno-PIC:

    .text
.globl _f
_f:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $24, %esp
    movl    $0, -12(%ebp)
    jmp L2
L3:
    movl    _x, %eax
    incl    %eax
    movl    %eax, _x
    leal    -12(%ebp), %eax
    incl    (%eax)
L2:
    cmpl    $4, -12(%ebp)
    jle L3
    leave
    ret
.comm _x,4,2
    .subsections_via_symbols

在Snow Leopard中,你会得到64位的组件(AMD64)。你可以通过使用gcc的-m64选项得到豹同样的事情,你可以得到雪豹同样的事情,你会在Leopard与-m32选项得到运行的gcc。

On Snow Leopard you will get 64-bit assembly (amd64). You could get the same thing on Leopard by using the -m64 gcc option, and you can get on Snow Leopard the same thing you'd get running gcc on Leopard with the -m32 option.