This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to compile hello word code with aarch64 gcc

Assembly code:

    .syntax unified

    @ --------------------------------
.global main
main:
    @ Stack the return address (lr) in addition to a dummy register (ip) to
    @ keep the stack 8-byte aligned.
    push    {ip, lr}

    @ Load the argument and perform the call. This is like 'printf("...")' in C.
    ldr     r0, =message
    bl      printf

    @ Exit from 'main'. This is like 'return 0' in C.
    mov     r0, #0    @ Return 0.

    @ Pop the dummy ip to reverse our alignment fix, and pop the original lr
    @ value directly into pc — the Program Counter — to return.
    pop     {ip, pc}

    @ --------------------------------
    @ Data for the printf calls. The GNU assembler's ".asciz" directive
    @ automatically adds a NULL character termination.
message:
    .asciz "Hello, world.\n"

It's successfully when I use armv7-a compiler to compile it.

Currently I have aarch64 compiler. Can I use it to compile the above code?

  • Well those don't look like aarch64 register names for a start.

    Suggestion: Start by writing a small hello.c, compile it then look at the generated code with objdump -d . That's always a good place to start.

    gcc-for-aarch64 hello.c -o hello

    objdump-for-aarch64 -d hello

  • Hi, cdhmanning,

    Thanks for your answer.

    I note that armv8 is back compatible with armv7 instructions. 

    Does it means these codes could run on the armv8 cores, such as A53/57?

    If yes, how could I achieve it?

  • The two different instruction sets can't be mixed within a single process, it isn't like ARM 32 bit code and Thumb. Each process runs either one instruction set or the other. So yes you can run the code provides the operating system supported aarch32 processes. Some like those on the RPi3 only support aarch32, some only aarch64 and some both. The linked executable have some bytes in the header saying what type of architecture and operating system they expect..

  • Hi,

    Thanks for you answer.

    Let me confirm again.

    1. Whether the 32 bit code could execute is determined by OS.

    2. Currently, I can not successfully compile the code.

         According to the above answers,   I think I should I have two cross toolchains while one is for aarch32        code, and the other is for aarch64 code.  For the above code that I submitted, I should use the aarch32      cross compiler.

    3. If the OS supports both aarch32 and aarch64, the OS will switch the CPU execution state between 32bit     state and 64bit state.

    For the above 3 questions, am I right?