This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Stacking and interrupts

Hullo guys , I had to write my own assembly subroutines to be called from C files . From the ARM Cortex-M documentation , it's clear that when an interrupt occur , only R0->R3 , R12 are pushed into stack . So for safe operation , those 're the only registers allowed to be used when interrupts 're enabled ?

Parents
  • Well , I guess i will try my best not to use more than those 5 registers who r saved before the ISR

    It sounds like you haven't understood the situation.

    Your ISR must save any extra registers required, just as it would have to do for a normal C routine. If you follow this very simple rule, you can use any registers you want.

    Compared to some processors, the Cortex is a joy. People far smarter than me (and almost definitely the majority of the users of this forum) were involved in the design of it.

    Don't knock it before you have at least a basic understanding of it.

Reply
  • Well , I guess i will try my best not to use more than those 5 registers who r saved before the ISR

    It sounds like you haven't understood the situation.

    Your ISR must save any extra registers required, just as it would have to do for a normal C routine. If you follow this very simple rule, you can use any registers you want.

    Compared to some processors, the Cortex is a joy. People far smarter than me (and almost definitely the majority of the users of this forum) were involved in the design of it.

    Don't knock it before you have at least a basic understanding of it.

Children
  • If the ISR is in C, then the compiler's got you covered, it understands the register requirements, and pushes those registers that would otherwise get corrupted. On the Cortex parts you don't need the __isr directives, because unlike the ARM7/9 the Cortex has a unified calling conventions, with no hoops to jump through in IRQ/FIQ prologue/epilogue code.

    If you do it in assembler, then yes you'd better understand how the processor works, and the concept of preserving the operating context when you interrupt it. This isn't an ARM unique concept.

    Please pick up some manuals, and microprocessor texts, and study them.

  • Westonsupermare Pier , well I have been working with Cortex for quite 3 yrs now , but its my first time to encounter a situation in which I have to do some components in Assembly .
    I studied the Cortex-M4 programming manual provided by ST , and also the armasm User Guide but all that provided is the stacking frame . As u r saying its the work of the C compiler , so thats why I did not find much about it in those documentations .
    Thx for ur help man , and if u recommend further documentations , that would be nice .

  • The ARM documentation is very, very clear on what registers are preserved and what registers aren't. It's all in the ABI what a function may expect when being called, and what it may expect when calling some other function.

    If you need three extra registers, you push three extra registers. Doing it at the start? Maybe. That depends on if you know that you always needs three extra registers. If the code maybe only needs them once every 10 calls then it's better to delay with saving them until your code performs that special magic that does need them. But decisions about what registers to use and when to save them is all part of "engineering", i.e. all the little decisions you need to do in your daily work based on your goals, your needs and the path you have decided to take to reach your goals.