gcc can produce assembly but how do I compile pure assembly with gcc or other compiler? I know x86 assembly is difficult and another instruction set than the MIPS and Nios I was looking at but now I want to try to compile direct x86 asm. There are instruction for how to do it but there's a C file included and I don't need a C file for my first most basic compile.

gcc -o test_asm asm_functions.S test_asm.c 

There's the step creating .o files

gcc -c asm_functions.S gcc -c test_asm.c gcc -o test_asm asm_functions.o test_asm.o 

But I don't see the step where I can directly compile x86 asm with gcc. There's another program named GNU as (GNU Assembler), can it be used to translate x86 assembly to machine code?

Test

Code (32.s)

.globl _start .text _start: movl $len, %edx movl $msg, %ecx movl $1, %ebx movl $4, %eax int $0x80 movl $0, %ebx movl $1, %eax int $0x80 .data msg: .ascii "Hello, world!\n" len = . - msg 

Steps

$ gcc -c 32.s $ ls 32* 32.o 32.s $ gcc -o 32 32.o 32.o: In function `_start': (.text+0x0): multiple definition of `_start' /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' 

So it seems it may have mixed 32 and 64 bits, must I tell the complier whether the assembly is 32 or 64 bit instructions?

Update

This test worked with gcc.

$ cat hello.s .data .globl hello hello: .string "Hi World\n" .text .global main main: pushq %rbp movq %rsp, %rbp movq $hello, %rdi call puts movq $0, %rax leave ret $ gcc hello.s -o hello $ ./hello Hi World 
2

2 Answers

You're already doing it.

gcc -c asm_functions.S 

That step produces an object file, asm_functions.o. The object file is "linkable" (as opposed to "loadable") file that contains the machine code, with some extra instructions for how the linker should modify the code when linking. The gcc program itself is just a driver, it runs as behind the scenes for you to produce asm_functions.o. So you do have the option of running as directly, but often it's easier to run the gcc frontend.

3

Although the update works, the original code could have been compiled by simply using gcc -nostdlib. For example,

gcc -nostdlib 32.s -o 32 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy