Search code examples
cassemblygccarm64

GCC output Arm64 assembly instructions


I have a c program on a raspberry pi. I am trying to get the assembly of the code I have written. As the Raspberry Pi is an ARM64 processor I was expecting the assembly to be in ARM64.

Command: gcc -S main.c

Code:

#include <stdio.h>

int main() {
    printf("Hello world!\n");
    return 1;
}

Assembly:

    .arch armv8-a
    .file   "main.c"
    .text
    .section    .rodata
    .align  3
.LC0:
    .string "Hello world!"
    .text
    .align  2
    .global main
    .type   main, %function
main:
.LFB0:
    .cfi_startproc
    stp x29, x30, [sp, -16]!
    .cfi_def_cfa_offset 16
    .cfi_offset 29, -16
    .cfi_offset 30, -8
    mov x29, sp
    adrp    x0, .LC0
    add x0, x0, :lo12:.LC0
    bl  puts
    mov w0, 1
    ldp x29, x30, [sp], 16
    .cfi_restore 30
    .cfi_restore 29
    .cfi_def_cfa_offset 0
    ret
    .cfi_endproc
.LFE0:  .arch armv8-a
    .file   "first.c"
    .text
    .section    .rodata
    .align  3
.LC0:
    .string "Hello world!"
    .text
    .align  2
    .global main
    .type   main, %function
main:
.LFB0:
    .cfi_startproc
    stp x29, x30, [sp, -16]!
    .cfi_def_cfa_offset 16
    .cfi_offset 29, -16
    .cfi_offset 30, -8
    mov x29, sp
    adrp    x0, .LC0
    add x0, x0, :lo12:.LC0
    bl  puts
    mov w0, 1
    ldp x29, x30, [sp], 16
    .cfi_restore 30
    .cfi_restore 29
    .cfi_def_cfa_offset 0
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Debian 12.2.0-14) 12.2.0"
    .section    .note.GNU-stack,"",@progbits
    .size   main, .-main
    .ident  "GCC: (Debian 12.2.0-14) 12.2.0"
    .section    .note.GNU-stack,"",@progbits

This is AT&T syntax (I think) and I want to get the ARM64 assembly. I have been trying options like -masm but the compiler throws an error stating that it's an unrecognized option.

How would I get GCC to generate ARM64 assembly?


Solution

  • That output really is ARM64 assembly code. There's nothing wrong with it.

    There are a lot of directives (dot commands) generated by the compiler, but those are not actually assembly instructions.

    The actual assembly code is this filtered portion:

        stp x29, x30, [sp, -16]!
        mov x29, sp
        adrp    x0, .LC0
        add x0, x0, :lo12:.LC0
        bl  puts
        mov w0, 1
        ldp x29, x30, [sp], 16
        ret