1
00:00:03,040 --> 00:00:10,050
So in this lesson we're going to take a look at how we can write functions in C and then import them

2
00:00:10,050 --> 00:00:12,550
to assembly to use them.

3
00:00:12,550 --> 00:00:20,350
This becomes necessary when we write in purely assembly code and then we find ourselves requiring functions

4
00:00:20,860 --> 00:00:27,850
and other capabilities that we cannot access directly through assembly programming like LCD controls

5
00:00:27,970 --> 00:00:31,340
and that Perry throws.

6
00:00:31,360 --> 00:00:37,060
So when this happens we write that function C and then pull it to the Assembly side.

7
00:00:37,140 --> 00:00:39,840
We're going to take a look at that example here.

8
00:00:39,890 --> 00:00:46,520
So as always we come here to Project New Vision projects to create on your project.

9
00:00:47,410 --> 00:00:50,160
And I'm going to create a new folder.

10
00:00:50,170 --> 00:00:53,530
It's a good practice to store every new project in a new folder.

11
00:00:53,640 --> 00:00:57,630
And I'm going to call this imported from seed.

12
00:00:57,700 --> 00:01:06,720
And then I open the folder and then call the project name port to see as well.

13
00:01:06,740 --> 00:01:17,980
And then I save it and I'll board team for C 1 2 3 2 8 6 p.m. and then on the CMC as we need a call

14
00:01:18,620 --> 00:01:19,350
device.

15
00:01:19,360 --> 00:01:21,900
We need to startup then.

16
00:01:21,930 --> 00:01:22,670
Okay.

17
00:01:22,680 --> 00:01:30,360
And then we will target 16 megahertz and a box to Larry's ICD.

18
00:01:30,370 --> 00:01:32,710
I like this.

19
00:01:32,980 --> 00:01:38,980
Then over here we create to followers like we did before but this time domain file would be the assembly

20
00:01:38,980 --> 00:01:39,660
file.

21
00:01:39,700 --> 00:01:46,420
So we're going to help Main dot s and function dot see going to write a function in C and then run the

22
00:01:46,420 --> 00:01:48,010
program in assembly.

23
00:01:48,160 --> 00:01:57,040
So we start by clicking here and then control is to save then create this first one name that's s then

24
00:01:57,070 --> 00:02:01,000
create a new file control ESC to say begin.

25
00:02:01,420 --> 00:02:05,170
And Dan I call this one function.

26
00:02:05,350 --> 00:02:08,620
Don't see don't come here.

27
00:02:08,620 --> 00:02:11,960
Double Click cared to do it see.

28
00:02:11,960 --> 00:02:14,400
Can I find it s open yeah.

29
00:02:14,520 --> 00:02:14,940
Sure.

30
00:02:14,940 --> 00:02:16,790
All files look at it.

31
00:02:16,870 --> 00:02:17,620
Yes.

32
00:02:17,860 --> 00:02:19,220
And we'll have them both.

33
00:02:19,560 --> 00:02:23,250
So we expand this to find them.

34
00:02:23,250 --> 00:02:24,690
And what we start with.

35
00:02:24,780 --> 00:02:27,350
Let's start by writing our function in C.

36
00:02:27,650 --> 00:02:35,530
Let's say we write in a function that would basically add a constant value to the variable.

37
00:02:35,550 --> 00:02:38,400
What if I variable would pass.

38
00:02:38,400 --> 00:02:45,650
So we open her and let's including a study into library included and

39
00:02:48,350 --> 00:02:48,770
H

40
00:02:52,960 --> 00:03:01,130
and then come here and then would declare our variable let's say we declare a variable thanks to its

41
00:03:01,130 --> 00:03:11,510
size integer we call it num the number and we have a function called ITER art and number two the variable

42
00:03:11,510 --> 00:03:13,660
to the number we pass to a variable.

43
00:03:13,730 --> 00:03:15,900
Show this what it's going to look like.

44
00:03:16,160 --> 00:03:16,570
Int

45
00:03:20,000 --> 00:03:27,170
either returns a tight end and its arguments is void.

46
00:03:27,590 --> 00:03:36,480
And basically what we want here is to take this number this number of variable that were created up

47
00:03:36,480 --> 00:03:38,300
here.

48
00:03:38,300 --> 00:03:40,950
We want this function to return.

49
00:03:40,950 --> 00:03:48,860
What if the finding of number is this number plus say another number say four.

50
00:03:48,870 --> 00:03:53,690
So this to pay the very basic algorithm of our function.

51
00:03:53,730 --> 00:04:00,370
This way does takes this and then it will return.

52
00:04:00,590 --> 00:04:02,990
If your 10 number

53
00:04:06,680 --> 00:04:09,060
show we have one variable.

54
00:04:09,620 --> 00:04:16,950
If we if we set a fair go to 100 then this function will return one hundred and thirty four.

55
00:04:17,240 --> 00:04:22,370
If we said if there were two two hundred will return two hundred and support to less then without this

56
00:04:22,370 --> 00:04:23,010
function.

57
00:04:23,060 --> 00:04:26,210
And as you know it could be as complex as you want.

58
00:04:26,410 --> 00:04:27,180
Let's.

59
00:04:27,340 --> 00:04:30,070
You want to use this function in assembly.

60
00:04:31,160 --> 00:04:35,680
So we go to the main dot s which is the assembly side of things.

61
00:04:35,720 --> 00:04:38,830
We apply to the content of this function.

62
00:04:39,530 --> 00:04:44,860
And as you can know dysfunction would have two areas that date and team code area.

63
00:04:45,380 --> 00:04:53,490
So it was stopped by declaring the areas we use that directed area and then the first to line to the

64
00:04:53,600 --> 00:04:54,220
end.

65
00:04:54,460 --> 00:05:01,610
We import a global variable and remember what we call a global variable called the NUM.

66
00:05:02,510 --> 00:05:03,350
Yes this is it.

67
00:05:03,350 --> 00:05:05,090
We have to import this.

68
00:05:06,470 --> 00:05:13,760
So we used to import key word there and then we have to import the function cos we're going to use the

69
00:05:13,760 --> 00:05:15,120
function as well.

70
00:05:15,650 --> 00:05:21,330
And we use the four key ones were function schooled.

71
00:05:22,850 --> 00:05:30,920
Now now that we've done to create the second area which is the code area you start by area specified

72
00:05:30,990 --> 00:05:32,400
item area.

73
00:05:32,510 --> 00:05:41,210
Then we've used conductive text keyword to make communicate well to enable it to communicate with a

74
00:05:41,210 --> 00:05:46,580
C side and call it Area.

75
00:05:47,570 --> 00:05:59,320
And then the odd dot text to be able to link it to C then it's a code area that's make it read only

76
00:06:00,470 --> 00:06:12,610
then we apply the outline to bite this and then we use it thumb instruction set.

77
00:06:12,650 --> 00:06:17,530
So we are that here and then we indicates the start of our programming.

78
00:06:17,540 --> 00:06:24,530
We do this by exporting the label that starts to program this label we can give it a name starts on

79
00:06:24,530 --> 00:06:27,710
Main which we call this label name.

80
00:06:27,830 --> 00:06:29,520
This is where it resets handler.

81
00:06:29,530 --> 00:06:30,620
We'll start that program.

82
00:06:31,790 --> 00:06:38,200
So all we have to do now is start our program like this long blond

83
00:06:40,740 --> 00:06:49,920
is not a variable to load a number into our variable and then let the algorithm perform Y has to perform

84
00:06:49,950 --> 00:06:51,210
and give us the answer.

85
00:06:51,360 --> 00:06:56,690
So to do that we use the LTA point to address off the variable.

86
00:06:56,700 --> 00:06:57,870
By doing this.

87
00:06:57,870 --> 00:07:05,220
The name of the variable that is dead the number we want to point a variable we store in another register

88
00:07:05,220 --> 00:07:11,800
unless we want to put the number 100 in the variable so we have this number 100 in the variable.

89
00:07:11,970 --> 00:07:18,280
So all we have to do is use this for an order store is what we put in there.

90
00:07:18,750 --> 00:07:25,470
And remember source destination is our one course everyone is pointing to the address of the variable

91
00:07:25,740 --> 00:07:34,840
2 after this or we have to do its branch the function or subroutine of the algorithm which is other.

92
00:07:34,940 --> 00:07:42,360
So when we come here one thing you realize is that we don't have to write the subroutine here.

93
00:07:42,360 --> 00:07:53,160
We don't have to specify any algorithm of either show which has branch there and then a line and then

94
00:07:53,300 --> 00:07:55,000
we end the code.

95
00:07:55,050 --> 00:07:59,790
So this is a lot of value.

96
00:07:59,790 --> 00:08:00,870
Hundred.

97
00:08:01,070 --> 00:08:05,160
We load a value we load value hundred into a variable.

98
00:08:05,160 --> 00:08:09,270
We store it and then we'll branch to the algorithm.

99
00:08:09,570 --> 00:08:18,420
And then the algorithm would return would return the answer to us which is said the algorithm should

100
00:08:18,420 --> 00:08:21,780
return known and num becomes nonplussed day for.

101
00:08:22,080 --> 00:08:26,800
So we expect to see 134 when we check our depart view.

102
00:08:26,940 --> 00:08:28,920
So let's see.

103
00:08:29,340 --> 00:08:30,530
So this.

104
00:08:30,730 --> 00:08:40,310
Download or if this must download or there is an error somewhere.

105
00:08:40,340 --> 00:08:49,490
So before we download onto our board we need to go to the D setup file and change the starting point

106
00:08:49,580 --> 00:08:54,380
in the Reset handler and there's the file we're looking for start up the s.

107
00:08:54,980 --> 00:09:07,790
And as we always do control f we look for reset handler there's the one you want and what we want is

108
00:09:07,790 --> 00:09:14,210
for it to start in the assembly code which we named the starting point as main like this

109
00:09:18,320 --> 00:09:23,540
show we compile we have zero errors we download onto the board

110
00:09:26,040 --> 00:09:30,550
okay talk good that would reset

111
00:09:33,910 --> 00:09:37,010
the knife to the can opener.

112
00:09:37,010 --> 00:09:41,050
Dick Parker can watch not just the fair variable here.

113
00:09:41,050 --> 00:09:47,350
We can watch its final fight when it could finish running so we can up to the watch window one hit like

114
00:09:47,350 --> 00:09:55,890
this and display it decimal and it's one thing for show.

115
00:09:55,910 --> 00:10:00,500
This is how we write right function in C and bring it to assembly.

116
00:10:00,500 --> 00:10:05,660
It's very straightforward and the function we write can have multiple argument.

117
00:10:05,690 --> 00:10:09,800
We can just write a normal C function in this Come here.

118
00:10:09,860 --> 00:10:14,270
What happens when you write a function and you return from the function to assembly.

119
00:10:14,280 --> 00:10:18,630
Is that the return value stored in the receiver register.

120
00:10:19,340 --> 00:10:21,560
So when the function returns.

121
00:10:21,560 --> 00:10:26,620
What if it's returns is returned in the RSA register in the same way.

122
00:10:27,470 --> 00:10:35,600
When you export from here the answer Register does that export the first firm until we see the first

123
00:10:35,600 --> 00:10:38,960
parameter is passed to the Iris here.

124
00:10:39,200 --> 00:10:44,770
So this is how we import and export between assembly and C code.

125
00:10:44,810 --> 00:10:51,410
We have to know this because when we start calling in our final project which will be in the next lesson

126
00:10:51,950 --> 00:10:54,870
we will be used in assembly code.

127
00:10:54,980 --> 00:11:01,880
And when we need to access certain peripherals that we think might not be directly accessible or maybe

128
00:11:01,960 --> 00:11:10,460
a bit more cumbersome to access to assembly we are going to write it in C and use in assembly good programs

129
00:11:10,490 --> 00:11:19,160
makes assembly and C didn't go all out assembly or all out C so I think it's important to end this this

130
00:11:19,160 --> 00:11:23,810
course with our final project being a mixture of C and assembly.

131
00:11:24,140 --> 00:11:30,110
So get ready as we start building our robot to end this course.

132
00:11:30,110 --> 00:11:31,640
Yeah see you in the next lesson.
