 So, I have explained what is this function do I ok. So, this is the main function as a main suggest, but I have turned it in assembly. I have loaded the address of num 1 and then using that address I am actually getting the value into r 0 and this value is got into r 1 by first initially loading the address of the variable variable is declared and then moving the value address. Now, once the values are moving to r 0 and r 1 which happen to be num 1 and num 2, these two are the parameters to this function ok. So, as per the ARM procedure called our first parameter will be in r 0 and then second parameter will be in r 1. If more parameters are to be passed maximum 4 we can pass through registers r 2 and then r 3 and if there are more than 4 parameters then the rest of the remaining parameters will go into the stack ok. The calling function will push it into the stack and then call the function. Now, because we have only two parameters they are passed through registers. So, stack is not used for passing the parameter now and then branch with a link my address is called my address return in C which has been compiled using metadaptics tool and I will be showing you the assembly of this. So, after that suppose you assume that this is executed and it returns that it will first what it is doing is it is taking the address of variable is into r 4 and then the value in r 0 is copied there. So, it is assumed that this function on returning the value return value is kept in r 0. So, that is also a part of the procedure call. So, the C compiler which is compiling this particular function follows the ARM procedure call and based on that I have written this assembly code and I am getting the answer back. So, I suggest all the students to try out this particular piece of code about this attribute in C and then compile it and then generate an assembly dot s 5 ok and then you can this s file and this can be multiple s files can be assembly files can be loaded into the simulator. So, load both then run this. So, what will happen this will call this function and then return back and then you will get the answer. So, the result will be in back in r 0 ok. So, if you are watching this to vary all three variables you will be seeing the sum of this. So, you can even change this number as you wish and then try to see whether it is what you are getting here is the addition of these two number ok. Let us now. So, hope this is clear to you. So, this main function is calling this routine which is written which was written in C and now we will see the assembly output of this and then how this is followed we can combine these two. So, this is the C function which I showed in the last slide this is the generated output. So, I have just taken a assembly listing of the combined output ok. OBJ file will have the binary of this ok the binary encoded values of this instruction, but you can always ask for a output file which is an assembly output ok instead of because we will not be able to understand OBJ. So, we can get a assembly output file from the compiler to see whether the instructions what has been generated by the compiler matches with what we understand this function is doing ok. So, this all these instructions are generated by the compiler looking at this definitely you know C function. Now, let us understand what is happening here. So, first thing when you get into a function called the stack how it is managed is what is more important ok. So, assume first of all as I mentioned the stack is always assumed to be fully and descending ok. So, an R 13 is used for the stack pointer ok. Now, full descending means it is pointing at something which is already filled with some value. Now, inside the function if anything needs to be pushed into the stack first the stack has to be decrymented ok. So, this is the high address and this is the low address we do not have to worry about what is like that stack address value ok R 13 what is it already we are sure that this is initialized ok. So, we do not have to worry about the actual value, but only thing is relative value whatever is the value that R 13 has with respect to that how the stack is going to be managed by this function ok. Now, first thing what it is doing the compiler has generated all these codes. So, this is the first thing is that as soon as the BL my add was done ok. Let me link you from that assembly file to this then this was executed there was some instruction after this ok. Let us call it as some x y x y z ok some instruction is there with adding at this address. Now, after when this particular VX branch is done with respect to LR link register it is supposed to go back to the address next to this BL instance before this function was called. So, there is this address store it is stored default in R 14 ok. Who does it the processor pushes this address it computes this address after this instruction whatever is address it is pushed into the R 14 that means, it is the R 14 is filled with that value and then this instruction is executed. Now, R 15 is modified to point to this instruction. So, by this time R 14 is already loaded with the instruction next to this VX ok where from where it is supposed to come back on executing a VX instruction ok. Now, as soon as we come into the my add the first instruction is SPR SPR is the store instruction storing the value of SP. Here one more thing what we should understand is SP is a frame pointer ok. Frame pointer is the in register which is R 11 in arm state ok normally and if it is a is a is going to a arm state R 11 is a frame pointer R 7 is the frame pointer in thumb state because thumb state does not have the visibility of higher registers. So, R 0 to R 7 only is visible at thumb state. So, R 7 is assumed to be a thumb state frame pointer. So, when a compiler is generating a thumb instruction and it knows whether it is generating for arm state or thumb state because we will put code or code 16 or 32 before the particular C file or C function or whatever or it could be some other program or pragma is a thumb instruction after the compiler. So, see you know it has to generate whether a arm instruction or a thumb instruction. So, based on that it will generate and accordingly it may you know allocate result SP as R 7 in thumb state and R 11 in arm state for ok no problem this SP is a R 11. Now, R 11 is stored in the location ok this is what this is the post index or sorry pre index because it is the pattern minus 4 before indexing into this. That means, the whatever is the value of SP SP is actually SP is a stack pointer which is always a stack pointer which always R 13 ok. So, R 13 is pointing at thumb under a each first decrement by 4 that means, what it is pointing at the next entry location decrementing. So, grab the stack is going downward always full descending it is full. So, it decremented anything to be pushed before then it has to be decremented. Now, after decrementing it is supposed to fill some value which value it is filling it is filling whatever is the SP it is pushed into this. So, SP is what it is in arm state you know so R 11 is pushed into this ok. Now, what happened after that the exclamation mark is there that means, the R 13 is also now updated after accessing and then storing this value into the R 13 address. So, SP is added subtracted by 4. So, SP whatever was R 13 that value was subtracted by 4 and then in that location R 11 was put where SP is R 11 and then because exclamation mark there the SP is updated now. So, SP is now pointing at the new address which is pointing at R 11 wherever R 11 is stored. Now, why is the R 11 stored it is going to be modified ok. This right now only has 0 is there that means, it is not adding anything more to the SP value otherwise it will generate a fixed location ok it will not generate it in the stack it will be reserving some space. So, whatever is the SP value it will add something to that so that you know frame pointer moves further. So, inside the function frame pointer only is considered to be a for access me a stack. So, in this case in our particular case there is no requirement of adding more space in the stack it is only adding 0 ok. Otherwise based on the code that we have written it may choose something a different constant based on the local variables or other requirement of stack it may do it. Now what happens? Now this is SP is added with the 0 and then it is loaded into SP. Now in our case it is same as R 13 now SP is also pointing at the same location correct because SP is not modified after this addition. So, SP is also having the same value. Now SP is now SP is pointing now you have to remember SP is pointing it is same location where R 13 was pointing ok. Let me erase it and the properly ok. So, SP is also pointing at the same location where R 11 is your store and then in a this is done ok and then subtract SP with from SP 20 value. That means, this R 13 whatever is there is subtracted by 20 20 is what decimal value. So, let me so 5 into 4 if each is 4 bytes the stack is 4 byte byte. So, 5 into 4 only is 20. So, because this is 4 byte 5 times the value ok it is going down in the stack. So, 1 2 3 4 5. So, stack now is pointing at this location ok R 13 stack pointer sorry stack pointer is pointing at this location and this is also can be used to fill energy. Only it is done this is done for that this function what is the my add function how much space it wants in the stack it first grabs that space. What is the requirement? Because if any interrupt happens interrupt may the interrupt also may run on the same context then it may have to use this stack pointer or even this function is calling another function then also there will be a stack usage by the called function. So, the depth is undefined. So, before anything is done with the stack it should first the function which is now getting executed it takes care of for local variable it needs some space and even to keep the return value it needs some stack. So, it first gets the stack result for itself. Now, how is it using them? So, to use it it is accessing it with respect to SP. So, once a R 13 is moved here after any instruction ok using the 30 R 13 will be pushing the value here R for the down. So, this is not going to be touched by anybody because R 13 has been already have the advanced downwards because stack is going down it is a it is advanced downwards. So, no issues. So, these what are we doing with this? Now, let us see what the compiler is trying to do? It is doing minus 16 it is storing the value in R 0 to this. So, from SP where SP is finding minus 16 is there that means it will point to this location ok. Here it is storing R 0 value let me change the color ok it is storing R 0. What is there in R 0? As I told you R 0 has argument 1 R go up ok and R 1 will have argument 2. Now, this function knows it is expecting two arguments because it is been compiled with the two functions a parameter. So, it knows that R 0 is having a parameter. So, it shows that R 0 here. So, you may wonder why is it you know you can do with R 0 itself you can continue to operate on R 0 why it has to make use of the stack because there may be so many instruction which may be needing that value and then the R 0 needs and may be used for some other purpose that is one reason. Another reason is suppose in this function is calling another function then that called function will assume that this R 0 to R 4 is for it to process some parameter R 0 to R 3 correct. So, if this function is calling another function it needs to be free. So, it is better to store it in the stack. So, that this value is preserved even when the internally if this function is calling another function also no issue because you you have already you know having those values in the stack. So, you are not dependent on the value in R 0. So, R 0 is pushed here by this instruction R 1 is pushed into minus 20 minus 20 is here ok R 1. So, R go on R 2 1 and R 2 both values have come here ok and then it is load LDR now again this value is copied. So, 16 is copied R 2 and from this. So, actually it is writing into this and then copying from here. So, you may see that it is not optimized code, but it is you know unless you mention some optimization the compiler is you know not free to do any optimization. So, it it does according to whatever you want because that is why I will be able to explain you what is happening ok. So, I am not put any optimization. So, it is not doing any optimization. So, the compiler has now it is putting the value back into R 2 that means, we are getting in R 2 now we are having the value which was passed in R 0 correct which was passed in R 0 is moved into R 2. Similarly R 3 is in this instruction this is done this instruction is taking the value which was in R 1. So, you see it has copied this R 0 into stack and then taking it from the stack. So, you are effectively get the two parameters here R 2 and R 3 and then it is adding R 2 plus R 3 into R 3. So, you may wonder how can it use R 3 because it is overwriting the value of R 2 and R 3. It is free to use R 2 and R 3 because we know as per the AAPF R 0 to R 3 are some parameters are passed into the function and if the function called the function is free to use it. So, the caller will not expect this where registers to be at undisturbed ok. It is it is actually to be used by the caller that called the function. So, it is free to use R 2 and R 3 also no problem. So, that is why it is trying to add this value and putting it into R 3 itself it is not using R 4 place among all that. So, it is putting into the R 3 itself this is done. So, addition of this job the R 1 and R 2 is added now, but it does not want to result ok. It is lying in R 3 some temporary register. Now, R 3 is stored into again stack fp minus 8 which is minus 8 ok this is minus 4 this is minus 8. So, result is coming here ok. The result variable which is declared as a local variable is actually . So, it is copying the value R 3 into that after doing immediately it is again taking it back LDR from there to R 3 ok. We may not see some optimization that is fine. So, now it is getting the value in R 3 back again the result is now in R 3 this is done this is done. Now R 0 R 3 is moving to R 0 why because it is a return. So, this this instruction is specific to this return value. So, when we are returning a variable parameter back to the called function it is supposed to be in R 0 to R 1 ok. Single value means it is R 0 double value is means it is 54 bit value means R 0 and R 1. So, R 0 is the what is going to carry the return value if you remember that we recall previous function we were taking the R 0 from there value from R 0 and then copying into answer. So, R 0 is now having the result now remember it actually initially came in with the parameter now going back with the result ok. So, R 0 is playing a dual role it brings a parameter R 1 it has brought bringing in and then now result return result it is taking it back ok while going out from the function. So, R 0 is now loaded with the result value because R 3 is holding I hope this is clear to you now what happens is it is let me erase around this. So, that now you understood so far up to this instruction now what is happening here it is doing the same thing that was done here. So, in case if it was some you know 4 or 8 or 12 the same will be done here, but here because it is this compiler is generating the code for engineering purpose. So, this 0 will be the same constant is used here. So, it actually the SP is getting back ok the same value ok because SP was decremented here it was decremented here, but prior to that SP was loaded with the value between which it started off ok after this minus 4. So, now SP is added to 0 and then getting that SP SP is now coming back ok it was here minus 20 was done now SP is coming back to this looking and I think it here for that you understand it SP is coming back to this looking original or that in here this after coming in it saves the SP value and then it has come here ok. So, it was decremented by 20. So, SP was changed to prepare keep in SP prepared for any other function calls to be used now if any function call comes SP can be or that in can be updated. Now since we are going to go back the SP is returned to the same location where it was pointing at originally SP was loaded here. So, the SP comes back to this location and then now what is happening is whatever SP minus 4 was done where the SP was loaded into the stack now that SP is taken ok. So, whatever is pointing at SP is pointing at is stored into SP now this value which was R 11 original value which was there in R 11 goes back from the stack. So, R 11 gets its old value and then because 4 is there it will be here plus 4 is there. So, it will be added. So, SP will get added. So, it will come it will become like this this value before calling what was this value before coming in what was the value it comes go back goes back to that value after this instruction which you remember that after this instruction SP goes back to the value which was it was having prior to entering here ok. So, as soon as it came it decremented by 4 and then SP was loaded now the SP is copied back SP is restored up to now what happens let me erase everything ok or maybe let me not erase everything let me again tell you what is happening here ok. So, R 11 is restored now R 13 is also restored by this instruction ok, because SP gets back this old value and SP is also updated. So, this is SP because it gets back from the stack and stack pointer is again decremented by 4 here. So, it incremented by 4 here because decremented by 4. So, it gets back this old value. So, if SP and SP are getting back their value R 0 is loaded with the result. So, that is what is expected and you do not have to worry about result ok. Now R 1 to R 3 we do not have to bother because it is supposed to be disturbed by you know corrupted by the calling routine no issues, but it does not disturb any other value. See if you remember if you can see that it has not changed any other value R 4 to anything it is not it does not this function has not touched any of them. So, it is saved now this is executed now what happens is R 14 is also was not disturbed ok in this it was not saved anywhere. So, R 14 will be having still the instruction where it is supposed to go back the address of the instruction where it is supposed to go back. So, this will take it back to that mechanism because R 14 is not disturbed it is not modified. So, over written by any of the instructions here. So, R 14 is saved. So, it will take back the program to the called routine means after this call routine where the next instruction is important. So, this is the flow. So, now you may wonder whether the stack whatever it has pushed where it gone once R 13 is updated the stack is released ok. It is actually just releasing the stack because when next any instruction wants to use the stack pointer it will use from this address only. So, know it will overwrite on this location. So, this function uses stack up to this point and then it will release it. You may wonder there were some gaps that is a specific to some implementation of the compiler. So, in this particular case it was not loose it is not actually filled with any value, but based on the parameters or based on the functionality which is implemented in this function it could change ok. The compiler behavior might be different ok. So, effectively if you can see this this whole flow it will be able to under appreciate ok. I hope this is clear to you. My intent was to explain you how the parameter passing happen and how the return value is coming from the function. If you understand these two you will be in a position because the exact implementation will be different from different compiler, but it will stick to this AAPS convention ok. It will always assume the parameters to be in this register and then it has to return in R 1. These things should be consistent across, but internally how it is using where it is putting these no parameters and how it is keeping the variables you know allocating the place in the stack. There are little bit of freedom is there for the compiler to innovate ok. But the correctness is assured by all the compiler ok. So, I have used this code then this version I have used to generate this particular code. So, I suggest I will give these references so that you will also be able to generate this code and then verify for yourself and then you can write some different functions and then look at the output generated so that you can you know map a C file with the assembly generated. If you can follow what is being done by the compiler in terms of assembly instruction you have learnt the language as well as the architecture very well and you will be able to debug later on in case if you encounter any problem in the deep you know it will be in any embedded system that you have built ok. So, that is it we have come to an end of this session and thank you very much for your attention I hope this was very useful to you. So, with this we have stopped you know stopping the discussion on tools and environment develop an environment and I will talk to you in the next session about some of the different topics and actually I would like to cover something on linker ok and then with that we will be ending the discussion on the tools ok. Thank you very much for your attention have a nice day. Bye bye