I am learning x86 build on OS X 10.6, how do I compile?

I am going to learn the x86 assembly language on Mac OS X. I use as an instruction to compile the assembly file in the commend window. But I have a few mistakes, and I donโ€™t know how I can get through.

Here are the errors and my build code, which is pretty simple.

**ung-mi-lims-macbook-pro:pa2 ungmi$ as swap.s swap.s:16:Unknown pseudo-op: .type swap.s:16:Rest of line ignored. 1st junk character valued 115 (s). swap.s:19:suffix or operands invalid for `push' swap.s:46:suffix or operands invalid for `pop' ung-mi-lims-macbook-pro:pa2 ungmi$** 

and source

 .text .align 4 .globl swap .type swap,@function swap: pushl %ebp movl %esp, %ebp movl %ebp, %esp popl %ebp ret 

and I was looking for some solution that I need to put -arch i386 than

 **ung-mi-lims-macbook-pro:pa2 ungmi$ as -arch i386 swap.s swap.s:16:Unknown pseudo-op: .type swap.s:16:Rest of line ignored. 1st junk character valued 115 (s). ung-mi-lims-macbook-pro:pa2 ungmi$** 

could you help me? Just let me know what I need to compile the build file. I already have Xcode, and I would prefer to do this with the commend and vi editor.

+4
source share
4 answers

Your code is a 32-bit build. Compilation on Snow Leopard is 64-bit by default, but you can use gcc -m32 to compile 32-bit code. Of course, there are options that you can pass to as and ld , but I found that remembering that only the option for gcc was sufficient, as it is an interface to all of these things.

Use gcc to show you an example of assembly code that works: enter the minimum C function in test.c and use gcc -S test.c to create the assembly in test.s.

Example:

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

compiled in the 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 

You can make this easier with the gcc -fno-pic option:

  .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 

In Snow Leopard, you get a 64-bit build (amd64). You can get the same on Leopard using the -m64 gcc option, and you can get the same on Snow Leopard that you get gcc on Leopard with the -m32 option.

+6
source

Use GCC , it will compile and link you: cc swap.s

+2
source

Apple as forked from binutils 1.38, and the current version is 2.20, so there may be many new directives that are not available. I can not find the manual for 1.38, but the oldest (2.10) I could find that .type is only associated with the COFF output format. The object format on Mac is Mach-O, so the .type directive does not make sense. Just delete it.

+2
source

I recently ran into this problem while trying to compile Intel x86 on Mac OS X:

For nasm:

 -o hello.tmp - outfile -f macho - specify format Linux - elf or elf64 Mac OSX - macho 

For ld:

 -arch i386 - specify architecture (32 bit assembly) -macosx_version_min 10.6 (Mac OSX - complains about default specification) -no_pie (Mac OSX - removes ld warning) -e main - specify main symbol name (Mac OSX - default is start) -o hello.o - outfile 

For shell:

 ./hello.o - execution 

Single line:

 nasm -o hello.tmp -f macho hello.s && ld -arch i386 -macosx_version_min 10.6 -no_pie -e _main -o hello.o hello.tmp && ./hello.o 

Let me know if this helps!

I wrote how to do this on my blog:

http://blog.burrowsapps.com/2013/07/how-to-compile-helloworld-in-intel-x86.html

For a more detailed explanation, I explained my Github here:

https://github.com/jaredsburrows/Assembly

0
source

Source: https://habr.com/ru/post/1308578/


All Articles