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;
}







-1















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?










share|improve this question




















  • 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













  • 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















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?










share|improve this question




















  • 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













  • 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








-1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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











  • maybe similar stackoverflow.com/a/11668346/7508077

    – EsmaeelE
    Nov 22 '18 at 12:44
















  • 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













  • 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














1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer


























  • 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












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
});


}
});














draft saved

draft discarded


















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









0














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.






share|improve this answer


























  • 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
















0














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.






share|improve this answer


























  • 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














0












0








0







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)