Assembly Language for Beginners

(nextflipdebug2) #1
1.15. SWITCH()/CASE/DEFAULT
.rodata:0000000000000077 DCB 3 ; case 20
.rodata:0000000000000078 DCB 3 ; case 21
.rodata:0000000000000079 DCB 0 ; case 22
.rodata:000000000000007B ; .rodata ends

So in case of 1, 9 is to be multiplied by 4 and added to the address ofLrtx4label.

In case of 22, 0 is to be multiplied by 4, resulting in 0.

Right after theLrtx4label is theL7label, where you can find the code that prints “22”.

There is no jump table in the code segment, it’s allocated in a separate .rodata section (there is no special
necessity to place it in the code section).

There are also negative bytes (0xF7), they are used for jumping back to the code that prints the “default”
string (at.L2).

1.15.4 Fall-through


Another popular usage ofswitch()operator is so-called “fallthrough”. Here is simple example^98 :

1 bool is_whitespace(char c) {
2 switch (c) {
3 case ' ': // fallthrough
4 case '\t': // fallthrough
5 case '\r': // fallthrough
6 case '\n':
7 return true;
8 default: // not whitespace
9 return false;
10 }
11 }


Slightly harder, from Linux kernel^99 :

1 char nco1, nco2;
2
3 void f(int if_freq_khz)
4 {
5
6 switch (if_freq_khz) {
7 default:
8 printf("IF=%d KHz is not supportted, 3250 assumed\n", if_freq_khz);
9 / fallthrough /
10 case 3250: / 3.25Mhz /
11 nco1 = 0x34;
12 nco2 = 0x00;
13 break;
14 case 3500: / 3.50Mhz /
15 nco1 = 0x38;
16 nco2 = 0x00;
17 break;
18 case 4000: / 4.00Mhz /
19 nco1 = 0x40;
20 nco2 = 0x00;
21 break;
22 case 5000: / 5.00Mhz /
23 nco1 = 0x50;
24 nco2 = 0x00;
25 break;
26 case 5380: / 5.38Mhz /
27 nco1 = 0x56;
28 nco2 = 0x14;
29 break;
30 }
31 };


(^98) Copypasted fromhttps://github.com/azonalon/prgraas/blob/master/prog1lib/lecture_examples/is_whitespace.c
(^99) Copypasted fromhttps://github.com/torvalds/linux/blob/master/drivers/media/dvb-frontends/lgdt3306a.c

Free download pdf