|
The square returns the square in register r0
result = square(3); |
The assembler code is:
// result = square(3) /* ------------------------------------------------- Call square(3) ------------------------------------------------- */ mov r0, #3 bl square /* ------------------------------------------------- Store in result ------------------------------------------------- */ movw r1, #:lower16:result movt r1, #:upper16:result str r0, [r1] |
You can see for yourself in this demo that it works.
How to run the program:
|
result = square(3) + square(4); |
Can you tell what's wrong with this naive code:
// result = square(3) /* ------------------------------------------------- Call square(3) ------------------------------------------------- */ mov r0, #3 bl square mov r1, r0 // Save 32 in r1 /* ------------------------------------------------- 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] |
Answer:
|
In fact, I have written the square subroutine that it will zero out every register in ARM.
I will change the reg-problem.s in class and run this demo to show you that this program does not compute 32+ 42
We cannot solve this problem right now.... you have not learn enough techniques to do so.
I will revisit this problem after I have discussed how to use the program stack to store parameters and local variables
|
This is always the case when you perform a computation that contain multiple function calls.
|
result = square(3) + square(4); ^^^^^^^^^ ^^^^^^^^^ 2 function calls in this expression |
the computation of the expression is always interrupted by a function call.
This is because when you computes an expression that contains multiple function calls, you must:
|
As I said, we can't solve this problem yet.
We must first learn how to use the program stack to store things first.
If you can't wait, the webpage that explain how to solve this problem is: click here
|
|
We will discuss this next.