So in this lesson we're going to take a look at how we can write functions in C and then import them
to assembly to use them.
This becomes necessary when we write in purely assembly code and then we find ourselves requiring functions
and other capabilities that we cannot access directly through assembly programming like LCD controls
and that Perry throws.
So when this happens we write that function C and then pull it to the Assembly side.
We're going to take a look at that example here.
So as always we come here to Project New Vision projects to create on your project.
And I'm going to create a new folder.
It's a good practice to store every new project in a new folder.
And I'm going to call this imported from seed.
And then I open the folder and then call the project name port to see as well.
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
device.
We need to startup then.
Okay.
And then we will target 16 megahertz and a box to Larry's ICD.
I like this.
Then over here we create to followers like we did before but this time domain file would be the assembly
file.
So we're going to help Main dot s and function dot see going to write a function in C and then run the
program in assembly.
So we start by clicking here and then control is to save then create this first one name that's s then
create a new file control ESC to say begin.
And Dan I call this one function.
Don't see don't come here.
Double Click cared to do it see.
Can I find it s open yeah.
Sure.
All files look at it.
Yes.
And we'll have them both.
So we expand this to find them.
And what we start with.
Let's start by writing our function in C.
Let's say we write in a function that would basically add a constant value to the variable.
What if I variable would pass.
So we open her and let's including a study into library included and
H
and then come here and then would declare our variable let's say we declare a variable thanks to its
size integer we call it num the number and we have a function called ITER art and number two the variable
to the number we pass to a variable.
Show this what it's going to look like.
Int
either returns a tight end and its arguments is void.
And basically what we want here is to take this number this number of variable that were created up
here.
We want this function to return.
What if the finding of number is this number plus say another number say four.
So this to pay the very basic algorithm of our function.
This way does takes this and then it will return.
If your 10 number
show we have one variable.
If we if we set a fair go to 100 then this function will return one hundred and thirty four.
If we said if there were two two hundred will return two hundred and support to less then without this
function.
And as you know it could be as complex as you want.
Let's.
You want to use this function in assembly.
So we go to the main dot s which is the assembly side of things.
We apply to the content of this function.
And as you can know dysfunction would have two areas that date and team code area.
So it was stopped by declaring the areas we use that directed area and then the first to line to the
end.
We import a global variable and remember what we call a global variable called the NUM.
Yes this is it.
We have to import this.
So we used to import key word there and then we have to import the function cos we're going to use the
function as well.
And we use the four key ones were function schooled.
Now now that we've done to create the second area which is the code area you start by area specified
item area.
Then we've used conductive text keyword to make communicate well to enable it to communicate with a
C side and call it Area.
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
then we apply the outline to bite this and then we use it thumb instruction set.
So we are that here and then we indicates the start of our programming.
We do this by exporting the label that starts to program this label we can give it a name starts on
Main which we call this label name.
This is where it resets handler.
We'll start that program.
So all we have to do now is start our program like this long blond
is not a variable to load a number into our variable and then let the algorithm perform Y has to perform
and give us the answer.
So to do that we use the LTA point to address off the variable.
By doing this.
The name of the variable that is dead the number we want to point a variable we store in another register
unless we want to put the number 100 in the variable so we have this number 100 in the variable.
So all we have to do is use this for an order store is what we put in there.
And remember source destination is our one course everyone is pointing to the address of the variable
2 after this or we have to do its branch the function or subroutine of the algorithm which is other.
So when we come here one thing you realize is that we don't have to write the subroutine here.
We don't have to specify any algorithm of either show which has branch there and then a line and then
we end the code.
So this is a lot of value.
Hundred.
We load a value we load value hundred into a variable.
We store it and then we'll branch to the algorithm.
And then the algorithm would return would return the answer to us which is said the algorithm should
return known and num becomes nonplussed day for.
So we expect to see 134 when we check our depart view.
So let's see.
So this.
Download or if this must download or there is an error somewhere.
So before we download onto our board we need to go to the D setup file and change the starting point
in the Reset handler and there's the file we're looking for start up the s.
And as we always do control f we look for reset handler there's the one you want and what we want is
for it to start in the assembly code which we named the starting point as main like this
show we compile we have zero errors we download onto the board
okay talk good that would reset
the knife to the can opener.
Dick Parker can watch not just the fair variable here.
We can watch its final fight when it could finish running so we can up to the watch window one hit like
this and display it decimal and it's one thing for show.
This is how we write right function in C and bring it to assembly.
It's very straightforward and the function we write can have multiple argument.
We can just write a normal C function in this Come here.
What happens when you write a function and you return from the function to assembly.
Is that the return value stored in the receiver register.
So when the function returns.
What if it's returns is returned in the RSA register in the same way.
When you export from here the answer Register does that export the first firm until we see the first
parameter is passed to the Iris here.
So this is how we import and export between assembly and C code.
We have to know this because when we start calling in our final project which will be in the next lesson
we will be used in assembly code.
And when we need to access certain peripherals that we think might not be directly accessible or maybe
a bit more cumbersome to access to assembly we are going to write it in C and use in assembly good programs
makes assembly and C didn't go all out assembly or all out C so I think it's important to end this this
course with our final project being a mixture of C and assembly.
So get ready as we start building our robot to end this course.
Yeah see you in the next lesson.