 Hi friends, welcome to the session 13. Let us continue our discussion. Today we are going to be discussing about control flow instructions ok. This is little different from what you have seen so far and this is going to modify the way the program is executing the direction and it might the flow of the program will be modified based on these instructions ok. So, we will see what are the instructions support this and then what are the requirements for this from the higher level language perspective and we will we will see couple of examples to understand how does it function ok. Very good. So, pay attention to this because this is something to do with the program counter because these instructions are going to modify the value of PC which register it is R 15 ok. So, anything that touches this register we need to be careful otherwise we will be taking the program to some other unknown location. So, when you are writing this program using this instruction you because assembler takes care of it most of the thing, but you should be able to understand what is actually happening what the assembler the compiler generates which instructions ok. So, let us see what are the things we need to be careful about ok. Now, this session we will talk about why we need a program control flow, why do we need a control on the program flow and then in specific to a control flow instructions the normal thing what we look at format and the data path what happens in the data path that is inside the processor when this instruction comes into the processor and then we will also look at the timing that the number of cycles that is this instruction takes and then some usages of those instructions ok. Very good why do we need program flow control ok what is the default execution flow of a program you know that this is sub code memory assume ok and PC is currently pointing here. So, you know that the width of this code memory is 32 bits right we are only talking about ARM state of the processor not a thumb mode. So, ARM mode so, in the thumb state the instructions are all 32 bits wide. So, the PC will be keep on increment and incrementing to next locations that is plus 4 and then as soon as this instruction which is in this 4 words come into the processor the processor executes it and then it automatically increment the PC accesses the next instruction and then it this goes on. So, you know the pipeline pretty well. So, whatever PC is feeding the instructions come what is the first stage by now you should know what are these stages are fetch ok this is fetch stage of the pipeline and this is decode ok and this is execute. So, the the PC keeps on incrementing by 4 and then the instructions keep coming into the pipeline and then processor executes it. So, when it is executing a particular instruction in the execute stage it may take one cycle or it may take n cycles based on what kind of instruction you have executed the what is being executed. If it is a LDM or STM instruction which is multiple load or multiple it is going to take more cycles based on the number of registers are answered based on what what is the answer it is going to take more time. So, if this particular stage is blocked it is very natural that these stages are also blocked ok it is like a queue you cannot dump a queue ok. So, you have to wait the instruction need to wait not you the instruction need to wait for the previous instruction which is occupying the stage to be completed. So, this is very clear to you I have explained this multiple times in different scenarios. So, I am just trying to recall all that. So, that you are able to understand what is being discussed in this particular lecture ok good. So, sequential access you all understand now the program counter is incremented by 4 which I mentioned and then it is feeding the pipeline which are the instruction words which are 32 bits wide. Now, apart from the normal sequential show of the program we need a selection and iteration ok these two are different kinds of program programming paradigm or a not a paradigm it is program different operation that a program has ok. When I say program we we are talking about a high level language either C or C plus the whatever language you know. So, these languages support different different instructions ok and you know that you can write a function and then you can call a function suppose this is a function 2 in a higher language in a main ok you are writing your C code then what you do you write this function it will be in another file called 2 dot C maybe and then this is another file which is called main dot C is there you are compiling this file and this file and linking them all together and then building a executable and then running it on a ARM processor for our example this whole thing is running on ARM processor now. Now, you are calling a function foo suppose it is accepting this is written in this file 2 dot C it is accepting an integer as assumed that it is a parameter ok. Now, what happens you will be calling it with some variable in this case j then you will call this. Now, what exactly happening this whole C program has to be compiled and then if it has to be run on ARM processor the compiler will generate some ARM assembly code correct. So, for the now what happens during the function call ok the program comes here the execution now when you are suppose you if I use C plus plus you know we show C plus plus or we we show any of thedevelopment platform if you are single stepping it and then you say that ok I want to execute this and then I want to know what is happening inside the function if you are single stepping it and then going into the then immediately the control will jump to the first instruction in the foo dots foo function ok inside the foo is there function the control goes there then you start seeing that it is getting executed and then when the function ends it comes back and then start executing from the next instruction may be here I plus plus is there it will execute this instruction. So, what exactly is happening in the program in the assembly code if this call of a function ok it is supposed to call a function from this. So, this will be replaced with one set of instruction which we are going to talk about today. So, that instruction it takes the control of the processor to some other location where this program is loaded in the memory. Now, when I say it is taking a control what does it mean actually it is actually taking the PC value normal flow the PC would have got incremented to the instruction which may be a small C code here which could get expanded to a a set of assembly code. So, it would have executed this instruction in a normal flow PC would have got incremented by 4 and then it would have got executed you know it will be executing all the assembly code which was generated by the compiler. But when a foo is called a function is called then the control goes here and then it is going to PC is going to access every assembly instruction which was generated by compiling this C dot file error foo dot C it it would have generated someobj and it would have compiled and then you know combined together and link file would have been created and then that is code is sitting in the memory code memory. So, now we are going to see when you are having such a control flow happening changes happening in the program order the way it is getting executed how is it implemented in the assembly code ok. So, there should be a support by the processor for compiler to generate that code. So, that these kind of things can be done ok. So, finally, whatever C code you write is not going to be executed this is going to what is going to be executed is the set of assembly instructions. So, there should be a support in the assembly code to enable this program flow. So, that the R 15 the PC register is modified accordingly. So, that you get the same feel of what the original programmer intended to do in the C code or C plus plus code what our high level high level language . So, our focus is going to be how this is done. Now tell me you might have seen all this in your high level language. So, if some condition is true execute this otherwise there is no else here. So, it will just fall through that ok. Just to refresh your memory if suppose i is greater than 0 this code what you have written i plus plus ok has to be done. If this is not true then execute whatever is written here in the C code. Similar things should be done in the assembly so, that you have a whatever is intended here is executed by the processor ok. So, it is one form of representing what you are interested in performing and assembly language is the same thing is converted in this language. So, that processor understands what it is supposed to do ok that job of the compiler is to do this. So, now you will not see if or else in the assembly code ok you would not have if instruction in the assembly code. Similar this whatever is seen here is converted into a set of assembly instructions which implement this particular intent ok what the programmer wanted very good. So, may be program there are some constructs like if this condition do this else execute some other set of instructions. So, if this is true only this set of instructions should be executed and this should not be done if this is false then this should not be done only this should be done. So, this kind of conditional statements need to be executed in the assembly. So, what are the support needed for that? So, when I say a set of instruction that means, a small few lines in this some suppose pi lines of C code you have written that would have been transferred to some 50 lines may be I am not saying that it is always plus into 10, but that it is in terms of some n ok. It is certainly it will be more than number of C instructions that you see in the code which in it is converted into assembly there will be a set of you know huge number of instruction. Now, they will be starting from some address 100 and then it will go up to 200 assume. Now, you have you are saying that if this condition is true may be 0 is set ok I want to execute this other way I am I want the this to be executed. Now, how can you do it in assembly you have to put some condition instruction to say that if it is 0 you execute this other way jump to this instruction so, that it falls through this. Now, suppose if else you are put in in this case what happened even do not if you execute this after this you jump here, otherwise come here and then execute it and then come here. So, it could be if else or if then anything needs to be commonly you know communicated to the assembly so, the processor through the assembly language. Now, this you understand and while loop so, you might have seen lots of code written in C or 3 plus plus where it says as long as i is greater than 0 do this set of instruction ok. So, when you see this after this execution what happens you will be incrementing i plus plus or in this case I am saying if this is greater than 0 you do it. So, if the loop has to be terminating so, it should be sorry what I mean by i minus minus you should do ok. So, you are initializing the i with some 10 ok and then as long as this is more than 0 you keep increment you know keep doing this instruction and then it is decremented once in every iteration. So, you do not see that the control coming here and then checking this condition and then calling through it, but when you are converting this particular C code into assembly what will what you may have is some assembly instruction and then incrementing will be replaced by in our case add whatever i is i is you know there or if because it is a minus minus maybe it is a subtract. So, this instruction will be done and then it will check branch if it is equal to ok this may be where this particular instruction is starting level is may be L L 1 suppose if 0 is equal to 0 you do not jump. So, NE ok suppose it is converted into branch NE L 1 what has been i is decremented and then if it is not equal to 0 you come to L 1 otherwise fall through that. So, effectively this Y loop has to be converted into assembly instruction which will perform this job . So, even the same thing is true for work for loop also. So, when you have such instructions in your C code when the languages define such a constructs in the programming language assembly needs to support them ok. So, let us see how they are implemented in our case. So, basically these instructions are PC modifying instructions to support those high level language constructs means support by the assembly language instructions which can change the flow of execution. Now the flow of execution can be changed if assembly instruction modify the PC that is very important PC is what R 2 takes in. Now PC modify the instruction can be of two types one is unconditional what does it mean always you jump to that particular where will you have this kind of unconditional jump when you have in a high high levels you know as construct suppose I will tell youfor for loop you have ok inside that. Now you suppose here you are saying that break ok you have put a break condition a break instruction ok in the C. What does it mean if the control comes here it is supposed to come out of the for loop may be the when it come comes out of the for loop it has to come somewhere here. So, whatever is the instruction which are generated specific to these instructions these high level languages that address should be given for this control to come here that means, this is the branch to this label may be assume that this is L 2 then as the compiler would have generated a code to say branch L 2 if your condition is control is coming here please go to L 2. This is unconditional branch ok it is not checking the break is not checking if is there or inside some condition is not there. So, it is a unconditional branch if there is a if then else it is a conditional branch ok. So, that is what I am calling it as a unconditional or conditional conditional branch always checks some of the facts. So, how can the processor know if I is greater than 0 or less than 0 all those conditions it will do a compare or subtract or add some arithmetic operation it will do and then it will make one more thing you have to remember ok compare if it does anyway the flags are affected automatically. So, you do not have to worry about it, but when you are writing these instructions suppose not you may be a compiler generating it will remember to put the S there ok if you recall these instructions default do not impact the season in these flags. So, the compiler has to take care suppose if it is going to take look at these flags based on this instruction we should put the S there. So, that if this instruction affects the flags and then it takes the based on this flags it will take a branch to some other location. So, this is called conditional branch ok. Let us see now I am not told you what are the instructions that support this. So, let us see this is the instruction ok how do you write it this is B B instead for branch ok. L is optional because it is within the parenthesis if L is mentioned let us come to that later assume that B is the condition if this condition is true may be the branch instruction also can be you can add the you know condition inside and then what will be here it will be a label ok label means it is a assembly label ok. In the assembly instruction a particular instruction will be give associated with some label and that will that address will be computed based on this instruction. Now how does this particular instruction help in computing it ok actually what you are you still remember this is 32 bit wide instruction. So, you cannot have a an absolute jump with a 32 bit address ok it has to be a a reduced number of bits allocated for offset as much as possible they have given 24 bits here ok. Now I will remember this then we will we will put the next slide I mean I will explain you how this offset is modified ok how it is computed we will come to that ok. Some offset is computed who computes this offset it is done by the it based on the compiler or some day whoever is computing it they look at the label and where it is located and based on the address it will generate the offset. It will compute this offset and then generate an instruction by fitting in this offset in this particular bit positions ok. We will see some examples then it will become clear to you. Now let us talk about L L is another indication to say that when you are branching ok when the processor is branching let me give you an example. Suppose B L L 1 is a label ok now L 1 is here ok set of instructions are there. Now if suppose if you are calling in the original C program you are calling a function then what happens when you are calling a function the control should go to the function and then it should return back you remember it should return back to the location which is below the this instruction which actually took the control to the function. I am calling this is a function the L the function is starting at L 1. So, the condition the control should come back to the instruction which is below B L. Now how can it be achieved? See once the PC is modified ok by adding this offset or subtracting this offset from the current PC ok and then the control comes here the processor does not have any means of finding out where where it does not have to come back to ok. It does not know this PC is lost it is overwritten when I say PC is offset is computed and it is control is coming here it is very implicit your it is very obvious that the PC value it is overwritten. So, that register value whatever is holding the address the R 15 is modified. Now what happens to that? This address is overwritten with this address and then it will keep on executing it by incrementing it by 4. Now how can will it know how will it know when where to come back to which address to come back to? So, to inform the processor we need to store that value somewhere we means the processor while executing this instruction you should make sure that the address for to which it is supposed to come back should be stored somewhere so that the control can come back. So, ARM processor supports it with the help of a reserving a register for the purpose which is called a link register why is it called link register because it is linking the function with the call in a scholar and colleague if you have if you are knowing about this somebody is calling another function and then this function is linking back to that caller by storing the value or the address to which it is supposed to come back. So, where will you store because R 15 is modified so it has to be stored in some temporary register. So, the processor does not insist that you should store it only in R 15, but if you are using this instruction it will separately be stored in this location, but you are free to do it by doing using some other registers also ok, but assume you are using a BL instruction then it will automatically save this value into R 14 which value the address to which it is supposed to come back to. Now how will you make sure that it comes back who does that that is also be generated by a code at the end of the function it is of some function some code has to be written which will copy ok this whatever value is there in R 14 into PC ok. So, it cannot come back automatically some you know assembler or a compiler has to generate a code which will copy the R 14 value into PC so that the control comes back. I hope this this is you are able to follow, but I will show you some examples so that it becomes clear to you. So, to explain this I am giving you a background of what is happening in the processor ok. So, if you are using a BL instruction automatically the address of the next instruction is stored in the R 14 and then the control jumps to the wherever the offset points to ok wherever it takes to PC is modified. Let us see an example then it becomes clear to you ok. Let us move over to the next slide. So, branch jumps to offset address given with the instruction if you are mentioned just a branch label ok some label then this instruction just just goes there that means, what PC is modified to point to this. So, in this case the R 14 is not affected please remember only when you say L it will be affected. So, that means, what you are not or the assembler or compiler is aware that the program is not interested in coming back to this next address ok. So, it does not have to bother about where is it jumping to jumping from. So, it is a you know it is going there, but without bothering about whether it has to come back or not. Suppose if the code is like that you have a one way jump, but you are not it is not a call as to a subroutine or a call to a function. So, you do not need to bring the control back to the this address ok. So, in that case what happens BL is not used B only is used and then the control goes there. So, R 14 is not used by this instruction ok please remember that. Now branch with link if you say this then what happens is the old PC value it is not a actual old PC value this is a PC value next to the current BL instruction ok that that is what we are calling it as a old PC value. Suppose BL instruction is at 100 the 104 will be stored in the R 14. So, that when you copy that R 14 into PC the control comes here ok it will not come automatically it has to be brought back by copying R 14 into PC ok very good let us go back ok. So, the PC value written into R 14 is adjusted why why it has to be adjusted what I mean by adjusting let me explain. Again I am bringing back the pipeline by now we should know that, but still let us see I am now found out 100. So, let us see here assume that I have put a BL instruction to L 2 L 2 is what at some 200 ok this is the address 200. Assume that all the addresses what I am writing is all in XR decimal ok. So, that you dot dot is there please remember there is so much of memory I cannot draw my screen is only limited to this size. Now remember the pipeline now see always when you look at any instruction this pipeline should be at your back of your mind ok. What is this? F I will not say what is F and what is D and what is E ok. Now what happens? When will the BL get executed when the instruction is here is it correct? Now when this instruction is here and getting executed that means, what what I mean by getting executed it has the offset which has come along with the instruction whatever is the value ok. It has come along with the offset it could be plus or minus what will this do? It will add this value with the existing PC ok plus or minus offset it will do and then write that value into PC. Now apart from that it is supposed to do one more thing whatever is the address that is 104 should be copied into R 14 correct because I said BL is getting executed. So, it is a branch with a link that means, BL is located here. So, whatever is the offset it is a which will compute the PC to 200 and then it will start executing whatever instruction here. Do not bother about how will it come back we are not worried about that right now. I am only explaining you what will the PC do when it is supposed to store this R 14 value ok sorry the correct value which is supposed to be the instruction which is next to BL into R 14. I said that something has to be adjusted why come back to PC the pipeline ok which instruction will be here whatever instruction here ok x whatever and this is O 0 8 which instruction will be here whichever is at the O 0 8 you agree because when BL is here the next plus 4 will be here it should have got decoded here, but the next to next to instruction is getting first and then it is sitting here. Now, what is the PC value at this moment when this BL is computing this offset adding that offset what is the value of PC is it 100 or 104 or 108 it will be 108 because you just saw that it has fetched this instruction. So, PC will be that only after this cycle only the auto increment in the data part increments the value and then initialize it with the next address after this execute this instruction goes out of the execute state. So,the PC is actually 8 and you are computing some offset to go there, but you are you are likely you are supposed to save this some PC value which is next to this BL instruction that means, it should not save 108 into R 14 you agree. If it says 108 in the R 14 the control will come back here suppose if somebody copies the R 14 into PC the control will come back here, but the control should come here because the instruction next to BL is this. So, it should subtract 4 ok basically adjusting means subtract 4 and then keep this address in R 14 ok and then it should add this offset or subtract this offset and then come to the next location. In this case it is supposed to add that offset and then come and execute this instruction. Now can you realize that when this jump happens what happens though the pipeline previous stages of the pipeline the X and Y are here already, but they are not supposed to be executed now because the BL wants the control to go here. That means, the instruction here maybe you call it as A B that should come here into the pipeline ok. So, what effectively happens is this will be flushed 2 cycles are wasted. Now A will pass out moving into the pipeline first it will come here and then after that A will move here and B will be here after that A will come here and B will come here maybe if there is another instruction C that will be sitting here ok. This is the kind of pipeline happens. So, please remember when you are flushing the pipeline do not think that A can jump here and then start executing skipping these 2 stages. It is not possible until the fetching happens until the decoding happens and the instruction cannot come into the execute stage to get executed. So, there is no shortcut it has to go through this ok different stages it has to go through. So, effectively 2 cycles the 2 instruction which were there will get emptied and then they control flows here. So, this is what I mean by adjusting that means, R 14 should be copied with the address which is next to the BL instruction ok. In this case BL is assumed. So, I am saying because R 14 will be modified only when you use a BL instruction ok good I hope you understood this. Note that the CPSR is not saved with the PC. If you guys are aware of x86 processor ok some of the instructions ok especially when it is an interrupt or you know the call call there is a call assembly language in x86. So, there are some instructions ok in x86 or in some other processors also which will default save some CPSR also if this require ok, but please remember there is no implicit saving of CPSR when there is a branching happens in the ARM processor. So, though you do not think that it will be saving it ok. If you need to keep that you have to save it yourself otherwise you will lose the value what was there in CPSR. Now, R 14 inside the R 14 after this address is saved ok after this is done inside R 14 1 and 0 bit that means, R 14 you know it is a 32 bit register right. So, it will have 0 to 31 bit right. So, 0 and 1 what are these bits they are the lower 2 bits of the address which address program address please remember this is the program address code address. Now, code address changes in a 4 byte bound it is aligned always 4 byte aligned and then it gets incremented by 4 always ok. That means, the address 1000 our favorite 1000 is the first instruction. Next instruction will be at 1004 only can there be any instruction between these 2 addresses can it be 1001, 2 or 3 it cannot be you agree. If if you you get only from 1000 you jump to 4 from 4 you jump 8 means jumping means the addresses change keep incrementing by 4 that means, the lower 2 bits are always 0 correct. If you change this bit position it will be 0 0 0 and then 1004 8 is 1000 C is 1100. So, you see that lower 2 bits are always 0 because it is a 4 byte aligned because of that reason because you are writing something into the R 14 now it always make sure that this 110 are always clear ok because there are 2 bits reserved for that position in the register. So, that will be 0. Now to return from the routine called a branch with a link now I am coming back coming to theplace where how will you come back from the subroutine remember you went to L 2 ok. The you jump now the sorry I am saying U U U you are jumping sorry about that ok. Now BL L 2 is we said it came here the processor control came here it start executing. Now I told that this address next to BL is saved in R 14 ok you also know that it is done. Now wherever this function becomes suppose it should write some code for this control to come here what is that code that is what is given here. You know that PC can be used as a simple R D in move instruction. So, you when this adjustment is already done ok which is pointing to the instruction next to this is very simple that you can come bring the control back to this particular place by just copying R 14 into PC you agree. So, this is what will bring the instruction the control back to this location ok. So, what is given here is the instruction this instruction ok this is how the control comes back to the location which was saved in R 14 ok prior to the BL instruction ok good. Now there is another way also we can do you know by you know you are familiar with the L D M. So, suppose when you were going to another subroutine BL this is a subroutine then you when you know took the control to L 2 you know that there are some instructions here which you have written as a subroutine and then you will be using loss of registers right R 0 R 1 R 2 there are so many registers you are using here, but in the original code in the original flow also you are having instruction which are being used now if the control comes here and then it modifies some of these registers which was mod you know changed here and then you come back by executing this instruction either move or L D M instruction you come back here will you have the values which were before this you will not have it because if you have modified them in this subroutine you call it as a subroutine suppose you are calling a function which is you know which is using some registers. So, in that case what you are supposed to do is you are supposed to save the registers which are you are going to be using in the subroutine and then recover copy back those registers ok just before coming back. Also that you get the same state of those registers that that that was there prior to this real instruction to achieve this you have to use L D M you know you have to use STM here ok store whatever registers you will mention it in the register with ok to stack somewhere in the stack you know that may be R 13 you may be using as a stack pointer or any other register you put them into the stack suppose it is going downwards you keep on putting them ok and then when you are done with your function you will be generating a writing an L D M to say copy back the same set of registers. So, that the stack comes back here and then and then you do a L D M PC that may in this PC what will you do is you will write R 14 ok. That means, the PC value is copied the whatever is there the stack you know stack you will be saying that which is actually stored in R 14 because suppose you R 14 is saved here ok this register that R 14 you will be copying into PC. So, that you come back to this location. So, the PC is restored by using a a stack instructionthat means L D M ok because you are saving that R 14 in the stack. Otherwise you may not have not saved it then you are to take care that R 14 is not disturbed in this function ok that is very important then only this one will be over written. So, please remember whenever you are writing assembly code you are supposed to take care of the instruction we are register contents if you are expecting it to be maintaining some continuity in your program ok. Then so, we have taken more time here I hope this is all clear to you. So, that is the next few things we can go faster. So, offset is computed now offset is 24 bit I said that 24 bit only is possible for the ARM instruction to reserve it has reserved all the 24 bit. Now, it is shifted by left 2 bits why see when it is saved it is saved suppose you are supposed to you know that the bottom 2 bits are always 0 right I mentioned to you that this 1 and 0 the sorry bit 1 and 0 of the address is always 0 inside. So, you do not have to save this 2 bits while computing the offset what you do is suppose whatever address you will get by adding this offset that offset itself you save it such a way that only the higher 24 bit is saved and the lower 2 bits are recovered by shifting it left ok. So, what exactly happens is you get a 26 bit wide offset actually because it is all 4 bit aligned. So, you 4 byte aligned. So, you very clear you are aware that this 2 bits are anyway going to be 0. So, you can accommodate more number of offset values if you save only this part of the offset ok. Anyway this will be 0 because all the addresses are always 4 byte aligned. So, that is why you see that it is when you compute when you get the offset from the instruction the processor when it gets the offset value it actually shifts that by 2 bits. So, so you remember that when the assembler is putting it inside it should shift right and then store it and then the processor will internally shift it left by 2 bits and then recover it ok. So, that way you are 2 bits are saved so that more offset can be given. Now you may wonder why this offset will be greater as per as far as possible why I will explain you. Suppose this is where your program BL is there ok currently you are executing this you are willing to come back to this with the code instead of saving this, but from here how far you can jump how far how far you can make the processor ok minus offset and plus offset. How far you can make it go decides about ok how much instruction you know how big the instruction could be in the core memory and how far you can reach them. So, the processor will try to accommodate as much as possible. So, it has done is the no ARM processor they have accommodated 24 bits and then they have also given you another 2 bits of advantage. So, maximum 26 bits of offset can be stored not 32 bit of offset. If 32 bit of offset is there the whole range could be covered remember if you see the whole code you know code memory including data and code the whole thing is 32 bit wide right. That means, plus or minus suppose you read in the middle plus or minus 32 bit in the middle if it is here it should jump even more than that right. So, if it has to jump the whole memory area which is available it will need at least 32 bits or even maybe 33 will make it you know come here, but you are limited with the 24 bit and then additional 2 bits. So, 26 bit offset. So, you this processor can jump only 32 memorabits plus or minus 32 memorabits you I want you to verify how by saving and now having an offset with the bit of 26 bits ok plus or minus 26 you have only 26 bits to represent the offset in this complement form. How can you get a 32 bit bit of megabit of offset ok. So, it can go for or as far as possible that is the maximum is plus here minus 32 by megabit and this is plus 32 megabit in the code memory it can jump the control can go. So, that is what is done here by this particular instruction. So, the offset is taken and it is plus or minus is done. Now, how will it know whether it is plus or minus there is no bit any the inverse code here to say it is a plus or minus this offset itself is stored in 2s complement form please remember. So, what does the processor do it left shifts it by 2 bits right that you I told you maximum it will accommodate 26 bits it will become then it will sign extend please remember sign extend if it is name it will be all 1 if it is MSB 0 or MSB 0 it will sign extend it up to 32 bit and then now it has got a maximum 2 or not 2s complement offset value which will be added with PC whichever is the current PC to compute the new PC value and that will be written back into PC again ok please remember this is what is happening when the assembler or the compiler are generated to the offset this is what the sequence of operation it will be a shift left by 2 bits that means, this will be 0 and then now you will get a 26 you know this 26 bit value you will get and then you do a sign extension to 32 bit. So, that any operation is in the processor is 32 bit wide. So, this offset is also made 32 bit wide a signed value ok 2s complement value and then it is added with the current PC value and then the control goes to P then PC is modified then control naturally goes to this location new location ok very good. Now, I am sure you will be very clear now this is what I mentioned to you ok. So, this I have already told you PC will be towards ahead of the current instruction this I explained to you already. Now, suppose if the you are going to have big code and then it has to jump to beyond this 32 megabyte what it is supposed to do is it should use a previously loaded register maybe it could have written the offset into some place memory ok the 32 bit value whole 32 bit value and then it can do a load if you remember pseudo instruction I said LDR ADR I mentioned right ADR in the last session. So, address calculation this ADR instruction what does it do it will compute the address. So, it could be in another memory location also that whole 32 bit offset can be copied and then added with the PC ok. So, if it is not possible to know if the jump is further from 32 megabyte it has to be done by previously loaded value into a register or a written a memory copy and then modify the PC accordingly ok good. So, these are all not to be done by us by the tool, but still you should be knowing what actually happens ok. Now, it is a interesting part of the instruction ok how does the BL get executed. Now by now I think based on whatever I explained you should be able to clearly understand PC is coming through the A bus ok PC value which is the current PC value and then offset is coming from here why should it come from here it is not sitting anywhere in the register ok. Please remember offset was with the instruction which was decoded fetched and decoded and then passed through a pipeline register. If you remember between the stages there are pipeline register which carry the information about the particular instruction. So, it is carrying it is passing on the offset value which was read by the instruction along with the instruction that is coming through this back oriented. It comes to the barrel shifter, automatically it knows if it is exhibiting BL it is supposed to do a left shift logical shift left ok by 2 2 bits because you know that 24 bit has to be made into 26 bit ok. Originally 24 bit offset is only written into the instruction. So, 26 bit sign extended inside automatically the sign extended value will be going into this. Now it is just added why it is just added the positive or negative is based on the offset value ok B will be either positive or negative. So, you have to just add along with the PC value to get the new address where the control has to be going and it is written into address bus. Now tell me what is this address this is the new that B 1 or L 1 or L 2 whatever I mentioned that address is written into this which goes into the address bus to fetch the instruction in a new instruction got it. I hope this is clear to you. Now let us come next cycle in the next cycle I if suppose it is a BL instruction please remember if it is a BL only this job will be done. If it is B this this particular cycle will not be there. So, I am explaining for BL I put both here, but please remember this is only meant for BL why copying into R 14 happens only with the BL instruction branch with the link register. So, what is it supposed to put it I said adjusted if you remember that adjustment is not done here it is done in the next cycle, but whatever is the current PC value what is this PC value? Let me come back again to the example L 2 ok assume that this instruction is at 100 and L 2 is at a 200 if you remember that is what I said. Now the 200 the new address has gone here ok please remember 200 has gone to the address register and the new instruction is getting fetched. During that time the next cycle even after the one instruction the new location is fetched. Next the cycle still the PC is still fetched with the value which is actually 108 because it is plus 8 right it was it was actually fetching this instruction right. So, 108 is there in this register PC it is still not modified with a 200. So, this 108 comes here and goes with gets written into R 14 ok the same value is written there is no adjustment possible here because you know you see that it is equal to a what is written into is whatever comes on this bus what will come on this bus whatever is written R 15 R 15 is right now 108. So, 108 first gets written into R 14 ok while the instruction gets incremented by 4 and the next instruction gets accessed. Here 204 goes out remember originally here 200 when in the next cycle 204 goes that instruction is getting fetched, but still your that R PC is still R 14 which is getting entered into R 14 now. Now you may wonder who will do the adjustment ok. Let me explain you in the third cycle ok remember in the third cycle after this cycle is done in the third cycle what will happen is this R 14 will come here and then this will be programmed the i l u will be programmed to minus 4 ok it will do minus 4 and then it should write back that value R 14 will become which one 104 is not it correct 104 will be written into R 14 okit is not PC ok please remember in the third cycle it will be R 14 will be written copied and then written back into R 14 ok same R 14 comes here which is 108 subtract by 4 minus 4 and then 104 you write back into R 14. At the same time in the third cycle what will happen this incremented value will get written into PC. So, PC would have by now modified to the new location 208 or whatever it would have got changed, but this now R 14 preserves the old PC value ok that is what I said PC is also updated with the new 200 that 200 plus may be 204 or 208 based on you know which cycle we are in that will get written into PC because that can be written thoroughly because PC has a separate read and write port and then the rest of the registers are to read write port. So, it can happen in the same cycle so that is what happens. So, I hope this data path is clear to you now this will be very very obvious ok this will be sequential cycle this is also a sequential cycle because it is next instruction only, but will this be sequential though the same fetching is happening the new instruction is getting fetched from a new other location. So, it has to be a M please remember ok and then afterwards it will all be S. Now how many cycles that branch with link or branch it takes it takes this many cycles which are what are they 2 S and 1 M 2 S and 1 M is the number of cycles it takes. So, I hope you will be able to compute this on your own ok. Now let me ask you to take a 5 minutes break not to go out of the class just to look at this instruction discuss with your neighbor and then try to understand what is happening here ok. I have given some comments also so that you would not be totally surprised with what is happening please spend some time understand this ok. Let us get back you canrevent it after a 5 minutes. So, 5 minutes break ok welcome back I think I do not have to explain now because all of you would have understood it just for the continuity sake let me tell you this is what just a branch does its affect R 14 no R 14 is not touched by this instruction where does it jump to this location. Now I will leave it to yourexperimentation ok what will be the value of offset here if you if you if you can guess the offset value then only I will say that you understood this only ok. Though it is not your job a similarcompiler will take care of it, but knowing this is better. So, when a whether it is a simple branch or a branch L the offset is same ok please remember whether it is just a branch or branch with link register the offset whatever is computed is same way which is depends on the the label where it is going, but based on L is there are not this next instruction whatever is the next instruction gets saved into R 14 ok please remember that clearly. So, in these two instruction one is a just a branch another one is branch with link. So, what is the offset I will explain you may be explain one here you can compute the offset here and then you can feed the same set of instruction in your simulator and then try to under see what happens there ok. So, tell me what is the offset the process the assembler is supposed to put in the instruction the PC a current PC is 1008 which that is the instruction where the B is there getting executed, but during this time when this branch is in the execute state where does PC 0.2 it points to 1010 where this particular label points to fortunately or unfortunately it is to the same location. Please see that I have written B sub here and then written this code. So, it is nothing but I mean these two ok this is the this is the address this B sub is pointing to. So, effectively whatever is the incremented PC value is where the particular jump is supposed to happen here. So, when the compiler or assembler generate this instruction it will make sure that the offset value is 0 because it knows that PC would have been already pointing here because PC plus 8 is this and your jump is also happens to the branch is also happening to the same location. So, PC need not have to be modified it just has to be added with the 0, but you have to mention some offset. So, it can it can also mention the offset as 0 which will actually correctly point to the location where it is supposed to go. Now tell me what happens to this when this BL is done immediately it comes here and then it executes another BL where it saves this address in the R 14 ok R 14 register will have the value exactly 1014 it will have ok. I want you to run this code in your simulator and check every content ok, but the control will come here first ok. When it comes here you can check R 14 is modified to this value or not ok. Now what is the offset it will be compute the PC will be here actually plus 8. So, it has to add this 4 and another 4. So, plus 8 has to be added. So, what will be the offset stored here will it be plus 8 or will it be plus 2 please think it over. If you recall what I mentioned offset is not the exact offset it is the right shifted value 2 bit to the 2 bits ok. So, originally offset was computed to be 100, but it will shift it right shifted by 2 bits to become make it just 10 which happens to be 8 2 these are all all 0s right. So, offset will be stored as 2 in this location offset will be 2. Now how will you come back to proper value? You know that the processor shifts it to the left by 2 bits. So, that it will get back that value originally and then it will add it with the PC. So, that it will come back to this location ok understand it it is very important. Now tell me this will be executed this will be executed then what happens here R 14 I told this value is there. So, it is copied into PC. So, control comes back to this instruction it will get executed after it then this is the software interrupts we will talk about this instruction later on this this takes them control back to the processor. So, this is what happens in this ok. If you understand this and if you can find out what is the values of offset computer you are an expert in branch instructions ok good. Now branch with exchange ok what is this normally when you say exchange means we are exchanging values ok. I exchange money with you means I may be you are exchanging a money with a shop keeper then what you do you give money to the person you get some item back right that is what is called exchange. But here this exchange does not mean that it is exchange means it is not two way it is it is a one way actually what is that mean suppose I have written a code instead of B X or B B or B L I said B X R 0 ok. So, what is actually means is the PC whatever is the PC R to P ok is copied with the value which is there in R 0 R 0 is what one of the registers R 0 to R 14 can be used for this purpose ok. Please remember R you cannot write a R 15 here you cannot say I want to exchange with my PC with my PC ok it is illogical this is not allowed ok. Other than R 15 you can say that I am interested in exchanging the value what is in R some registers with the PC see effectively this could be done with a MOE also ok MOE PC comma R 0 also does the same thing right. Here the B X code has some more intricacies to that I will explain to you. So, it is little different from MOE ok it is not just as moving ok it has got some other meaning also, but I will I will explain that B X ok. Now B X does this job it is exactly copies the value in R 0 ok. Now does it copy everything? No does it. It copies branch causes a ok first of all let me explain you what does the branch causes if the pipeline pressure happens and then a new instruction gets fetched all it is same it is same as what I explained to you earlier ok. This also takes the same number of cycles originally as B and BL ok just hold on I will tell you the difference as I explain this R 15 is used as an operand in the if it is you are trying to say B X come B X please remember it is only one single operand ok you cannot write B X R 15 ok and this also permits the instruction I said permits the instruction set to be exchanged what I mean the instruction instruction set exchange. I need to again remind you of two modes though I have to touch up on the thumb mode here ok, but this instruction cannot be explained without bringing thatthing to you ok. At least for a moment and assume that arm mode is what whatever we have seen so far thumb mode is something little different which means that it actually assumes that 16 bit wide instructions are saved in the memory ok in the thumb mode. Whenever the processor is running in thumb mode what we mean by that is I told you earlier also this time we we calling that it just accesses 16 bit at a time ok it could read 32 bit and then internallyorganize it, but actually every instruction is only 16 bit wide please understand that is the different mode. Now how will the processor is communicated either whether it is accessing a 32 bit value or 16 bit value based on a thumb mode bit in the CPSR register. So, actually this B X instruction is used for informing the processor that ok going forward you are going to get 16 bit instruction from the memory. So, you are supposed to interpret whatever you read from the memory are 16 bit wide instructions not 32 bit wide ok that is what you are informing the processor. So, how is it done? It is done by setting one bit in the register ok I let me come back. Now I said that B X instruction can exchange with some register I am taking this example as r 0 what exactly happens assume that this is at 100 ok and r 0 is having 200 our favoritesubroutine ok. Now assume for a moment it is 200 then what happen this 200 gets written into r 15 ok r 15 is r 0 is moved into r 15. So, what happens the control flows goes to 200. Now during that time what you can do optionally is in the rm 0 bit what I mean by rm 0 bit if suppose you are using r 0 there will be LSB right for the register assume the r 0 is there LSB bit is there. You know in a in a typical ARM instruction there 2 bits last 2 bits are always 0 correct because it is a 4 byte aligned, but you are intentionally making this bit 1 suppose you are doing or maybe the assembler or compiler is doing. Now what the processor thinks when it receives r 0 and then it sees that LSB bit is set then it will know ok. Now the programmer is intended interested in moving back to thumb mode moving back or moving to ok it does not matter from ARM mode to thumb mode that means, what is going forward what are instructions you encounter from their memory please read it as 16 bit wide instructions internally process it as a 16 bit ok you run it in thumb mode it is a different mode of a processor ok, but only thing it has to be communicated to the processor that it is going to be a 16 bit wide instructions. So, that 1 will indicate the processor that it is a 16 bit mode it is jumping to. So, it will start reading from 200 202 204 ok you see the difference 200 202 204 because it is a 16 bit 2 bytes. So, 200 and 201 will correspond to 1 instruction 222 203 will be another instruction like that it will be interpreting it. So, that is what is communicated by setting the lower LSB bit of the R ok I am sorry this should have also been R ok very good. So, BX is used to switch between ARM and thumb mode ok thumb mode will be discussed later in detail ok do not worry about that. Now, let me ask you one quiz take 2 minutes break read through this come back will come backyou can find out how many people have different answers ok. So, you can see a distribution of the class how many having these answers I am sure all all answers would have been picked up by you or may be only one hopefully hopefully that is correct one ok this is a why just come back though this bit 1 is set ok it is ignore inside the processor other than setting the mode to thumb mode P mode the instruction will fetch from leaving the lower LSB bit why because if the all the thumb mode instructions are all 2 byte byte. So, the similar to ARM mode in ARM mode 2 lower bits are 0 ok in ARM mode 2 lower bits are 0 or always because it is a 4 byte boundary in thumb mode lower bit is always the LSB is always 0. So, the instruction address is only the remaining 31 bits please remember that ok. So, what is the remaining 31 bit even if you expand it it will not be any of them right it cannot be this because I said you have to go to 1000 only. So, it cannot be 1004 it cannot be this it cannot be this it has to be this one ok it will just though you are doing 1001 only the LSB bit is made 0 and then remaining value is copied into PC. So, PC will move to 1000 ok I hope you are able to understand this I have explained a lot about this. So, these are the 2 byte boundary addresses and these are the 4 byte boundary so, if it has to be jumping to 1000 it has to be this address not this one or any of this ok good let us go again we have a very interestingexample you can even take 10 minutes if you want please do not break away this is the last slide of this class. So, finish this and enjoy your day take a break come back ok. Welcome back I hope you are able to understand what happens here let me explain this is very trivial ok I have put some instructions just to keep the processor busy and keep your brain also busy. So, that you know it is not just instructions of our interest and just keeping some instructionsit does not have any meaning other than wasting the psychics of the processor. What is happening here this is very important you should not miss out this move you can ignore these moves, but you cannot ignore this because is the very interesting register is there. So, R 15 gets copied into R 0 tell me what is the value of R 15 and what is what gets stored in R 0 you know currently it is executing this instruction R PC will be here. So, actually this address please remember R 0 gets this ok that is what I am saying here. Now, the next point of interest is this one what happens here R 0 is added with the R 0 and R 4 and return back into R 0 very straight forward. So, it was having 1 0 1 0 please remember R 0 is not modified anywhere here. So, it is safe in fact, whatever you saved here is there which gets added to 4 by 4. So, 1 0 1 0 gets become 1 0 1 4. Now, what happens here B X R 0 now currently what I was saying 1 0 1 4. So, after this instruction where will it jump it will come here correct because R 0 was holding 1 0 1 4. Now this will be executed now again it is going to execute this at this moment 1 0 1 4 was there which will get added to 1 0 4. So, it becomes 1 0 1 8 correct ok. Now, when this B X is in executed the value of R 0 is modified it is changed now. So, it is it is not going to jump back to this location like what it did will it come to this location agree it will come here. Now, what happens here again is getting added by 4 it becomes 1 0 1 C please remember it is all in X R decimal. So, what is 1 0 1 C its own instruction correct. So, when this is executed will it go anywhere else will it go anywhere to this area or this area or will it stay here it will stay here. So, what happens in the processor is is very funny, butit is interesting ok B X gets executed here ok and then at the end of the B X R 15 is loaded with the instruction 1 0 1 C. So, it will clutch these instructions whatever it has access and then it will load again the B X instruction ok. So, it will come here again it will it will do the same thing 1 0 1 C is written into R 15. So, this processor is now locked onto this it will never move to any other location forever it will be here agree if you do not agree read again understand this if you do not understand this go back to previous lecture because this is the crux of what you learnt in this lecture ok. In fact, it corresponds to something you learnt earlier also because you are saying add and other instruction. So, I am sure you will understand this please run the same code in your simulator seeing is believing do not trust me I may have given something wrong here please try it out try out the same set of instructions in your code and experiment it understand it then you will be a expert in assembly programming all the very best ok that is what is done there. Now, one more small example which I have already shown earlier. So, ADR instruction during this time the last session I explained ADR means you will copy it will load the address of this particular label with 1 ok additional 1 is added to R 0 ok where is this label the label happens to be here ok this label is here. So, assemble compute the address and then add one to that and then push it in R 0 ok. Now, when this BX is done the control comes here at the same time because you have the R 0 bit 0 is 1 it moves to thumb mode. So, it will execute all this as 16 bit instruction ok and then it will execute a BX R 5these are all thumb mode instructions, but these are similar to ARM instruction only R 5 is copied with back to ARM back to ARM is does not have a bit 0 as 1 it is 0 here. So, once this bit is 0 it will know that this address is bringing back to ARM mode. So, it will start fetching the instructions from here as 4 byte values ok it will interpret them as ARM instruction with a bit right. So, basically you know I will come back to this again when I am explaining thumb mode, but I am saying that BX is used to change the instruction mode of the processor instruction set of the processor it changes from here it was ARM mode it went to thumb mode and then it came back to ARM mode ok by executing this BX instruction by properly modifying the bit 0 of the register. Remember this could be any register as long as that bit particular bit is, but register is loaded with the address of the instruction this will work. So, I hope this is clear to you even if you not understood fully do not bother when we talk about thumb mode it will become much clearer, but it is part of the BX instructions I thought I will explain you how that mode changes happen using these instructions ok. Very good we have come to the end of the session today in this class we talked about program flow why it is required, why high level languages have a why loop and for loop and if then else and how assembly can support those kind of control flow and what are the timing data part we saw in detail and then we saw a few examples and you have tried out also some program as an exercise. So, and we saw how this helps in ARM to thumb mode H A you know switch switching between modes ok. Thank you very all of you for your attention I hope this was very useful this is very interesting part of the discussion. So, we will discuss again come back again with a some more interesting stuff on the ARM processor. Thank you very much for your time have a nice day bye bye.