1. 今天在看JOS的源码时,看到如下代码段:
  2. // Load GDT and segment descriptors.
    void
    env_init_percpu(void)
    {
    	lgdt(&gdt_pd);
    	// The kernel never uses GS or FS, so we leave those set to
    	// the user data segment.
    	asm volatile("movw %%ax,%%gs" :: "a" (GD_UD|3));
    	asm volatile("movw %%ax,%%fs" :: "a" (GD_UD|3));
    	// The kernel does use ES, DS, and SS.  We'll change between
    	// the kernel and user data segments as needed.
    	asm volatile("movw %%ax,%%es" :: "a" (GD_KD));
    	asm volatile("movw %%ax,%%ds" :: "a" (GD_KD));
    	asm volatile("movw %%ax,%%ss" :: "a" (GD_KD));
    	// Load the kernel text segment into CS.
    	asm volatile("ljmp %0,$1f\n 1:\n" :: "i" (GD_KT));
    	// For good measure, clear the local descriptor table (LDT),
    	// since we don't use it.
    	lldt(0);
    }
  3. 当中有这行:
    asm volatile(“ljmp %0,$1f\n 1:\n” :: “i” (GD_KT));
    没看懂为什么会跳到GD_KT:0x1f
    搜到http://stackoverflow.com/questions/3898435/labels-in-gcc-inline-assembly里面有回答:
  4. A declaration of a local label is indeed a number followed by a colon. But a reference to a local label needs a suffix of f or b, depending on whether you want to look forwards or backwards - i.e. 1f refers to the next 1: label in the forwards direction.
    
    So declaring the label as 1: is correct; but to reference it, you need to say jmp 1f (because you are jumping forwards in this case).
    
  5. 进一步在文档上看到【http://sourceware.org/binutils/docs-2.18/as/Symbol-Names.html】:
  6. Local Labels
    
    Local labels help compilers and programmers use names temporarily. They create symbols which are guaranteed to be unique over the entire scope of the input source code and which can be referred to by a simple notation. To define a local label, write a label of the form `N:' (where N represents any positive integer). To refer to the most recent previous definition of that label write `Nb', using the same number as when you defined the label. To refer to the next definition of a local label, write `Nf'—the `b' stands for “backwards” and the `f' stands for “forwards”.
    
    There is no restriction on how you can use these labels, and you can reuse them too. So that it is possible to repeatedly define the same local label (using the same number `N'), although you can only refer to the most recently defined local label of that number (for a backwards reference) or the next definition of a specific local label for a forward reference. It is also worth noting that the first 10 local labels (`0:'...`9:') are implemented in a slightly more efficient manner than the others.
    
    Here is an example:
    
         1:        branch 1f
         2:        branch 1b
         1:        branch 2f
         2:        branch 1b
    
    Which is the equivalent of:
    
         label_1:  branch label_3
         label_2:  branch label_1
         label_3:  branch label_4
         label_4:  branch label_3
    
    Local label names are only a notational device. They are immediately transformed into more conventional symbol names before the assembler uses them. The symbol names are stored in the symbol table, appear in error messages, and are optionally emitted to the object file. The names are constructed using these parts:
    
    local label prefix
        All local symbols begin with the system-specific local label prefix. Normally both as and ld forget symbols that start with the local label prefix. These labels are used for symbols you are never intended to see. If you use the `-L' option then as retains these symbols in the object file. If you also instruct ld to retain these symbols, you may use them in debugging.
    number
        This is the number that was used in the local label definition. So if the label is written `55:' then the number is `55'.
    C-B
        This unusual character is included so you do not accidentally invent a symbol of the same name. The character has ASCII value of `02' (control-B).
    ordinal number
        This is a serial number to keep the labels distinct. The first definition of `0:' gets the number `1'. The 15th definition of `0:' gets the number `15', and so on. Likewise the first definition of `1:' gets the number `1' and its 15th definition gets `15' as well. 
    
    So for example, the first 1: may be named .L1C-B1, and the 44th 3: may be named .L3C-B44. 
    
Advertisements