A little bit of explaination on assembler

Hi,
at the Debugging Hard Fault & Other Exceptions url I find a bit of assembly that I’ve tried to figure out without, however, really understanding what it does:

tst lr, #4                                            
ite eq                                                
mrseq r0, msp                                         
mrsne r0, psp                                         
ldr r1, [r0, #24]                                     
ldr r2, handler2_address_const                        
bx r2                                                 
handler2_address_const: .word prvGetRegistersFromStack

could you explain to me line by line what they do

best regards
Max

The ARM architecture reference manual for your CPU will help you.

Here’s what these instructions do.

“tst…”: Check the special exception-return value in the LR to see which stack pointer was active at the time of the exception.
“ite…” Tell the CPU we have an if-then-else coming up using the EQ condition
“mrseq…” Move the main stack pointer into r0 (if the main stack pointer was active)
“mrsne…” Move the process stack pointer into r0 (if the process stack pointer was active)
“ldr r1…” Move a specific 32-bit word** from the exception frame into r1 (**the PC)
“ldr r2…” Put the address of function prvGetRegistersFromStack into r2
“bx r2” Call prvGetRegistersFromStack, r0 has the formal parameter
“handler2_…” store the address of prvGetRegistersFromStack in the object code where “ldr r2…” can get to it (PC-relative addressing).

Interestingly the “ldr r1…” instruction appears to be unnecessary. The prvGetRetRegistersFromStack function doesn’t have a second formal parameter.

To add to the good explanation from Jeff, this assembly is roughly the following code:

/* Bit 2 tells whether main stack (MSP) or process stack (PSP) was in use at the time of exception. */
/* Bit 2 == 0 --> MSP was in use. */
/* Bit 2 == 1 --> PSP was in use. */
if( ( LR & 4 ) == 0 )
{
    prvGetRegistersFromStack( MSP );
}
else
{
    prvGetRegistersFromStack( PSP );
}

Thanks.