Confusion in gcc switch case assembly
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
My Prog.c contains a simple switch case as follows:
switch (x)
{
case 1:
p=2;
break;
case 2:
p=3;
break;
case 3:
p=4;
break;
case 4:
p=5;
break;
case 5:
p=6;
break;
default:
break;
}
I compiled this program with
gcc -g -v prog.c
and created objdump with
objdump -S -l a.out>dump_file
For the switch case statement, I have found that one indirect branch has been created (jmp *%eax
).
But if I remove any one of the case statement, no indirect branch is created.
gcc version: 5.4.0
I could not understand why this is happening?
c
add a comment |
My Prog.c contains a simple switch case as follows:
switch (x)
{
case 1:
p=2;
break;
case 2:
p=3;
break;
case 3:
p=4;
break;
case 4:
p=5;
break;
case 5:
p=6;
break;
default:
break;
}
I compiled this program with
gcc -g -v prog.c
and created objdump with
objdump -S -l a.out>dump_file
For the switch case statement, I have found that one indirect branch has been created (jmp *%eax
).
But if I remove any one of the case statement, no indirect branch is created.
gcc version: 5.4.0
I could not understand why this is happening?
c
1
It might be useful to show us the output. Currently you could simply doif (x>=1 && x<=5) p=x+1;
or jump to an address calculated fromx
. This does not work if you have a gap in the cases list.
– Gerhardh
Nov 22 '18 at 12:39
Compiler optimization decide to use indirect jumps than multiple comparisons.
– EsmaeelE
Nov 22 '18 at 12:40
maybe similar stackoverflow.com/a/11668346/7508077
– EsmaeelE
Nov 22 '18 at 12:44
add a comment |
My Prog.c contains a simple switch case as follows:
switch (x)
{
case 1:
p=2;
break;
case 2:
p=3;
break;
case 3:
p=4;
break;
case 4:
p=5;
break;
case 5:
p=6;
break;
default:
break;
}
I compiled this program with
gcc -g -v prog.c
and created objdump with
objdump -S -l a.out>dump_file
For the switch case statement, I have found that one indirect branch has been created (jmp *%eax
).
But if I remove any one of the case statement, no indirect branch is created.
gcc version: 5.4.0
I could not understand why this is happening?
c
My Prog.c contains a simple switch case as follows:
switch (x)
{
case 1:
p=2;
break;
case 2:
p=3;
break;
case 3:
p=4;
break;
case 4:
p=5;
break;
case 5:
p=6;
break;
default:
break;
}
I compiled this program with
gcc -g -v prog.c
and created objdump with
objdump -S -l a.out>dump_file
For the switch case statement, I have found that one indirect branch has been created (jmp *%eax
).
But if I remove any one of the case statement, no indirect branch is created.
gcc version: 5.4.0
I could not understand why this is happening?
c
c
edited Nov 22 '18 at 13:45
EsmaeelE
1,17221222
1,17221222
asked Nov 22 '18 at 11:50
Moumita DasMoumita Das
91
91
1
It might be useful to show us the output. Currently you could simply doif (x>=1 && x<=5) p=x+1;
or jump to an address calculated fromx
. This does not work if you have a gap in the cases list.
– Gerhardh
Nov 22 '18 at 12:39
Compiler optimization decide to use indirect jumps than multiple comparisons.
– EsmaeelE
Nov 22 '18 at 12:40
maybe similar stackoverflow.com/a/11668346/7508077
– EsmaeelE
Nov 22 '18 at 12:44
add a comment |
1
It might be useful to show us the output. Currently you could simply doif (x>=1 && x<=5) p=x+1;
or jump to an address calculated fromx
. This does not work if you have a gap in the cases list.
– Gerhardh
Nov 22 '18 at 12:39
Compiler optimization decide to use indirect jumps than multiple comparisons.
– EsmaeelE
Nov 22 '18 at 12:40
maybe similar stackoverflow.com/a/11668346/7508077
– EsmaeelE
Nov 22 '18 at 12:44
1
1
It might be useful to show us the output. Currently you could simply do
if (x>=1 && x<=5) p=x+1;
or jump to an address calculated from x
. This does not work if you have a gap in the cases list.– Gerhardh
Nov 22 '18 at 12:39
It might be useful to show us the output. Currently you could simply do
if (x>=1 && x<=5) p=x+1;
or jump to an address calculated from x
. This does not work if you have a gap in the cases list.– Gerhardh
Nov 22 '18 at 12:39
Compiler optimization decide to use indirect jumps than multiple comparisons.
– EsmaeelE
Nov 22 '18 at 12:40
Compiler optimization decide to use indirect jumps than multiple comparisons.
– EsmaeelE
Nov 22 '18 at 12:40
maybe similar stackoverflow.com/a/11668346/7508077
– EsmaeelE
Nov 22 '18 at 12:44
maybe similar stackoverflow.com/a/11668346/7508077
– EsmaeelE
Nov 22 '18 at 12:44
add a comment |
1 Answer
1
active
oldest
votes
This is happen because of Branch Tables. to avoid it compile with
gcc -g -v -fno-jump-tables SO.c
If you compile with jump tables [a feature in gcc compiler] size of dump_file
is: 9,965 bytes
But without it size is: 11,493 bytes.
Jump tables helps to reduce object code size by optimize and relocate some jump instructions.
see stackoverflow.com/a/15799358/7508077
– EsmaeelE
Nov 22 '18 at 12:52
see avrfreaks.net/forum/using-indirect-jump-c
– EsmaeelE
Nov 22 '18 at 13:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430398%2fconfusion-in-gcc-switch-case-assembly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is happen because of Branch Tables. to avoid it compile with
gcc -g -v -fno-jump-tables SO.c
If you compile with jump tables [a feature in gcc compiler] size of dump_file
is: 9,965 bytes
But without it size is: 11,493 bytes.
Jump tables helps to reduce object code size by optimize and relocate some jump instructions.
see stackoverflow.com/a/15799358/7508077
– EsmaeelE
Nov 22 '18 at 12:52
see avrfreaks.net/forum/using-indirect-jump-c
– EsmaeelE
Nov 22 '18 at 13:22
add a comment |
This is happen because of Branch Tables. to avoid it compile with
gcc -g -v -fno-jump-tables SO.c
If you compile with jump tables [a feature in gcc compiler] size of dump_file
is: 9,965 bytes
But without it size is: 11,493 bytes.
Jump tables helps to reduce object code size by optimize and relocate some jump instructions.
see stackoverflow.com/a/15799358/7508077
– EsmaeelE
Nov 22 '18 at 12:52
see avrfreaks.net/forum/using-indirect-jump-c
– EsmaeelE
Nov 22 '18 at 13:22
add a comment |
This is happen because of Branch Tables. to avoid it compile with
gcc -g -v -fno-jump-tables SO.c
If you compile with jump tables [a feature in gcc compiler] size of dump_file
is: 9,965 bytes
But without it size is: 11,493 bytes.
Jump tables helps to reduce object code size by optimize and relocate some jump instructions.
This is happen because of Branch Tables. to avoid it compile with
gcc -g -v -fno-jump-tables SO.c
If you compile with jump tables [a feature in gcc compiler] size of dump_file
is: 9,965 bytes
But without it size is: 11,493 bytes.
Jump tables helps to reduce object code size by optimize and relocate some jump instructions.
edited Nov 22 '18 at 12:58
answered Nov 22 '18 at 12:47
EsmaeelEEsmaeelE
1,17221222
1,17221222
see stackoverflow.com/a/15799358/7508077
– EsmaeelE
Nov 22 '18 at 12:52
see avrfreaks.net/forum/using-indirect-jump-c
– EsmaeelE
Nov 22 '18 at 13:22
add a comment |
see stackoverflow.com/a/15799358/7508077
– EsmaeelE
Nov 22 '18 at 12:52
see avrfreaks.net/forum/using-indirect-jump-c
– EsmaeelE
Nov 22 '18 at 13:22
see stackoverflow.com/a/15799358/7508077
– EsmaeelE
Nov 22 '18 at 12:52
see stackoverflow.com/a/15799358/7508077
– EsmaeelE
Nov 22 '18 at 12:52
see avrfreaks.net/forum/using-indirect-jump-c
– EsmaeelE
Nov 22 '18 at 13:22
see avrfreaks.net/forum/using-indirect-jump-c
– EsmaeelE
Nov 22 '18 at 13:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430398%2fconfusion-in-gcc-switch-case-assembly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It might be useful to show us the output. Currently you could simply do
if (x>=1 && x<=5) p=x+1;
or jump to an address calculated fromx
. This does not work if you have a gap in the cases list.– Gerhardh
Nov 22 '18 at 12:39
Compiler optimization decide to use indirect jumps than multiple comparisons.
– EsmaeelE
Nov 22 '18 at 12:40
maybe similar stackoverflow.com/a/11668346/7508077
– EsmaeelE
Nov 22 '18 at 12:44