assemblyraspberry-piarmgnu-assembler

What is difference between .fun: and fun: in assembly language?


.section .data
msg1:
    .asciz "Hello1: %d\r\n"
msg2:
    .asciz "Hello2: %d\r\n"

.section .text
.global main

main:
    mov r4, #0         
.loop:
    cmp r4, #5          
    bge .endloop         
    ldr r0, =msg1       
    mov r1, r4
    bl printf          
    add r4, r4, #1      
    b .loop            
.endloop:
    mov r7, #1          
    mov r0, #0         
    swi 0              

My assembly code runs on Raspberry Pi 4B, why is it that when I replace .loop: with loop: and .endloop: with endloop:, it still executes normally? What are the differences between .loop: and loop:? Thank you.


Solution

  • No difference, they're just label names. . is a valid character in asm symbols, including as the first character.

    But by convention, . labels are "local" to the function. In NASM syntax (an x86 assembler), they are actually scoped that way, so you can have .loop inside multiple different functions.

    In GNU Assembler (GAS) like here, .loop isn't special at all; it would be an error (symbol redefinition) to have foo: ; .loop: earlier or later in the file.

    .Lxyz labels that start with a dot and capital L are special (not appearing in the symbol table of the .o at all even for debugging), but still file-scoped. https://sourceware.org/binutils/docs/as/Symbol-Names.html This is why GCC uses label names like .L0 for branch targets within functions (if/else/loops/etc) and names like .LC0 for constants in .rodata, and even stuff like .Lanchor0 for locations it wants to mark to reference other stuff relative to.

    For actual local labels in GAS that you can define more than once, use bare numbers like 1: and refer to it with 1b for backwards to the nearest 1 in that direction, 1f for the nearest 1: in the forward direction. Like 1: bhi 1b is an infinite loop or not depending on the carry and zero flags.