Search code examples
assemblynasmgnu-assembler

How to translate GAS 1: in NASM assembly?


I want to write a small OS to grow my programming skills, which worked to a certain point. Now i try to understand linux 0.01 source code to learn more about it. To compile it i need to translate a certain file (head.s) into nasm syntax because my toolchain dislikes the gas file. That wasn't a big deal (i thought) until i realized that i forgot something.

Piece of code:

_pg_dir:
startup_32:
    movl $0x10,%eax
    mov %ax,%ds
    mov %ax,%es
    mov %ax,%fs
    mov %ax,%gs
    lss _stack_start,%esp
    call setup_idt
    call setup_gdt
    movl $0x10,%eax     # reload all the segment registers
    mov %ax,%ds     # after changing gdt. CS was already
    mov %ax,%es     # reloaded in 'setup_gdt'
    mov %ax,%fs
    mov %ax,%gs
    lss _stack_start,%esp
    xorl %eax,%eax
1:  incl %eax       # check that A20 really IS enabled  ~~~
    movl %eax,0x000000
    cmpl %eax,0x100000
    je 1b                                             ~~~
    movl %cr0,%eax      # check math chip
    andl $0x80000011,%eax   # Save PG,ET,PE
    testl $0x10,%eax
    jne 1f          # ET is set - 387 is present      ~~~
    orl $4,%eax     # else set emulate bit
1:  movl %eax,%cr0                                    ~~~
jmp after_page_tables

"~~~" marks the lines i don't fully understand. The 1b and 1f aren't labels. At least not ones like those i know from intel syntax. How do i have to translate the labels and conditional jumps?


Solution

  • These are local labels, a gas-specific feature.

    Here 1f refers to the next instance of the 1 label (forward) and 1b refers to the previous instance (backward).

    So code like

    1:  blah blah
        jmp 1b
        blah blah
        jmp 1f
        blah blah
    1:  blah blah
    

    can be rewritten as

    xx: blah blah
        jmp xx
        blah blah
        jmp yy
        blah blah
    yy: blah blah