How do I write a given address to a register using inline assembly










0














I have the following statement:



asm volatile("ldr r0, =0x10000");


Instead of writing 0x10000 I want to write a parameter so it will look like this:



uint32_t addr = 0x20000;
asm volatile("ldr r0, =addr");


how can I manage to do it?



i am using ARM processor.



I want to do this:



At the end I want to do this:

asm volatile("ldr r0, =0x10000");
asm volatile("ldr r0, [r0]");
asm volatile("mov sp, r0");


same for pc



thanks.










share|improve this question



















  • 4




    That depends on your compiler. What compiler are you using?
    – Some programmer dude
    Nov 12 at 10:06






  • 3




    BTW it doesn't make much sense to just write to a register in the middle of some C code, you are either going to mess up the code generated by the compiler (if you don't specify the clobber list) or just have it overwritten immediately after. If instead this is to prepare data for another assembly instruction, they should both be in the same assembly block, and generally ldr is redundant in extended asm if you specify your constraints/clobber lists correctly correctly. You should really provide some more context about what you are doing.
    – Matteo Italia
    Nov 12 at 10:14
















0














I have the following statement:



asm volatile("ldr r0, =0x10000");


Instead of writing 0x10000 I want to write a parameter so it will look like this:



uint32_t addr = 0x20000;
asm volatile("ldr r0, =addr");


how can I manage to do it?



i am using ARM processor.



I want to do this:



At the end I want to do this:

asm volatile("ldr r0, =0x10000");
asm volatile("ldr r0, [r0]");
asm volatile("mov sp, r0");


same for pc



thanks.










share|improve this question



















  • 4




    That depends on your compiler. What compiler are you using?
    – Some programmer dude
    Nov 12 at 10:06






  • 3




    BTW it doesn't make much sense to just write to a register in the middle of some C code, you are either going to mess up the code generated by the compiler (if you don't specify the clobber list) or just have it overwritten immediately after. If instead this is to prepare data for another assembly instruction, they should both be in the same assembly block, and generally ldr is redundant in extended asm if you specify your constraints/clobber lists correctly correctly. You should really provide some more context about what you are doing.
    – Matteo Italia
    Nov 12 at 10:14














0












0








0







I have the following statement:



asm volatile("ldr r0, =0x10000");


Instead of writing 0x10000 I want to write a parameter so it will look like this:



uint32_t addr = 0x20000;
asm volatile("ldr r0, =addr");


how can I manage to do it?



i am using ARM processor.



I want to do this:



At the end I want to do this:

asm volatile("ldr r0, =0x10000");
asm volatile("ldr r0, [r0]");
asm volatile("mov sp, r0");


same for pc



thanks.










share|improve this question















I have the following statement:



asm volatile("ldr r0, =0x10000");


Instead of writing 0x10000 I want to write a parameter so it will look like this:



uint32_t addr = 0x20000;
asm volatile("ldr r0, =addr");


how can I manage to do it?



i am using ARM processor.



I want to do this:



At the end I want to do this:

asm volatile("ldr r0, =0x10000");
asm volatile("ldr r0, [r0]");
asm volatile("mov sp, r0");


same for pc



thanks.







c arm inline-assembly






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 14:06

























asked Nov 12 at 10:04









Vlad Shlimovich

162




162







  • 4




    That depends on your compiler. What compiler are you using?
    – Some programmer dude
    Nov 12 at 10:06






  • 3




    BTW it doesn't make much sense to just write to a register in the middle of some C code, you are either going to mess up the code generated by the compiler (if you don't specify the clobber list) or just have it overwritten immediately after. If instead this is to prepare data for another assembly instruction, they should both be in the same assembly block, and generally ldr is redundant in extended asm if you specify your constraints/clobber lists correctly correctly. You should really provide some more context about what you are doing.
    – Matteo Italia
    Nov 12 at 10:14













  • 4




    That depends on your compiler. What compiler are you using?
    – Some programmer dude
    Nov 12 at 10:06






  • 3




    BTW it doesn't make much sense to just write to a register in the middle of some C code, you are either going to mess up the code generated by the compiler (if you don't specify the clobber list) or just have it overwritten immediately after. If instead this is to prepare data for another assembly instruction, they should both be in the same assembly block, and generally ldr is redundant in extended asm if you specify your constraints/clobber lists correctly correctly. You should really provide some more context about what you are doing.
    – Matteo Italia
    Nov 12 at 10:14








4




4




That depends on your compiler. What compiler are you using?
– Some programmer dude
Nov 12 at 10:06




That depends on your compiler. What compiler are you using?
– Some programmer dude
Nov 12 at 10:06




3




3




BTW it doesn't make much sense to just write to a register in the middle of some C code, you are either going to mess up the code generated by the compiler (if you don't specify the clobber list) or just have it overwritten immediately after. If instead this is to prepare data for another assembly instruction, they should both be in the same assembly block, and generally ldr is redundant in extended asm if you specify your constraints/clobber lists correctly correctly. You should really provide some more context about what you are doing.
– Matteo Italia
Nov 12 at 10:14





BTW it doesn't make much sense to just write to a register in the middle of some C code, you are either going to mess up the code generated by the compiler (if you don't specify the clobber list) or just have it overwritten immediately after. If instead this is to prepare data for another assembly instruction, they should both be in the same assembly block, and generally ldr is redundant in extended asm if you specify your constraints/clobber lists correctly correctly. You should really provide some more context about what you are doing.
– Matteo Italia
Nov 12 at 10:14













1 Answer
1






active

oldest

votes


















0














gcc is quite capable of generating those instructions so you can do:



void** p = 0x10000;
register void* sp asm("sp") = *p;
__asm__ __volatile__("" : : "r" (sp) : "memory");


Note it's dangerous to mess with sp so make sure you know what you are doing.



To load pc you will indeed need to use a mov such as:



void** p = 0x10000;
__asm__ __volatile__("mov pc, %0" : : "r" (*p) : "memory");
__builtin_unreachable();





share|improve this answer






















  • hi, how I can do the same with pc?
    – Vlad Shlimovich
    Nov 12 at 12:50










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%2f53259825%2fhow-do-i-write-a-given-address-to-a-register-using-inline-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














gcc is quite capable of generating those instructions so you can do:



void** p = 0x10000;
register void* sp asm("sp") = *p;
__asm__ __volatile__("" : : "r" (sp) : "memory");


Note it's dangerous to mess with sp so make sure you know what you are doing.



To load pc you will indeed need to use a mov such as:



void** p = 0x10000;
__asm__ __volatile__("mov pc, %0" : : "r" (*p) : "memory");
__builtin_unreachable();





share|improve this answer






















  • hi, how I can do the same with pc?
    – Vlad Shlimovich
    Nov 12 at 12:50















0














gcc is quite capable of generating those instructions so you can do:



void** p = 0x10000;
register void* sp asm("sp") = *p;
__asm__ __volatile__("" : : "r" (sp) : "memory");


Note it's dangerous to mess with sp so make sure you know what you are doing.



To load pc you will indeed need to use a mov such as:



void** p = 0x10000;
__asm__ __volatile__("mov pc, %0" : : "r" (*p) : "memory");
__builtin_unreachable();





share|improve this answer






















  • hi, how I can do the same with pc?
    – Vlad Shlimovich
    Nov 12 at 12:50













0












0








0






gcc is quite capable of generating those instructions so you can do:



void** p = 0x10000;
register void* sp asm("sp") = *p;
__asm__ __volatile__("" : : "r" (sp) : "memory");


Note it's dangerous to mess with sp so make sure you know what you are doing.



To load pc you will indeed need to use a mov such as:



void** p = 0x10000;
__asm__ __volatile__("mov pc, %0" : : "r" (*p) : "memory");
__builtin_unreachable();





share|improve this answer














gcc is quite capable of generating those instructions so you can do:



void** p = 0x10000;
register void* sp asm("sp") = *p;
__asm__ __volatile__("" : : "r" (sp) : "memory");


Note it's dangerous to mess with sp so make sure you know what you are doing.



To load pc you will indeed need to use a mov such as:



void** p = 0x10000;
__asm__ __volatile__("mov pc, %0" : : "r" (*p) : "memory");
__builtin_unreachable();






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 14:23

























answered Nov 12 at 11:14









Jester

46.3k34581




46.3k34581











  • hi, how I can do the same with pc?
    – Vlad Shlimovich
    Nov 12 at 12:50
















  • hi, how I can do the same with pc?
    – Vlad Shlimovich
    Nov 12 at 12:50















hi, how I can do the same with pc?
– Vlad Shlimovich
Nov 12 at 12:50




hi, how I can do the same with pc?
– Vlad Shlimovich
Nov 12 at 12:50

















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%2f53259825%2fhow-do-i-write-a-given-address-to-a-register-using-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







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands