We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi guys, I am a new bee in programming for ARM7. I have written the simple following program using for loop to find a factorial of a given number. However to check the result, I have kept the input value static (a = 4), for which factorial should be 24 but somehow the result in the dis assembly window and watch window is not as per expectations. Please help me out. Thank you. The source code is as below:
#include <stdio.h> #include <LPC21xx.h> int main() { int a=4,i,fact=a; // a is user defined though I've taken it as 4 right now, just to check fact value for (i=a;i<=1;i--) { fact = fact*(i-1); // fact should be 24 but at this breakpoint I am getting 4! } }
Don't exit main(), put a while(1) loop at the end, and breakpoint that. Try also turning off optimization.
Dear sir, Even if I put the while(1) at the end and try to break point at that point, compiler shows the value of fact variable 'out of scope'! So I can not monitor the value of fact variable. Kindly show me the way to solve this problem and tell me how can I observe the final value of fact variable. Thank you. Regards, Ankur
Did you also follow the advice about optimisation...?
Magic question.
What's the difference between
int main() { int a=4,i,fact=a; for (i=a; i<=1; i--) { fact = fact*(i-1); } }
and
int main() { int a=4,i,fact=a; for (i=a; i>1; i--) { fact = fact*(i-1); } }
Dear Sir, Thank you for pointing out the logical error. I am feeling ashamed of my self for such a silly mistake! And yes optimization issue wasn't there. Thanks a lot for your help. Regards, Ankur Gajjar