1 00:00:00,470 --> 00:00:07,040 Hello, my name is Stephan and welcome to this in-depth exploration of an assembly code example designed 2 00:00:07,040 --> 00:00:11,570 to calculate the sum of numbers from zero to a given value. 3 00:00:11,600 --> 00:00:18,320 This tutorial aims to guide you through the intricate workings of the code, breaking down each instruction 4 00:00:18,320 --> 00:00:25,530 to unveil the underlying mechanisms of register manipulation, loop constructs and interfacing with 5 00:00:25,530 --> 00:00:30,500 the external functions such as values printf here. 6 00:00:30,500 --> 00:00:37,040 So by the end of this lecture, you will have gained a profound understanding of these components, 7 00:00:37,160 --> 00:00:39,950 come together to create a functional program. 8 00:00:39,950 --> 00:00:43,850 And now let's delve into the specifics of the code. 9 00:00:43,940 --> 00:00:48,830 We will first declare the external function declaration. 10 00:00:48,830 --> 00:00:55,550 The External Directive announces the existence of the printf function, which resides externally, implying 11 00:00:56,270 --> 00:01:00,090 that its implementation will be linked later. 12 00:01:00,090 --> 00:01:03,930 So here we will write extern printf. 13 00:01:04,490 --> 00:01:10,550 And after that we will create a section data and this is the data section. 14 00:01:10,550 --> 00:01:15,140 And here we will define a 64 bit quad word. 15 00:01:15,170 --> 00:01:21,080 Here we will number number equal five. 16 00:01:21,110 --> 00:01:25,730 Here we are defining the 64 bit quad word, which is eight bytes. 17 00:01:26,990 --> 00:01:28,870 Uh, let's actually write it down here. 18 00:01:28,880 --> 00:01:30,320 Eight bytes. 19 00:01:33,620 --> 00:01:34,520 Quadword. 20 00:01:35,330 --> 00:01:45,020 We are defining the eight bytes Quadword variable named number with an initial value of five and we 21 00:01:45,020 --> 00:01:45,650 are here. 22 00:01:45,740 --> 00:01:47,390 Fmt db. 23 00:01:48,500 --> 00:01:50,030 We will add a string here. 24 00:01:50,030 --> 00:01:53,840 The sum from zero to. 25 00:01:55,650 --> 00:01:57,020 L d. 26 00:01:58,100 --> 00:01:58,940 Is. 27 00:02:01,130 --> 00:02:03,040 Is old. 28 00:02:03,410 --> 00:02:05,240 And here we will pass ten. 29 00:02:06,320 --> 00:02:06,790 Atlas. 30 00:02:07,870 --> 00:02:10,270 Here, ten and zero. 31 00:02:11,350 --> 00:02:20,130 Here we are defining a null terminated string format suitable for printf, and we have also developed 32 00:02:20,130 --> 00:02:23,160 the section BSS. 33 00:02:23,260 --> 00:02:27,610 This is the section section uninitialized data. 34 00:02:27,910 --> 00:02:34,990 Here currently empty and this section is used to reverse space for variables and this that will be initialized 35 00:02:34,990 --> 00:02:36,580 during program execution. 36 00:02:36,850 --> 00:02:39,970 We will also define the section text. 37 00:02:40,420 --> 00:02:42,460 Section Text. 38 00:02:43,090 --> 00:02:49,870 We will create a global main here and you can define main as the global entry point of the program. 39 00:02:50,260 --> 00:02:52,000 We will create the main. 40 00:02:52,000 --> 00:02:55,270 Now this is a function prologue here. 41 00:02:55,960 --> 00:02:57,760 Posh RVP. 42 00:02:59,110 --> 00:03:01,180 Here and move RVP. 43 00:03:02,900 --> 00:03:05,210 RWP rsp. 44 00:03:07,800 --> 00:03:08,610 Rtsp. 45 00:03:09,930 --> 00:03:20,070 And here in this line of code, we are preserving the value of base pointer rbpp by pushing it on to 46 00:03:20,070 --> 00:03:21,460 the stack. 47 00:03:21,480 --> 00:03:29,010 And here we are setting the base pointer rbpp to the current stack pointer RSP. 48 00:03:29,400 --> 00:03:33,660 And now we will go to initialization path phase here. 49 00:03:33,690 --> 00:03:37,050 So we will use move rc. 50 00:03:39,760 --> 00:03:40,600 Number. 51 00:03:41,290 --> 00:03:42,530 The number we defined here? 52 00:03:42,530 --> 00:03:43,100 Five. 53 00:03:43,780 --> 00:03:44,430 Here. 54 00:03:44,470 --> 00:03:45,280 Here we are. 55 00:03:45,390 --> 00:03:46,210 Load the. 56 00:03:46,480 --> 00:03:54,610 We are telling the assembly to load the value stored in the memory location number into the loop contour 57 00:03:54,640 --> 00:04:01,930 here, which in this case is rc x and we will move racks. 58 00:04:02,840 --> 00:04:03,440 Zero. 59 00:04:03,620 --> 00:04:12,170 We are initialize telling the assembly to initialize the accumulator racks to zero. 60 00:04:13,280 --> 00:04:16,610 And now we will create this loop here. 61 00:04:16,690 --> 00:04:17,480 B Loop. 62 00:04:17,900 --> 00:04:20,600 So this is the loop body. 63 00:04:20,630 --> 00:04:23,240 We will add like this. 64 00:04:23,360 --> 00:04:24,880 We will add Rex. 65 00:04:24,890 --> 00:04:25,280 Actually. 66 00:04:25,280 --> 00:04:25,760 Why? 67 00:04:26,780 --> 00:04:28,310 This here is like this. 68 00:04:32,000 --> 00:04:35,150 So bloop ad rex. 69 00:04:37,090 --> 00:04:40,960 Are racks and ask X. 70 00:04:41,920 --> 00:04:49,780 Here we are telling the assembler to add the value of the looping counter rc x to the accumulator rax 71 00:04:49,780 --> 00:04:50,470 rax. 72 00:04:50,620 --> 00:04:52,960 And now we will loop. 73 00:04:54,490 --> 00:04:55,540 Up here. 74 00:04:56,600 --> 00:05:04,520 Now we will decrement the loop counter arcs and jump back to bloop bloop. 75 00:05:04,520 --> 00:05:13,970 If RC X is not zero and here we can write our loop termination and here. 76 00:05:18,760 --> 00:05:20,870 Move the. 77 00:05:23,320 --> 00:05:26,080 Are the I fmtm here. 78 00:05:26,080 --> 00:05:31,330 Load the address of the format string into RDA or RDA. 79 00:05:34,390 --> 00:05:35,620 Or die here. 80 00:05:35,620 --> 00:05:36,010 Yeah. 81 00:05:36,040 --> 00:05:37,720 MOV RSI. 82 00:05:40,400 --> 00:05:41,210 Number. 83 00:05:41,990 --> 00:05:44,810 There are a lot of the value of number. 84 00:05:45,290 --> 00:05:55,390 This is the sum to be displayed into RSI and move the X to racks. 85 00:05:55,400 --> 00:06:01,200 So load the computer to sum of value from racks into RDX. 86 00:06:01,250 --> 00:06:03,440 Move racks. 87 00:06:05,000 --> 00:06:05,630 Zero. 88 00:06:05,750 --> 00:06:11,030 Clear racks in preparation for the printf call. 89 00:06:11,090 --> 00:06:13,810 Now we will also call the printf here. 90 00:06:13,820 --> 00:06:14,840 Call printf. 91 00:06:14,870 --> 00:06:18,860 We are invoking the printf function to display the formatted output. 92 00:06:18,860 --> 00:06:22,730 And now we will write our function epilogue here. 93 00:06:22,910 --> 00:06:24,650 MOV rsp. 94 00:06:26,230 --> 00:06:31,900 R'p E.r.v are telling the Assembly to restore the stack pointer rsp to its original value. 95 00:06:31,930 --> 00:06:40,090 Cleaning up the stack and we will pop the restore the original value of the base pointer are P and after 96 00:06:40,090 --> 00:06:42,640 that we will return from the main function. 97 00:06:46,650 --> 00:06:47,190 This. 98 00:06:47,280 --> 00:06:55,590 The assembly code showcased here efficiently calculates the summation of numbers starting from zero 99 00:06:55,590 --> 00:06:57,960 up to a specified value. 100 00:06:57,990 --> 00:07:08,190 The key mechanism is the loop construct in which the RC X register serves as a loop counter with each 101 00:07:08,190 --> 00:07:09,300 iteration. 102 00:07:09,330 --> 00:07:21,330 RC x is automatically decremented and the the loop body adds its value to the accumulator racks or racks. 103 00:07:21,330 --> 00:07:30,570 And once the loop concludes, the accumulated sum is presented through the printf function, and a thought 104 00:07:31,200 --> 00:07:38,410 provoking experiment arises by setting number to a notably larger value such as 1 billion. 105 00:07:38,430 --> 00:07:44,160 By doing so, you can observe the program's execution time using the Linux Time command, which we will 106 00:07:44,160 --> 00:07:45,900 do that in the next lecture. 107 00:07:45,930 --> 00:07:49,990 Leading to valuable insights into its performance characteristics. 108 00:07:49,990 --> 00:07:57,130 And this enlightening tutorial empowers you to grasp the intricate interplay of registers, memory and 109 00:07:57,130 --> 00:07:59,530 control flow in assembly language programming. 110 00:07:59,620 --> 00:08:03,670 Fostering a deeper understanding of low level computer operation.