How do I compile this assembly file with GCC or others?

0

I copied a sample assembly code from wiki:

.section .data

s0 : .asciz "Processor Brand String: %.48s\n"
err : .asciz "Feature unsupported.\n"

.section .text

.global main
.type main,@function
.align 32
main:
    pushq   %rbp
    movq    %rsp,   %rbp
    subq    $48,    %rsp
    pushq   %rbx

    movl    $0x80000000,    %eax
    cpuid

    cmpl    $0x80000004,    %eax
    jl  error

    movl    $0x80000002,    %esi
    movq    %rsp,   %rdi
   ......
    ......
        jmp end

.align 16
error:
    movq    $err,   %rdi
    xorb    %al,    %al
    call    printf

.align 16
end:
    popq    %rbx
    movq    %rbp,   %rsp
    popq    %rbp
    xorl    %eax,   %eax
    ret

I replaced main with _start and add syscall at the end, then built it successfully with "as" and "ld". But I thought it should be compiled directly with GCC somehow. So what do I need to do?

linux
gcc
assembly
asked on Stack Overflow Dec 21, 2018 by user2158697

1 Answer

2

gcc -no-pie main.s for this source which defines a main, just like a .c that defines a main.

gcc -no-pie -nostartfiles start.s if you define _start (the ELF entry point) yourself. (Since you're on Linux, glibc can initialize itself via dynamic linker hooks, even if your _start doesn't call the glibc init functions.)

To build a static binary without any libraries, gcc -nostdlib -static start.s.

answered on Stack Overflow Dec 21, 2018 by Peter Cordes

User contributions licensed under CC BY-SA 3.0