7

So I'm looking into the source code for Redox OS (An operating system made with Rust) just to see if I can learn something.

I'm reading the assembly file start.s in the bootloader folder. In the interrupt_vector_table label we have:

interrupt_vector_table:
    b . @ Reset
    b . 
    b . @ SWI instruction
    b . 
    b .
    b .
    b .
    b .

What exactly is b .?

I'm not a complete beginner in assembly, I just never came across this before.

old_timer
  • 69,149
  • 8
  • 89
  • 168
UndercoverCoder
  • 953
  • 3
  • 13
  • 28
  • 3
    Well, `b` is an unconditional branch, and I'd guess the dot means "here" (as in: the address of the the `b` instruction). So these look like stubs (infinite loops) meant to be replaced later if necessary. – Michael Jan 03 '18 at 21:00
  • @Michael - Thank you. But how come there are so many in one label one after the other? – UndercoverCoder Jan 03 '18 at 22:52
  • 2
    this is/looks like arm (full sized 32 bit, not cortex-m not aarch64). so the first 8 with some unused vectors are instructions you execute, not a bad idea to have the unused ones branch to self or branch to an infinite loop somewhere else. this way if you hit one (undefined instruction, data abort, prefetch abort) you wont go wandering into the boot code again and possibly misunderstanding what is going on. later changing those to real vectors (branch to a real place using branch or pc based load, has to be a single instruction). – old_timer Jan 04 '18 at 02:16
  • 1
    this is covered in the arm architectural reference manual for your arm core, good idea to start with the older arm arm, I think they have it listed as armv5 architectural reference manual, then the armv7 one adds a ton more complication overall (even though the basic events are the same although can now be moved). – old_timer Jan 04 '18 at 02:17
  • 1
    if you assemble then disassemble you will see that the b . encodes a branch to self, infinite loop. – old_timer Jan 04 '18 at 02:19
  • @old_timer - Thank you so much for the informative responses :) – UndercoverCoder Jan 04 '18 at 02:32
  • 1
    there are some more strange ones that gas supports like if you have a label with a number like 1: then not far away DOWN the page/code you see a b 1b that means label 1: backward. b 1f would mean label 1 forward. so these lines 1: ; b 1b ; b1f ; 1: there are two labels that match, but apparently that is okay although one has to question the sanity, the b 1b means branch backward one the b 1f means branch forward to label 1: and you can use other numbers. clearly a lazy programmer thing, or a macro expansion thing, a hard on the reader thing. – old_timer Jan 04 '18 at 02:36
  • 1
    again the assembly language is defined by the assembler, the program that reads it. gnu assembler (gas) is a separate assembler from arms various offerings over time or kiel (now arm) or ida or others. So no reason to assume that gas assembly language (b .) works on other assemblers, and vice versa. Sometimes you get lucky sometimes you dont, if you are new at assembly I recommend you avoid the ghee whiz stuff (specific to that assembler) and stick to creating new labels. this b . thing though is very handy and perhaps the exception when using gas to quickly add an infinite loop. – old_timer Jan 04 '18 at 02:39
  • 1
    sadly gnu assembler although backends made by different individuals seems to intentionally break what you know about assembly language for a target, for example what is written in the processor documentation (which is the syntax used by the processor or chip vendors tools). for example a semicolon has for eons been used to comment out in various assembly languages (and targets), not for the arm port to gas....plus many other annoyances arm and other backends that are not consistent in any way with the processor documentation (true another assembler). – old_timer Jan 04 '18 at 02:43
  • 1
    `.` in GAS works the same way as `$` in NASM / MASM. See https://stackoverflow.com/questions/47494744/how-does-work-in-nasm-exactly for a description of how it works. – Peter Cordes Jan 04 '18 at 02:57
  • 1
    eww $ historically denotes a hex number follows...LDA $15,X. I dont remember using that back when I did x86...(masm/tasm/borland asm, zortec, watcom, etc)...x86 went away from that, then other languages like C and others influenced the assembly languages...I dont know that the . in gas is as universal as that, would have to try it. perhaps it is... – old_timer Jan 04 '18 at 03:30

1 Answers1

16

The b instruction for the ARM CPU is nearly the same as the jmp instruction for the x86 CPU: A jump instruction

Using the GNU tool chain . means: Address of the instruction itself.

So b . is equal to:

temporaryLabel:
    b temporaryLabel

or (for x86 CPUs):

temporaryLabel:
    jmp temporaryLabel
Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • Ahhhh that makes plenty of sense. I'm familiar with `jmp` but new to `b`. Thank you very much. – UndercoverCoder Jan 03 '18 at 21:08
  • Question, if it's like `jmp`, why is it used so many times in the label when they all do the same thing? – UndercoverCoder Jan 03 '18 at 22:04
  • 1
    @UndercoverCoder: Looks to me like all the interrupt vectors are infinite loops until replaced with jumps to actual handlers for different interrupts. – Peter Cordes Jan 03 '18 at 22:51
  • 1
    it is an assembly language thing where assembly language is defined by the assembler the tool you use. this isnt and shouldnt be limited just to arm, but gnu perhaps has some others. Works for x86 (jmp .), works for mips, avr, pdp11, and I stopped trying there. the . in gnu assembly (not a universal rule as individuals make those backends) means here or branch/jump to self. – old_timer Jan 04 '18 at 02:13