result = square(3) + square(4); |
The following naive code does not work:
// result = square(3)
/* -------------------------------------------------
Call square(3)
------------------------------------------------- */
mov r0, #3
bl square
mov r1, r0 // Save 32 in r1 *** FAILED !!!
/* -------------------------------------------------
Call square(4)
------------------------------------------------- */
mov r0, #4
bl square
add r0, r0, r1 // Computes 32+ 42
/* -------------------------------------------------
Store in result
------------------------------------------------- */
movw r1, #:lower16:result
movt r1, #:upper16:result
str r0, [r1]
|
Because: the addition was interrupted by a function call
|
|
Therefore:
|
result = square(3) + square(4); |
The following modified code save register r0 on the program stack and retrieve the value after the function call to continue with the (interrupted) computation:
// result = square(3) /* ------------------------------------------------- Call square(3) ------------------------------------------------- */ mov r0, #3 bl square push {r0} // Save 32 in stack *** !!! /* ------------------------------------------------- Call square(4) ------------------------------------------------- */ mov r0, #4 bl square pop {r1} // Retrieve 32 from stack *** !!! add r0, r0, r1 // Computes 32+ 42 /* ------------------------------------------------- Store in result ------------------------------------------------- */ movw r1, #:lower16:result movt r1, #:upper16:result str r0, [r1] |
Change the program into the above code
How to run the program:
|