1 00:00:00,770 --> 00:00:02,560 Hello and welcome back. 2 00:00:02,660 --> 00:00:09,470 In today's lesson we are going to look at subroutines which are also known as functions. 3 00:00:09,470 --> 00:00:16,280 So go ahead and download the file 0 6 subroutines functions it and put it in your fascinating project 4 00:00:16,280 --> 00:00:19,390 folder and then zip it 5 00:00:24,580 --> 00:00:35,890 and it is easy for now you have a new folder 0 6 subroutines functions so many and you receive the four 6 00:00:35,890 --> 00:00:37,380 of us here. 7 00:00:37,420 --> 00:00:46,900 One is the add two names are some file and another file had two nuns three functions. 8 00:00:46,910 --> 00:00:53,410 That means this file is run he's written not using any functions but this code. 9 00:00:53,600 --> 00:00:55,480 This program is written refactoring. 10 00:00:55,490 --> 00:00:59,730 So let's take a look at the first one first. 11 00:01:00,560 --> 00:01:07,480 So this program prompts I use it to enter two numbers and then then prints the result. 12 00:01:07,550 --> 00:01:10,580 So let's run it click on run compile 13 00:01:14,970 --> 00:01:19,530 and let's go through this coded after compiling it has this file. 14 00:01:20,030 --> 00:01:20,800 Yes he has 15 00:01:26,440 --> 00:01:29,610 time out at least the directory and you should have this far. 16 00:01:30,310 --> 00:01:31,250 So let's run it now 17 00:01:34,720 --> 00:01:35,830 a promise. 18 00:01:35,930 --> 00:01:36,920 Enter first number. 19 00:01:37,010 --> 00:01:40,110 So we enter three presenter. 20 00:01:40,160 --> 00:01:42,570 You can enter any number. 21 00:01:43,400 --> 00:01:45,620 Then it prompts you to enter the second number. 22 00:01:45,860 --> 00:01:47,310 Let's enter five. 23 00:01:47,910 --> 00:01:54,520 Again you can enter any number you like PRESENTER Any prints result eight. 24 00:01:54,590 --> 00:01:59,780 So it took the number three he took the string three and convert it into number. 25 00:02:00,760 --> 00:02:07,000 And then a two then the the string five and converted it into a number and added it to number to produce 26 00:02:07,030 --> 00:02:07,360 eight. 27 00:02:08,500 --> 00:02:13,660 So let's see how it works. 28 00:02:13,780 --> 00:02:17,030 So we have a few strings here. 29 00:02:17,140 --> 00:02:22,820 The first two string into num one is two probably four the the first number. 30 00:02:23,050 --> 00:02:29,890 The second string enter number two is a promise to enter the second number and the third string message 31 00:02:29,940 --> 00:02:39,220 is to output a result and some is in teacher 0 number. 32 00:02:39,320 --> 00:02:44,090 It is used to store the result of adding the two numbers together. 33 00:02:44,390 --> 00:02:52,930 And it is initially set to zero so here you have one two three four five. 34 00:02:53,100 --> 00:02:55,040 We have five numbers. 35 00:02:55,320 --> 00:03:05,700 Didi missing data double giving this data bytes so to go with here are numbers. 36 00:03:05,700 --> 00:03:07,620 So you're declaring five numbers. 37 00:03:08,940 --> 00:03:19,530 So the first two number is for storing the input handle used by the API function and vice red is to 38 00:03:19,860 --> 00:03:24,570 keep track of how many invites have been read by the Windows API can refile. 39 00:03:26,070 --> 00:03:34,790 And then number one is the star the number that has been converted from the string so when it enters 40 00:03:34,800 --> 00:03:38,400 a string he will be converted to number and start here. 41 00:03:38,400 --> 00:03:46,730 Same thing with the two input string is the string that is being read for example three he'd be starting 42 00:03:46,750 --> 00:03:51,400 his input string first before it is converted to a number and start in number 1 43 00:03:54,160 --> 00:04:01,780 and in also our starts the second number as a string before it is converted into a number and start 44 00:04:01,780 --> 00:04:04,110 in number two. 45 00:04:04,420 --> 00:04:07,540 You program starts you ask. 46 00:04:07,570 --> 00:04:12,570 Easy to enter a number by printing that from and then on one. 47 00:04:12,760 --> 00:04:15,820 So here in the number of print Enter first number 48 00:04:18,810 --> 00:04:23,360 then over here you've written a number issues enters. 49 00:04:23,370 --> 00:04:31,740 So these are type 3 which is a string and the string is three start in this terrible input string thereafter 50 00:04:31,740 --> 00:04:37,740 you use a string to all you have a function to convert the string into a number. 51 00:04:37,740 --> 00:04:46,270 And this 10 and a B starting he has to register in order here we shift and he s really set value in 52 00:04:46,890 --> 00:04:48,280 a variable call them or. 53 00:04:48,320 --> 00:04:56,150 So number one we store the number three then it is a user would then be prompted and enter a number 54 00:04:57,370 --> 00:05:01,020 so these types five finally this tiny string. 55 00:05:02,440 --> 00:05:04,860 And again he would be converted into a number. 56 00:05:05,230 --> 00:05:13,480 So file become with a number and start in E X NBER this number into another variable call. 57 00:05:13,560 --> 00:05:14,980 And the two. 58 00:05:14,980 --> 00:05:16,390 So number two in our stores. 59 00:05:16,470 --> 00:05:18,170 Five. 60 00:05:18,280 --> 00:05:19,590 And number one stars. 61 00:05:19,600 --> 00:05:22,980 Three how we are going to add two numbers. 62 00:05:23,100 --> 00:05:23,820 B 0. 63 00:05:23,860 --> 00:05:31,070 He asks because we are going to use the s to sum up the numbers and we had number one two idiots and 64 00:05:31,070 --> 00:05:33,140 stark idiots. 65 00:05:33,530 --> 00:05:43,220 So he number one starts the very tree and he s is zero initially so triple zero is tree and trees there 66 00:05:43,220 --> 00:05:51,910 is a tree start in he s so it s not becomes tree then we take the number two is contains no Fi and added 67 00:05:51,910 --> 00:06:03,100 2 Three he reaches edicts so 5 plus three is it and the result it is starting back into ADX then we 68 00:06:03,610 --> 00:06:14,060 soon copy some idea into another will cause some so it is an dies and it is not being saved into a sum 69 00:06:14,060 --> 00:06:25,010 so somehow becomes 8 and now finally we print the result how by putting some in to the stack and push 70 00:06:25,010 --> 00:06:27,230 up a message and then copying it. 71 00:06:28,130 --> 00:06:30,400 So these two are the two parameters to print f. 72 00:06:31,070 --> 00:06:37,620 So our whole message contains this string and a format specify. 73 00:06:38,500 --> 00:06:47,880 So percentage these is used to print out numbers and he will be replaced with some. 74 00:06:48,140 --> 00:06:51,120 So we get the results result hit. 75 00:06:51,800 --> 00:06:58,820 So as you can see this program is quite quite long because we are repeating a few things over and over. 76 00:06:58,940 --> 00:07:11,280 For example here we repeat repeating this here read the number converting to string. 77 00:07:11,280 --> 00:07:14,350 So it has a better way to do this. 78 00:07:14,400 --> 00:07:16,770 We don't want to keep repeating things over and over. 79 00:07:16,830 --> 00:07:25,760 So what we can do is we can take this and convert it into a function subroutine and improved version 80 00:07:25,760 --> 00:07:28,150 is over here. 81 00:07:28,200 --> 00:07:35,110 So in this second equation how police on the left hand is in the right. 82 00:07:35,130 --> 00:07:42,710 We have taken we have taken this out and put it into a function. 83 00:07:43,110 --> 00:07:44,430 So this is a 84 00:07:47,440 --> 00:07:58,340 if motion is from here the function get num starts here and function going on here and we give it a 85 00:07:58,340 --> 00:07:59,270 label. 86 00:07:59,270 --> 00:08:07,910 So it is the name of the function or the subroutine and you can see now this part here may be read from 87 00:08:07,910 --> 00:08:17,120 the console is found over here and this by here we convert string to number is fun. 88 00:08:17,370 --> 00:08:23,120 Or here and then we have something new here called written. 89 00:08:23,540 --> 00:08:32,810 So the function runs from here and then you finishes executing a return you return to the caller now 90 00:08:32,810 --> 00:08:36,230 Cody shorter instead of writing this. 91 00:08:36,410 --> 00:08:39,520 Now we just have a call the new function here. 92 00:08:39,770 --> 00:08:43,840 So one line here replaces all these lines here. 93 00:08:44,330 --> 00:08:52,550 So we call get num he would jump to their function executed and then when he hits the written instruction 94 00:08:52,570 --> 00:08:59,660 he would go back to the next instruction after the call which is move. 95 00:08:59,860 --> 00:09:04,430 So this function when he finishes a restored the result. 96 00:09:04,470 --> 00:09:13,420 In each case that means he has to contain the number the last functioning the insight needs and get 97 00:09:13,420 --> 00:09:17,800 numb is to convert the string into a number and that number is tiny. 98 00:09:17,880 --> 00:09:25,500 So many returned from this file Hinkle we will now start that he checks into the variable number one 99 00:09:26,530 --> 00:09:29,790 just like what we do in our weekend program. 100 00:09:30,910 --> 00:09:37,240 After that you probably use a two in there a second number and then you will call again. 101 00:09:37,420 --> 00:09:44,000 Canon once the function returns he will start the value in a second variable. 102 00:09:44,000 --> 00:09:45,290 Number two. 103 00:09:45,290 --> 00:09:54,770 Now notice that once you finish with the second call to the function and is starting the result of the 104 00:09:54,770 --> 00:10:03,530 second number in number two you are now ready to go to the next part of the instruction which you know 105 00:10:03,540 --> 00:10:05,860 in Django where all this function. 106 00:10:05,970 --> 00:10:12,920 So we do that by using a gem in jump up with Riza who jumped all the way past this function. 107 00:10:12,990 --> 00:10:21,720 Come down here and over here you add the two numbers together so you will first initialize the X to 108 00:10:21,720 --> 00:10:31,140 0 and then you add the number one mediates at number two tedious and then move the S into the variable 109 00:10:31,140 --> 00:10:33,640 sum and finally print the result. 110 00:10:34,680 --> 00:10:36,490 So now code is shorter. 111 00:10:36,570 --> 00:10:44,650 We don't have to call these few lines twice so our code each other as you'd see in the original code 112 00:10:45,490 --> 00:10:47,510 you find these two lines here. 113 00:10:47,560 --> 00:10:59,060 These two functions here and using being repeated again down here but oh here we replace those with 114 00:11:00,150 --> 00:11:05,800 one com and here is another call. 115 00:11:06,090 --> 00:11:07,700 So these two costs make it short. 116 00:11:10,590 --> 00:11:14,600 So another code is neater a shorter OK. 117 00:11:14,650 --> 00:11:15,730 This is not a new thing here. 118 00:11:17,380 --> 00:11:17,960 Notice. 119 00:11:18,050 --> 00:11:21,660 Instead of fashion I added these two lines. 120 00:11:21,790 --> 00:11:30,110 EVP and movie here speed EVP and here I also added this one I probably maybe now when you're writing 121 00:11:30,110 --> 00:11:33,280 your own functions you should always do this. 122 00:11:33,310 --> 00:11:40,070 This is called The Function Prolog and this is called The Function epilogue. 123 00:11:40,070 --> 00:11:47,280 The reason why we do this is because we want to preserve the register EVP why do we need to preserve 124 00:11:47,290 --> 00:11:53,040 EBV because GBP is used as a pointer to the stack frame. 125 00:11:53,040 --> 00:11:58,980 What is a stack frame as time frame is uh part of a stack. 126 00:11:58,990 --> 00:12:08,630 We started the main program and any of the CHA programs we are Cha functions which are running twice 127 00:12:08,650 --> 00:12:10,560 under when you first start this program. 128 00:12:10,620 --> 00:12:17,700 This whole program start from the start entry point so the entry point you will be pushed to the stack 129 00:12:18,060 --> 00:12:25,270 by the operating system and the entry point will have its own base address which you start in EVP. 130 00:12:25,830 --> 00:12:36,080 So when the main program calls in and a function you will then move to execute the function. 131 00:12:36,080 --> 00:12:43,700 But when you go to execute in the subroutine or function the central theme function might call up EVP 132 00:12:43,820 --> 00:12:46,170 but maybe using it inside is. 133 00:12:47,180 --> 00:12:57,860 So to maintain the original values in EVP the CHA function will always push the original EVP value to 134 00:12:57,860 --> 00:13:04,310 the stack to preserve its original value and then we'll pop it back to restore the order value when 135 00:13:04,310 --> 00:13:05,000 it finishes. 136 00:13:05,840 --> 00:13:07,240 So that is why we need to do this. 137 00:13:08,350 --> 00:13:10,320 And what is the reason for this line. 138 00:13:11,500 --> 00:13:14,270 So this line is optional. 139 00:13:14,260 --> 00:13:24,300 Now why we use this is because we want to use ESB in site here so ESB can be used in say a child function 140 00:13:25,130 --> 00:13:28,920 or subroutine when you want to create local variables. 141 00:13:29,220 --> 00:13:32,620 But in this case here you are not creating any local variables. 142 00:13:32,790 --> 00:13:38,340 So for now you can use a notice since you are not creating any local variables but it is good practice 143 00:13:38,340 --> 00:13:40,970 to have a habit of writing this as well. 144 00:13:41,670 --> 00:13:48,070 So whenever you write your own user defined function they should add these two lines plus just. 145 00:13:49,420 --> 00:13:58,620 Now if you recall in the early lessons on unpacking we also saw that we need to keep a lookout for EVP 146 00:13:59,100 --> 00:14:01,740 push EVP and probably be. 147 00:14:02,340 --> 00:14:10,210 So now you understand why we need to write this when we are creating our own user defined function. 148 00:14:10,320 --> 00:14:18,180 So if you forgot about the unpacking go and revise the attacking chapter since they we discuss why we 149 00:14:18,180 --> 00:14:27,210 need to keep track of pushing VPN properly so that's it for this program let's take a look at the second 150 00:14:27,210 --> 00:14:28,740 program. 151 00:14:28,740 --> 00:14:33,520 The second program is a new program which we studied before. 152 00:14:33,680 --> 00:14:39,390 Compiling and now we have a look see. 153 00:14:39,530 --> 00:14:40,250 So let's run it. 154 00:14:40,400 --> 00:14:46,960 Let me clear the screen first and then run the loop and then no. 155 00:14:47,420 --> 00:14:52,730 And then as we enter 5 he prints one up to the number that we enter. 156 00:14:53,660 --> 00:14:55,900 So we did this before in the previous lesson. 157 00:14:55,910 --> 00:14:58,310 So I'm not going to go through it again. 158 00:14:59,380 --> 00:15:03,130 So remember this look. 159 00:15:03,760 --> 00:15:08,350 Now this look is quite long because the implement the print f inside here 160 00:15:11,340 --> 00:15:16,760 so this makes it longer to make the court more neater and more efficient. 161 00:15:16,860 --> 00:15:21,360 We can factor this up and put it in a separate function. 162 00:15:21,360 --> 00:15:28,170 So let's take a look at the function version of this program looks a function. 163 00:15:28,560 --> 00:15:29,220 Here you see 164 00:15:34,910 --> 00:15:43,630 now we have uh call call call to print Current um print Konami search function we should be fine here. 165 00:15:43,700 --> 00:15:47,280 So notice now he said of these uh four lines. 166 00:15:47,310 --> 00:15:55,360 He replaces the one single line one single line and the effect out and this into a separate function. 167 00:15:55,430 --> 00:16:04,050 And notice here again we have this function Prolog and the function epilogue and the return keyword. 168 00:16:05,120 --> 00:16:12,310 So whenever these look runs when he comes to call he will call this function will and print a number 169 00:16:13,250 --> 00:16:15,040 and then you return. 170 00:16:15,040 --> 00:16:19,460 And if he shows so when he returns he always returns to the line after the call. 171 00:16:20,660 --> 00:16:28,280 So this is how the written was right let's run this nice comparison runs to make sure it works. 172 00:16:30,920 --> 00:16:40,790 So now let's try the version where it functions and the number is a 6. 173 00:16:41,450 --> 00:16:42,040 And it works. 174 00:16:42,950 --> 00:16:50,860 So okay so now you know how to create your own functions and subroutines to try to convert any program 175 00:16:50,860 --> 00:16:57,470 to one issue land before in to another version using functions. 176 00:16:57,470 --> 00:16:59,270 So that's all for this lesson. 177 00:16:59,270 --> 00:17:00,160 Thank you very much. 178 00:17:00,160 --> 00:17:01,100 House here the next one.