how to write __asm__ goto inline assembly












1














I m using a GCC compiler. I want to insert some inline instructions into C. Non-branching instructions are inserted successfully using __asm__ directive, as follows:



  __asm__ volatile (
"add %[my_out], %[my_op1], %[my_op2]n"
: [my_out] "=&r" (array[3])
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
);


But i cant inset a branch/jump instruction. Compilation gives the following error:



../test.c:5: error: expected '(' before 'goto'
../test.c:5: error: expected identifier or '*' before 'volatile'


I tried the following code:



unsigned int array[10] = { 0x22, 0x12,0,0,0,0,0,0,0};
int main() {


__asm__ goto volatile (
"jp %l[label]n"
: /*no output*/
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
: /*no cobblers*/
: label
);


return 0;

label:

__asm__ volatile (
"add %[my_out], %[my_op1], %[my_op2]n"
: [my_out] "=&r" (array[3])
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
);


return 0;

}


can you please help in correcting this code?
Regards










share|improve this question


















  • 1




    place volatile keyword before goto rather than after.In your case you can remove the volatile in your first statement given that you have no output (or input/output) constraints. When you have none of those asm is has an implicit volatile. Your second asm would still have to be marked volatile
    – Michael Petch
    Nov 21 at 9:02








  • 1




    To add to what Michael said, since asm goto cannot have outputs, and since asm statements without outputs are implicitly volatile, all asm goto statements are implicitly volatile. You might also want to check out the docs which would have shown you the order for the qualifiers.
    – David Wohlferd
    Nov 21 at 9:22






  • 1




    That should have worked. What's your target hw/os? And what version of gcc are you using?
    – David Wohlferd
    Nov 21 at 10:32






  • 1




    What version of GCC are you using? You can use gcc -v to find out. When you made the fix suggested did the error change?
    – Michael Petch
    Nov 21 at 10:53








  • 2




    asm goto wasn't available until GCC 4.5
    – Michael Petch
    Nov 21 at 12:53
















1














I m using a GCC compiler. I want to insert some inline instructions into C. Non-branching instructions are inserted successfully using __asm__ directive, as follows:



  __asm__ volatile (
"add %[my_out], %[my_op1], %[my_op2]n"
: [my_out] "=&r" (array[3])
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
);


But i cant inset a branch/jump instruction. Compilation gives the following error:



../test.c:5: error: expected '(' before 'goto'
../test.c:5: error: expected identifier or '*' before 'volatile'


I tried the following code:



unsigned int array[10] = { 0x22, 0x12,0,0,0,0,0,0,0};
int main() {


__asm__ goto volatile (
"jp %l[label]n"
: /*no output*/
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
: /*no cobblers*/
: label
);


return 0;

label:

__asm__ volatile (
"add %[my_out], %[my_op1], %[my_op2]n"
: [my_out] "=&r" (array[3])
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
);


return 0;

}


can you please help in correcting this code?
Regards










share|improve this question


















  • 1




    place volatile keyword before goto rather than after.In your case you can remove the volatile in your first statement given that you have no output (or input/output) constraints. When you have none of those asm is has an implicit volatile. Your second asm would still have to be marked volatile
    – Michael Petch
    Nov 21 at 9:02








  • 1




    To add to what Michael said, since asm goto cannot have outputs, and since asm statements without outputs are implicitly volatile, all asm goto statements are implicitly volatile. You might also want to check out the docs which would have shown you the order for the qualifiers.
    – David Wohlferd
    Nov 21 at 9:22






  • 1




    That should have worked. What's your target hw/os? And what version of gcc are you using?
    – David Wohlferd
    Nov 21 at 10:32






  • 1




    What version of GCC are you using? You can use gcc -v to find out. When you made the fix suggested did the error change?
    – Michael Petch
    Nov 21 at 10:53








  • 2




    asm goto wasn't available until GCC 4.5
    – Michael Petch
    Nov 21 at 12:53














1












1








1







I m using a GCC compiler. I want to insert some inline instructions into C. Non-branching instructions are inserted successfully using __asm__ directive, as follows:



  __asm__ volatile (
"add %[my_out], %[my_op1], %[my_op2]n"
: [my_out] "=&r" (array[3])
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
);


But i cant inset a branch/jump instruction. Compilation gives the following error:



../test.c:5: error: expected '(' before 'goto'
../test.c:5: error: expected identifier or '*' before 'volatile'


I tried the following code:



unsigned int array[10] = { 0x22, 0x12,0,0,0,0,0,0,0};
int main() {


__asm__ goto volatile (
"jp %l[label]n"
: /*no output*/
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
: /*no cobblers*/
: label
);


return 0;

label:

__asm__ volatile (
"add %[my_out], %[my_op1], %[my_op2]n"
: [my_out] "=&r" (array[3])
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
);


return 0;

}


can you please help in correcting this code?
Regards










share|improve this question













I m using a GCC compiler. I want to insert some inline instructions into C. Non-branching instructions are inserted successfully using __asm__ directive, as follows:



  __asm__ volatile (
"add %[my_out], %[my_op1], %[my_op2]n"
: [my_out] "=&r" (array[3])
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
);


But i cant inset a branch/jump instruction. Compilation gives the following error:



../test.c:5: error: expected '(' before 'goto'
../test.c:5: error: expected identifier or '*' before 'volatile'


I tried the following code:



unsigned int array[10] = { 0x22, 0x12,0,0,0,0,0,0,0};
int main() {


__asm__ goto volatile (
"jp %l[label]n"
: /*no output*/
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
: /*no cobblers*/
: label
);


return 0;

label:

__asm__ volatile (
"add %[my_out], %[my_op1], %[my_op2]n"
: [my_out] "=&r" (array[3])
: [my_op1] "r" (array[0]),[my_op2] "r" (array[1])
);


return 0;

}


can you please help in correcting this code?
Regards







gcc inline-assembly






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 8:19









Sajjad

276




276








  • 1




    place volatile keyword before goto rather than after.In your case you can remove the volatile in your first statement given that you have no output (or input/output) constraints. When you have none of those asm is has an implicit volatile. Your second asm would still have to be marked volatile
    – Michael Petch
    Nov 21 at 9:02








  • 1




    To add to what Michael said, since asm goto cannot have outputs, and since asm statements without outputs are implicitly volatile, all asm goto statements are implicitly volatile. You might also want to check out the docs which would have shown you the order for the qualifiers.
    – David Wohlferd
    Nov 21 at 9:22






  • 1




    That should have worked. What's your target hw/os? And what version of gcc are you using?
    – David Wohlferd
    Nov 21 at 10:32






  • 1




    What version of GCC are you using? You can use gcc -v to find out. When you made the fix suggested did the error change?
    – Michael Petch
    Nov 21 at 10:53








  • 2




    asm goto wasn't available until GCC 4.5
    – Michael Petch
    Nov 21 at 12:53














  • 1




    place volatile keyword before goto rather than after.In your case you can remove the volatile in your first statement given that you have no output (or input/output) constraints. When you have none of those asm is has an implicit volatile. Your second asm would still have to be marked volatile
    – Michael Petch
    Nov 21 at 9:02








  • 1




    To add to what Michael said, since asm goto cannot have outputs, and since asm statements without outputs are implicitly volatile, all asm goto statements are implicitly volatile. You might also want to check out the docs which would have shown you the order for the qualifiers.
    – David Wohlferd
    Nov 21 at 9:22






  • 1




    That should have worked. What's your target hw/os? And what version of gcc are you using?
    – David Wohlferd
    Nov 21 at 10:32






  • 1




    What version of GCC are you using? You can use gcc -v to find out. When you made the fix suggested did the error change?
    – Michael Petch
    Nov 21 at 10:53








  • 2




    asm goto wasn't available until GCC 4.5
    – Michael Petch
    Nov 21 at 12:53








1




1




place volatile keyword before goto rather than after.In your case you can remove the volatile in your first statement given that you have no output (or input/output) constraints. When you have none of those asm is has an implicit volatile. Your second asm would still have to be marked volatile
– Michael Petch
Nov 21 at 9:02






place volatile keyword before goto rather than after.In your case you can remove the volatile in your first statement given that you have no output (or input/output) constraints. When you have none of those asm is has an implicit volatile. Your second asm would still have to be marked volatile
– Michael Petch
Nov 21 at 9:02






1




1




To add to what Michael said, since asm goto cannot have outputs, and since asm statements without outputs are implicitly volatile, all asm goto statements are implicitly volatile. You might also want to check out the docs which would have shown you the order for the qualifiers.
– David Wohlferd
Nov 21 at 9:22




To add to what Michael said, since asm goto cannot have outputs, and since asm statements without outputs are implicitly volatile, all asm goto statements are implicitly volatile. You might also want to check out the docs which would have shown you the order for the qualifiers.
– David Wohlferd
Nov 21 at 9:22




1




1




That should have worked. What's your target hw/os? And what version of gcc are you using?
– David Wohlferd
Nov 21 at 10:32




That should have worked. What's your target hw/os? And what version of gcc are you using?
– David Wohlferd
Nov 21 at 10:32




1




1




What version of GCC are you using? You can use gcc -v to find out. When you made the fix suggested did the error change?
– Michael Petch
Nov 21 at 10:53






What version of GCC are you using? You can use gcc -v to find out. When you made the fix suggested did the error change?
– Michael Petch
Nov 21 at 10:53






2




2




asm goto wasn't available until GCC 4.5
– Michael Petch
Nov 21 at 12:53




asm goto wasn't available until GCC 4.5
– Michael Petch
Nov 21 at 12:53

















active

oldest

votes











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%2f53407795%2fhow-to-write-asm-goto-inline-assembly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53407795%2fhow-to-write-asm-goto-inline-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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'