I couldn't find my answer in google, I want some basic information about "Table manipulation and multiple-way branching in assembly-language" with an example
Asked
Active
Viewed 52 times
-4
-
Can you provide more context? – Margaret Bloom Sep 21 '17 at 12:39
-
2Seems like OP is quoting from section 11.3 (Instruction Formats) of "Computer Organization and Architecture" by William Stallings, as found [here](https://archive.org/stream/ComputerOrganizationAndArchitecture/Computer%20Organization%20And%20Architecture_djvu.txt). – Ruud Helderman Sep 21 '17 at 12:50
-
2"multiple-way branching" is probably a jump table, e.g. a `switch` implementation. – Jester Sep 21 '17 at 13:01
-
@MargaretBloom even I don't know the context that's why I asked it here – Omar Karbasi Sep 21 '17 at 15:45
-
@RuudHelderman Yes I am reading that book (o_o) – Omar Karbasi Sep 21 '17 at 15:46
-
@OmarKarbasi The book was exactly the context MargaretBloom (and probably others) were looking for; mentioning your sources is also part of asking a good question on SO. – Ruud Helderman Sep 21 '17 at 16:44
-
@RuudHelderman Thank you for mentioning sorry that I am noob in this site, didn't know how to ask question now I started reading the rules of asking questions – Omar Karbasi Sep 21 '17 at 16:47
-
what assembly language were you interested in, is this a homework question or some other such task specifically, or just a curiosity? – old_timer Sep 21 '17 at 17:13
-
See https://stackoverflow.com/questions/3012011/switch-case-assembly-level-code. Could even mark this a duplicate, but it's not a good SO question in the first place and should probably just be deleted. You have to know some basics to ask a question, because "teach me about X" isn't a good question. – Peter Cordes Sep 21 '17 at 17:42
1 Answers
0
unsigned int fun ( unsigned int x )
{
switch(x&7)
{
case 0: return(x<<1);
case 1: return(2);
case 2: return(x>>1);
case 3: return(x+x);
case 4: return(x&3);
case 5: return(x+(x<<1));
case 6: return(5);
case 7: return(x+x+x);
}
}
one implementation.
00000000 <fun>:
0: e2003007 and r3, r0, #7
4: e2433001 sub r3, r3, #1
8: e3530006 cmp r3, #6
c: 979ff103 ldrls pc, [pc, r3, lsl #2]
10: ea000006 b 30 <fun+0x30>
14: 00000040 andeq r0, r0, r0, asr #32
18: 00000050 andeq r0, r0, r0, asr r0
1c: 00000030 andeq r0, r0, r0, lsr r0
20: 00000048 andeq r0, r0, r8, asr #32
24: 00000038 andeq r0, r0, r8, lsr r0
28: 00000058 andeq r0, r0, r8, asr r0
2c: 00000038 andeq r0, r0, r8, lsr r0
30: e1a00080 lsl r0, r0, #1
34: e12fff1e bx lr
38: e0800080 add r0, r0, r0, lsl #1
3c: e12fff1e bx lr
40: e3a00002 mov r0, #2
44: e12fff1e bx lr
48: e2000003 and r0, r0, #3
4c: e12fff1e bx lr
50: e1a000a0 lsr r0, r0, #1
54: e12fff1e bx lr
58: e3a00005 mov r0, #5
5c: e12fff1e bx lr
the program counter is computed programatically so based on inputs and can land at any one of those places based on the input and math. (nice catch on the optimization I was trying not to have any direct duplicate cases, but didnt try that hard)

old_timer
- 69,149
- 8
- 89
- 168
-
as jester said it is probably a jump table from a switch. Out of context what other way could you do multi-way branching? either mathmatically produce a program counter for instruction sets where you can do that or a jump table for instruction sets that allow that (or as in this case a combination of the two). Right, what else could those words mean? – old_timer Sep 21 '17 at 17:11
-
this is a give them a fish answer, I get beat up whenever a teach them to fish here... – old_timer Sep 21 '17 at 17:12