Warm tip: This article is reproduced from serverfault.com, please click

Understanding Syntax of ARM Vector Table Assembly Declaration

发布于 2020-11-28 04:04:01

I'm teaching myself some ARM assembly and am struggling to find resources that describe a particular form of a reset vector table declaration.

CODE32
Vector_Table

reset_vector
    b reset_handler
undefined_exception
    b reset_handler
svc_exception
    b reset_handler
etc...

What I'm confused about is the use of the "Vector_Table" statement, which at first glance I thought was a label, but after looking at documentation realized that labels are terminated with a ":". Has anyone seen this form before and can explain the syntax? I'm not finding anything useful in my Google searches.

My naive guess is that the linker will assign "Vector_Table" and "reset_vector" symbols to the same address, such that they equal to the first branch instruction.

Questioner
Brandon Braun
Viewed
0
old_timer 2020-12-02 01:27:25

These are all labels:

Vector_Table
reset_vector
reset_handler
undefined_exception
svc_exception

Assembly language is specific to the tool not the target (ARM). Some assemblers (for ARM or in general) do not use a colon to mark a label, others do, others have a different solution. Note this has nothing whatsoever to do with Intel and AT&T and the x86. Assembler authors are completely free to invent whatever language they want so long as it produces usable machine code (well, it does not even have to do that, but how useful will it be if you cannot write programs with it).

ARM itself has had at least three generations of assemblers if not more, they acquired Keil not to far back who was strong in MCUs and multiple targets, one of the big names in professional tools that you would find a lot of corporations and government agencies using (they prefer to pay lots of money to have a phone number they can call for support rather than a free tool that the cannot as easily get the same level of support). I used at least three generations of tools from ARM, but no longer have access and have not used the Keil tools at all, I suspect they have had their tools swapped with a flavor of ARMs. GNU's assembler when you execute the program is called as for a native target so GNU assembler is often shortened to gas. Llvm does not have an assembler for the target it has one for its internal bytecode language. Llvm being a major competitor to gnu in the free tools options, clang is their main C compiler and unfortunately their assembler as well, claiming to support GNU's interpretation of things although there are holes in that as Stack Overflow questions have shown.

Variations in assembly languages are not limited nor specific to the instructions

mov r0,r1

You will find differences there but sometimes you will find that most of the differences in the language are the non-instruction areas like labels, comments, declarations of data (.byte, DB, etc), macros, etc. The classic tools from ARM you will see a lot of here at Stack Overflow, not as much as GNU assembler, but still more than enough does not use a lot of colons and periods label:, .section .text, .word, etc as you see in GNU assembler. A lot of assembly languages for a wide array of targets over history use a semi colon ; for comments. GNU assembler for ARM uses an at sign @ instead. A colon is a way to add more than one instruction per line mov r0,r1 ; add r3,r0,r3, which is unheard of in many other assembly languages.

It is misleading to call it a vector table as it is an exception table for this generation/flavor of ARM processor, not vectors. You execute the instruction at the magic address you do not find a vector (address) which generally means you need to use the b instruction (branch) or ldr pc,label to branch out of the 4 byte table location in a single instruction. The exception table and handlers are documented in the ARM Architectural Reference Manual and you should start with the armv5 version if you do not have a reference. If your question is strictly related to the labels then you likely already have acquired this document before reading or writing assembly language.

Being labels yes Vector_Table and reset_vector would have the same address as written.

Using GNU assembler (gas) the syntax would look something like this:

.code 32
Vector_Table:

reset_vector:
    b reset_handler
undefined_instruction:
    b reset_handler
    

And when linked

    Disassembly of section .text:

00000000 <Vector_Table>:
   0:   ea000000    b   8 <reset_handler>

00000004 <undefined_instruction>:
   4:   eaffffff    b   8 <reset_handler>

00000008 <reset_handler>:
   8:   eafffffe    b   8 <reset_handler>

and the nm tool shows

00000008 t reset_handler
00000000 t reset_vector
00000004 t undefined_instruction
00000000 t Vector_Table

In this case Vector_Table and reset_vector are at the same address and for the low/normal location for the exception table that address is going to be 0x00000000.

If this other assembler or toolchain with this assembler has the same richness of tools as GNU binutils or the file formats are such that you can use binutils you can also examine what the tools have produced and see these same things.

While this link may go stale at some point it was easy to google an example

https://www.keil.com/support/man/docs/armasm/armasm_dom1359731144051.htm

        AREA     ARMex, CODE, READONLY
                                ; Name this block of code ARMex
        ENTRY                   ; Mark first instruction to execute
start
        MOV      r0, #10        ; Set up parameters
        MOV      r1, #3
        ADD      r0, r0, r1     ; r0 = r0 + r1
stop
        MOV      r0, #0x18      ; angel_SWIreason_ReportException
        LDR      r1, =0x20026   ; ADP_Stopped_ApplicationExit
        SVC      #0x123456      ; ARM semihosting (formerly SWI)
        END                     ; Mark end of file

semicolons for comments no colons on the labels, etc.

Note if you have to port back and forth I tend to use ;@ to mark comments at the end of the line so that it works for both languages. One less thing you have to port. YMMV.

Interesting that same chunk of code goes back to the ARM ADS (ARM Developers Suite that predated ADT and RealView tools), this is also something you can google to see the ARM ADS tools document including syntax.