What does 1: mean in assembly language?

↘锁芯ラ 提交于 2021-02-07 09:30:50

问题


I was reading the source code of RISC-V test pattern. And there is a macro define in riscv-test.h,

I want to know what does 1: means in this code:

#define RVTEST_CODE_BEGIN                                               \
        .section .text.init;                                            \
        .align  6;                                                      \
        .weak stvec_handler;                                            \
        .weak mtvec_handler;                                            \
        .globl _start;                                                  \
_start:                                                                 \
        /* reset vector */                                              \
        j reset_vector;                                                 \
        .align 2;                                                       \
trap_vector:                                                            \
        /* test whether the test came from pass/fail */                 \
        csrr t5, mcause;                                                \
        li t6, CAUSE_USER_ECALL;                                        \
        beq t5, t6, write_tohost;                                       \
        li t6, CAUSE_SUPERVISOR_ECALL;                                  \
        beq t5, t6, write_tohost;                                       \
        li t6, CAUSE_MACHINE_ECALL;                                     \
        beq t5, t6, write_tohost;                                       \
        /* if an mtvec_handler is defined, jump to it */                \
        la t5, mtvec_handler;                                           \
        beqz t5, 1f;                                                    \
        jr t5;                                                          \
        /* was it an interrupt or an exception? */                      \
  1:    csrr t5, mcause;                                                \
        bgez t5, handle_exception;                                      \
        INTERRUPT_HANDLER;                                            \  

回答1:


The 1: is a local symbol name; it's just a label. From the gas manual:

Local Symbol Names

Local symbols 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 symbol, write a label of the form N: (where N represents any positive integer). To refer to the most recent previous definition of that symbol 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 as well. So 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:        jra 1f 
2:        jra 1b 
1:        jra 2f 
2:        jra 1b

Which is the equivalent of:

label_1:  jra label_3 
label_2:  jra label_1 
label_3:  jra label_4
label_4:  jra label_3 

Local symbol names are only a notational device. They are immediately transformed into more conventional symbol names before the assembler uses them. ...

In your example that label is referenced a few instructions prior:

beqz t5, 1f;

The 1f there means "the next label named 1" and just jumps to that 1:.

It's a convenient way to define and reference local labels without having to think up unique names everywhere.

Here's another example:

#define TEST_JR_SRC1_BYPASS( testnum, nop_cycles, inst ) \
test_ ## testnum: \
    li  TESTNUM, testnum; \
    li  x4, 0; \
1:  la  x6, 2f; \
    TEST_INSERT_NOPS_ ## nop_cycles \
    inst x6; \
    bne x0, TESTNUM, fail; \
2:  addi  x4, x4, 1; \
    li  x5, 2; \
    bne x4, x5, 1b \

There we see the labels being referenced as 2f, 1b, etc.



来源:https://stackoverflow.com/questions/39432228/what-does-1-mean-in-assembly-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!