How do you make an array of Labels in C/C++?

Asked

Viewed 116 times

1

It is a fact that exists in Assembly (Nasm) and it is a fact that this is a quick way to follow to a point of the program among several. Here is an example code for x64 where you choose one of five print codes setting 0 or 1 or 2 or 3 or up to 4 on the line mov eax, 2 (in case, starts set 2 and printa Label 2!\n).

section .text
    global _start
_start:
    mov eax, 2       ;la index
    mov ebx, 1       ;file descriptor (stdout)
    mov rcx, [la+8*eax]
    mov edx, 9       ;message length
    mov eax, 4       ;system call number (sys_write)
    jmp rcx
_l0:
    mov ecx, l0      ;message to write
    int 0x80         ;call kernel
    jmp _end
_l1:
    mov ecx, l1      ;message to write
    int 0x80         ;call kernel
    jmp _end
_l2:
    mov ecx, l2      ;message to write
    int 0x80         ;call kernel
    jmp _end
_l3:
    mov ecx, l3      ;message to write
    int 0x80         ;call kernel
    jmp _end
_l4:
    mov ecx, l4      ;message to write
    int 0x80         ;call kernel
_end:
    mov eax, 1       ;system call number (sys_exit)
    int 0x80         ;call kernel

section .data

nu  db  '%08X',10
l0  db  'Label 0!',10
l1  db  'Label 1!',10
l2  db  'Label 2!',10
l3  db  'Label 3!',10
l4  db  'Label 4!',10
la  dq  _l0 , _l1 , _l2 , _l3 , _l4

Is it possible to do this in C/C++? Like something like this.

void print( int index ){
    const label_t print[5] = { _print0 , _print1 , _print2 , _print3 , _print4 } ;
    goto print[index%5] ;
_print0:
    printf("Label 0!\n") ;
    return ;
_print1:
    printf("Label 1!\n") ;
    return ;
_print2:
    printf("Label 2!\n") ;
    return ;
_print3:
    printf("Label 3!\n") ;
    return ;
_print4:
    printf("Label 4!\n") ;
    return ;
}

I am required to use switch-case and hope that the compiler uses the array of Labels or there is a way to ensure this operation of the array of Labels whenever the programmer knows that a value will be restricted to index values, even if the compiler does not know?

  • May I ask why you would like that? If you gain performance: the gain on this is so irrelevant but I believe the code is less readable and you lose maintainability, in general, you lose with this.

  • In case of complicated mathematical formulas, it gives in many ways to greatly optimize and allow frequent use of weight-free function in consciousness, type in digital games where each frame has to run in time.

  • Obviously, there are N ways to optimize a code. The given example doesn’t even make sense for performance gain. Most of the time, in my opinion, your effort will be much greater to optimize and the gain from this basically nothing (this assuming that you are not in the prototyping phase, where code is usually made to "work", then improved).

  • Lack experience to imagine what I speak. Flw.

  • I’m sorry, I don’t understand your argument.

  • It gives for example to select a small formula among several without several conditions instead of making the conditionals, the switch or even a huge formula. You get a lot of performance depending on what you’re doing.

  • If it is a formula applied in a loop to calculate a table, the table is formed more quickly.

  • The compiler knows when there is a need to use tables for switch use. .case. There is no reason to make a "manual table". Look at the example with a small switch.. case: https://godbolt.org/z/_y2nlx. The -O3 flag has been passed, as you want performance. (I had to put some values so he wouldn’t ignore the commands because they weren’t used).

  • Here, VS didn’t do this in previous tests, but I did another test with more options and he did. Too bad it cannot override the treatment of unreachable values, such as the case of guaranteeing the value from 0 to 10 and still the compiler treat the -2, -1, 11, 12, etc as default.

Show 4 more comments

1 answer

1


My previous answer was wrong, and so I eliminated it, even though other responses supported it. 1 2 3

Like you said, is to say what you don’t know, but as you said there was support for such, searched and found.

There is a extension of the GCC (and only this):

void *s[3] = {&&s0, &&s1, &&s2};

if (n >= 0 && n <=2)
    goto *s[n];

s0:
...
s1:
...
s2:
...

Behold: Labels as Values

  • I’ll take a look to see if it really suits me, because I use VC++. Anyway, just knowing that exists in C dialect of a GCC extension, it already changes a lot.

  • It seems there isn’t. Anyway, you answered correctly. Vlw.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.