Ask your own question, for FREE!
Mathematics 23 Online
OpenStudy (dan815):

Hey anyone knows how referencing to a subroutine i.e calling a function within a function changes what happens to the addresses on the stack, how the stack pointer is incremented and the new address it stores??

OpenStudy (mimi_x3):

in c?

OpenStudy (dan815):

ya

OpenStudy (mimi_x3):

Pointer arithmetic?

OpenStudy (dan815):

here is code with subroutine and a code that references the subroutine

OpenStudy (dan815):

i want to know how the stackpoints incremented and stuff

OpenStudy (dan815):

#include "io430.h" void Delay(void) //defining the delay subroutine { for(int i=0;i<20000;i++); //loop indicating delay as long as i<20000 { } } int main( void ) //main function { WDTCTL = WDTPW + WDTHOLD; P1DIR = 0x01; while(1) //run infinitely { P1OUT ^= 0x01; //toggle the LED Delay(); //Call the delay function to create delay } }

OpenStudy (mimi_x3):

Im useless Im sorry

OpenStudy (dan815):

Okay this is what a friend of mine said Much like exercise 2, the purpose of this exercise was to examine the program counter, SP, and stack values by creating a simple code in which my LEDs would toggle on and off using subroutines. The only difference is that this time the delay is implemented using a subroutine. Examining some of the commands implemented like CALL and RET was done. The CALL instruction is similar to an unconditional jump JMP instruction [1]. The JMP would stop the current sequence and will command the program to jump to a specified subroutine where as the RET command returns the program to the location of the subroutine. Running the program it was apparent that the program counter is incremented by 2 to obtain the address of the following instruction onto the stack. Which then is pushed onto the stack and again increments the stack pointer by 2. When using the JMP command the stack location was 0x0C046 while the stack pointer was located at 0x03FE. Thus when calling functions within other functions, the stack pointer will decrease and thus the stack pointer increases by that amount. The code below first toggles the LED then calls the function delay, for a for loop of j<30000 that does nothing and then returns back to main so it can start again and so on.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!