1
00:00:00,210 --> 00:00:07,110
Before we conclude this module of the complete cortex an embedded developer course there are three more

2
00:00:07,110 --> 00:00:14,340
topics we need to cover in this lesson we're going to take a look at how we can call our assembly code

3
00:00:14,580 --> 00:00:22,850
from a C source code good programmers take advantage of the high execution code of assembly by mixing

4
00:00:22,930 --> 00:00:28,450
assembly code and C code in order to increase performance of their programs.

5
00:00:28,450 --> 00:00:31,140
So we're going to take a look at how to do here.

6
00:00:31,450 --> 00:00:38,440
As always I'll create a new for the hair to store our project and I'll call it call.

7
00:00:38,510 --> 00:00:47,380
Call it call from seed to indicate that the assembly is going to be called from C and I'll just name

8
00:00:47,380 --> 00:00:51,430
the project call from C as well.

9
00:00:51,560 --> 00:00:51,900
Yeah.

10
00:00:51,910 --> 00:00:58,810
This name is to sort of tell the function of this particular project.

11
00:00:58,810 --> 00:01:09,060
And as you know our board is to inform C 1 2 3 0 8 6 p.m. So we select our board.

12
00:01:09,730 --> 00:01:14,370
And over here we have the call.

13
00:01:14,860 --> 00:01:19,800
And on the device we have this startup like we always do.

14
00:01:19,850 --> 00:01:21,170
So we're here.

15
00:01:21,430 --> 00:01:24,870
So let's set the targets options debugger.

16
00:01:24,900 --> 00:01:33,290
As you know we're using distilleries I CDI and under target.

17
00:01:33,630 --> 00:01:38,970
It's the 16 megahertz that's the frequency of our board.

18
00:01:39,110 --> 00:01:46,350
So yeah we're going to create two files on like most of our programs that we have just one file which

19
00:01:46,350 --> 00:01:48,850
is that dot s file the assembly file.

20
00:01:48,850 --> 00:01:55,980
Here we're going to have one dot c file and one as far the dot c file is where the program would start

21
00:01:56,070 --> 00:02:01,230
and then did dot s file to contain the functions that we used to run the program.

22
00:02:01,500 --> 00:02:07,660
So let's look at it pushed up by create and let's create this first file.

23
00:02:07,770 --> 00:02:09,470
I'm going to call this far.

24
00:02:09,660 --> 00:02:19,320
I'm going to say that US functions as a function dot dot s to indicate that would still function in

25
00:02:19,320 --> 00:02:26,670
this file and then call the function in the c file and then I'm going to create another file like this.

26
00:02:26,710 --> 00:02:33,840
And the control s name it made don't see it so let's add the files to our program.

27
00:02:34,020 --> 00:02:37,620
We have the dot c and we come here.

28
00:02:37,800 --> 00:02:38,850
Sure all files.

29
00:02:38,850 --> 00:02:40,930
And then we can find it dot S..

30
00:02:41,200 --> 00:02:45,530
So now let's type all the code.

31
00:02:46,110 --> 00:02:49,080
Let's stop with the Assembly side.

32
00:02:49,320 --> 00:02:55,080
We open all functions till s file and then here we write our assembly code.

33
00:02:55,080 --> 00:03:03,100
We're going to stop by as always using the assembly directives.

34
00:03:03,100 --> 00:03:05,650
I hope you remember these directives.

35
00:03:05,670 --> 00:03:18,120
This is the code area that is read only and we outline to then we write in our code in time form the

36
00:03:18,120 --> 00:03:23,450
instructions here and we're going to create a function.

37
00:03:23,590 --> 00:03:25,140
Then we're going to call.

38
00:03:25,170 --> 00:03:26,080
Let's call it.

39
00:03:27,090 --> 00:03:29,090
Let's call this function.

40
00:03:29,200 --> 00:03:29,850
No.

41
00:03:31,020 --> 00:03:38,070
So basically this the function and we're going to call this function and c we call it number the export

42
00:03:38,100 --> 00:03:46,200
keyword as you know makes our function callable outside this assembly file and this text also enables

43
00:03:46,320 --> 00:03:49,800
us to use our code here in C.

44
00:03:49,800 --> 00:03:55,760
So we've put the the right keywords in place to make it usable in C.

45
00:03:56,130 --> 00:03:58,700
So let's write all the content of our function.

46
00:03:58,710 --> 00:04:09,870
So we stopped by given the label which is that which is number n let's say we're storing just a number

47
00:04:09,880 --> 00:04:12,040
one let's say.

48
00:04:12,270 --> 00:04:13,500
What should a function do.

49
00:04:13,500 --> 00:04:16,980
Let's say the function would display a number as simple as that.

50
00:04:16,980 --> 00:04:20,900
When the function is called It's just displayed a number that is stored in it.

51
00:04:21,210 --> 00:04:30,450
So let's say this move are riskier and let's move a random number 1 to 1 move this number there and

52
00:04:30,450 --> 00:04:36,020
then we store this number in our series like this

53
00:04:38,910 --> 00:04:46,380
and we return from the function this very basic example but this would make it very easy for you to

54
00:04:46,380 --> 00:04:46,740
go.

55
00:04:48,240 --> 00:04:52,770
So basically we have 1 2 1 stored in our shoe.

56
00:04:52,830 --> 00:05:00,780
The reason why it's stored in Irish euro is because the return value of a function is stored in the

57
00:05:00,780 --> 00:05:03,290
Irish you register in assembly.

58
00:05:03,360 --> 00:05:09,860
If we had to store this in a different register say out to it wouldn't return the number.

59
00:05:10,320 --> 00:05:19,500
So we store it in Irish here and also I should explain this before we go on as per the the a pieces

60
00:05:19,710 --> 00:05:24,890
convention which is a set of conventions tenderness how to use the.

61
00:05:25,080 --> 00:05:29,400
The instruction sets off the cortex and architecture.

62
00:05:29,400 --> 00:05:35,390
We can use just registers are a zero hour one hour two hour three an hour twelve.

63
00:05:35,430 --> 00:05:43,020
If we want to use registers are for an hour eleven we must push that current value onto the stock and

64
00:05:43,020 --> 00:05:48,090
then pop these values out before we return from them.

65
00:05:48,210 --> 00:05:50,560
We wouldn't talk about stocks here.

66
00:05:50,700 --> 00:05:56,850
I think it's important to understand this bit of assembly before stock but I'll give I'll provide a

67
00:05:56,850 --> 00:06:01,260
link for more reading on the cortex and dot com websites.

68
00:06:01,320 --> 00:06:09,330
Both stocks this really straightforward article there which a colleague of my root so I'll function

69
00:06:09,420 --> 00:06:13,190
in assembly is complete very basic.

70
00:06:13,190 --> 00:06:17,530
So let's take a look at the sea side of the code.

71
00:06:18,210 --> 00:06:23,770
So as we know we toward a number in the function the function is supposed to return a number.

72
00:06:24,210 --> 00:06:28,380
So we know that return type is an integer which is you.

73
00:06:28,500 --> 00:06:29,320
That's a two.

74
00:06:30,450 --> 00:06:35,790
So what we're doing basically now is rights in the function prototype in the c file and the name of

75
00:06:35,790 --> 00:06:43,770
the function here is no remember this name should be the same as the name we exported from assembly

76
00:06:44,400 --> 00:06:51,660
and it takes a forward acumen to no arguments to the reason why we have this here is because we've not

77
00:06:51,660 --> 00:06:57,690
included the STV int library which is part of the C ninety nine standard.

78
00:06:57,690 --> 00:07:06,240
So what we have to do is include STV and the age and we have it disappear.

79
00:07:06,240 --> 00:07:07,940
So we have our function.

80
00:07:07,950 --> 00:07:22,340
We've caught it now let's go into our main main open close and let's see.

81
00:07:22,410 --> 00:07:28,290
So let's create a global variable here and assign the return of the function to that variable and see

82
00:07:28,290 --> 00:07:33,620
whether it's the same us or we have just went fogging purposes.

83
00:07:33,690 --> 00:07:37,140
Okay let's call it fun you know this just

84
00:07:40,530 --> 00:07:42,410
so we'll call a number here.

85
00:07:42,580 --> 00:07:48,980
As you can see we have not written in a function here we just call in the function and we assign it

86
00:07:48,980 --> 00:07:49,590
to return.

87
00:07:49,590 --> 00:07:55,160
Finally we assign its return value to this global variable we've created code number.

88
00:07:55,570 --> 00:08:06,880
So basically this function x is an assembly and we want to display it in C so the C this one here wouldn't

89
00:08:06,890 --> 00:08:13,770
have any visual output in terms of LCD display on a microcontroller.

90
00:08:14,030 --> 00:08:22,220
So in order to see this we have to go to the debug view and see our COBOL variable and see whether indeed

91
00:08:22,580 --> 00:08:28,780
it is assigned one to one as our function says over here.

92
00:08:28,880 --> 00:08:36,380
So let's do the first you compile and then you connect your board.

93
00:08:36,440 --> 00:08:46,500
So once how port is connected with download onto the port okay and then we'll reset the port.

94
00:08:47,040 --> 00:08:55,050
Now to verify whether our function works or if indeed we are able to access it from C we click here

95
00:08:55,050 --> 00:08:57,230
to go to the debug view.

96
00:08:57,720 --> 00:09:01,590
Okay we'll have that report for you now.

97
00:09:02,340 --> 00:09:03,330
So basically

98
00:09:05,910 --> 00:09:11,710
what you want is to highlight this right click and displayed.

99
00:09:11,870 --> 00:09:19,430
And what is known as a watch window so you select here watch one window to basically we'll be watching

100
00:09:19,430 --> 00:09:29,600
the changes that occur to this variable here and to display in decimal format you can uncheck this hexadecimal

101
00:09:29,600 --> 00:09:31,950
display like this.

102
00:09:31,970 --> 00:09:40,910
So now what we have to do is use this step controls to go through it line step by step to see whether

103
00:09:41,060 --> 00:09:50,210
indeed the value changes and if the value is 1 to 1 which means indeed once one is shown India is shown

104
00:09:50,240 --> 00:09:52,760
in C anyway.

105
00:09:52,850 --> 00:09:56,990
If you are new to this debug view if you're not very conversant with it.

106
00:09:56,990 --> 00:10:02,720
There is not of course known as the cortex and in internals hours which explains this and goes deeply

107
00:10:02,720 --> 00:10:10,840
about what happens inside the machine code and inside the memory and registers when we run in cortex.

108
00:10:10,880 --> 00:10:17,030
Applications you can take that quick course as well if you want to get more familiar with this but for

109
00:10:17,030 --> 00:10:23,390
this course you don't need any familiarity all you need is to know these things and change for this

110
00:10:23,390 --> 00:10:23,890
view.

111
00:10:23,900 --> 00:10:26,560
So that being said let's continue.

112
00:10:26,570 --> 00:10:29,320
So let's step what we have to do is click here.

113
00:10:29,340 --> 00:10:36,610
Step over and as you can see this cropped mean in the next line to execute will be this line.

114
00:10:36,650 --> 00:10:37,770
Line 6.

115
00:10:38,060 --> 00:10:39,770
So let's step over again

116
00:10:44,650 --> 00:10:51,110
and as you can see after executing this we have 1 to 1 that is value.

117
00:10:51,160 --> 00:10:53,920
So 1 2 1 is indeed inside value.

118
00:10:53,920 --> 00:11:02,800
So this show star were able to access the function that we wrote in assembly and see in fact to prove

119
00:11:02,800 --> 00:11:03,800
this further.

120
00:11:03,940 --> 00:11:12,300
Let's use the value let's use that value for our computation and see let's create a not a global treble

121
00:11:12,340 --> 00:11:22,220
here you Infante to time let's call it an s to stand for answer and what we're going to say is I saw

122
00:11:23,420 --> 00:11:28,090
it cause I knew plus

123
00:11:30,910 --> 00:11:35,210
to thirty times too.

124
00:11:35,260 --> 00:11:44,270
Let's just let's let's assume we've written an algorithm a very complex algorithm in this function and

125
00:11:45,160 --> 00:11:52,420
dysfunction known as No and we want to use the answer the algorithm provides in our C language it could

126
00:11:52,420 --> 00:11:54,620
be something like this.

127
00:11:55,000 --> 00:11:58,310
So as I said this example is very basic.

128
00:11:58,310 --> 00:12:00,430
It could be as complex as you wanted.

129
00:12:00,430 --> 00:12:08,500
You could write recursive algorithms that you could write algorithms for really really complex issues

130
00:12:09,160 --> 00:12:18,290
and just call them here because as we know machine language executes faster than the C it takes less

131
00:12:18,350 --> 00:12:21,380
cycles to go through the machine language.

132
00:12:21,380 --> 00:12:26,340
So good programmers mix these two languages together.

133
00:12:26,660 --> 00:12:30,590
So let's run this less components download onto our board

134
00:12:34,100 --> 00:12:40,060
preset open our debugger or

135
00:12:46,030 --> 00:12:53,950
Okay so let's add this new global variable to our watch window and see what answer we get whether indeed

136
00:12:55,710 --> 00:12:56,430
the works.

137
00:12:56,430 --> 00:12:58,790
So we have this here.

138
00:12:58,950 --> 00:13:02,590
So let's step again using this step controllers here.

139
00:13:02,710 --> 00:13:03,660
Let's step over

140
00:13:09,510 --> 00:13:10,300
okay.

141
00:13:10,320 --> 00:13:11,240
1 2 1.

142
00:13:11,310 --> 00:13:13,040
On this the answer.

143
00:13:13,110 --> 00:13:17,570
So that's the hexadecimal formless display in digital only see.

144
00:13:17,640 --> 00:13:25,550
And as you can see indeed one two one plus two hundred and thirteen times two is 702.

145
00:13:25,560 --> 00:13:33,260
So this is how you can write two functions in assembly and pull them to the C side of things.

146
00:13:33,450 --> 00:13:37,950
And the next lesson we'll take a look at how to write variables in assembly.

147
00:13:37,950 --> 00:13:43,890
After that then we'll look to the opposite direction of this meaning we'll take a look at how we can

148
00:13:43,890 --> 00:13:47,320
write functions in C and call them in assembly.

149
00:13:47,430 --> 00:13:54,660
We want to cover all grants here in assembly costs as I always say the foundations are very important.

150
00:13:54,660 --> 00:13:56,420
So see you in the next lesson.
