Before we conclude this module of the complete cortex an embedded developer course there are three more
topics we need to cover in this lesson we're going to take a look at how we can call our assembly code
from a C source code good programmers take advantage of the high execution code of assembly by mixing
assembly code and C code in order to increase performance of their programs.
So we're going to take a look at how to do here.
As always I'll create a new for the hair to store our project and I'll call it call.
Call it call from seed to indicate that the assembly is going to be called from C and I'll just name
the project call from C as well.
Yeah.
This name is to sort of tell the function of this particular project.
And as you know our board is to inform C 1 2 3 0 8 6 p.m. So we select our board.
And over here we have the call.
And on the device we have this startup like we always do.
So we're here.
So let's set the targets options debugger.
As you know we're using distilleries I CDI and under target.
It's the 16 megahertz that's the frequency of our board.
So yeah we're going to create two files on like most of our programs that we have just one file which
is that dot s file the assembly file.
Here we're going to have one dot c file and one as far the dot c file is where the program would start
and then did dot s file to contain the functions that we used to run the program.
So let's look at it pushed up by create and let's create this first file.
I'm going to call this far.
I'm going to say that US functions as a function dot dot s to indicate that would still function in
this file and then call the function in the c file and then I'm going to create another file like this.
And the control s name it made don't see it so let's add the files to our program.
We have the dot c and we come here.
Sure all files.
And then we can find it dot S..
So now let's type all the code.
Let's stop with the Assembly side.
We open all functions till s file and then here we write our assembly code.
We're going to stop by as always using the assembly directives.
I hope you remember these directives.
This is the code area that is read only and we outline to then we write in our code in time form the
instructions here and we're going to create a function.
Then we're going to call.
Let's call it.
Let's call this function.
No.
So basically this the function and we're going to call this function and c we call it number the export
keyword as you know makes our function callable outside this assembly file and this text also enables
us to use our code here in C.
So we've put the the right keywords in place to make it usable in C.
So let's write all the content of our function.
So we stopped by given the label which is that which is number n let's say we're storing just a number
one let's say.
What should a function do.
Let's say the function would display a number as simple as that.
When the function is called It's just displayed a number that is stored in it.
So let's say this move are riskier and let's move a random number 1 to 1 move this number there and
then we store this number in our series like this
and we return from the function this very basic example but this would make it very easy for you to
go.
So basically we have 1 2 1 stored in our shoe.
The reason why it's stored in Irish euro is because the return value of a function is stored in the
Irish you register in assembly.
If we had to store this in a different register say out to it wouldn't return the number.
So we store it in Irish here and also I should explain this before we go on as per the the a pieces
convention which is a set of conventions tenderness how to use the.
The instruction sets off the cortex and architecture.
We can use just registers are a zero hour one hour two hour three an hour twelve.
If we want to use registers are for an hour eleven we must push that current value onto the stock and
then pop these values out before we return from them.
We wouldn't talk about stocks here.
I think it's important to understand this bit of assembly before stock but I'll give I'll provide a
link for more reading on the cortex and dot com websites.
Both stocks this really straightforward article there which a colleague of my root so I'll function
in assembly is complete very basic.
So let's take a look at the sea side of the code.
So as we know we toward a number in the function the function is supposed to return a number.
So we know that return type is an integer which is you.
That's a two.
So what we're doing basically now is rights in the function prototype in the c file and the name of
the function here is no remember this name should be the same as the name we exported from assembly
and it takes a forward acumen to no arguments to the reason why we have this here is because we've not
included the STV int library which is part of the C ninety nine standard.
So what we have to do is include STV and the age and we have it disappear.
So we have our function.
We've caught it now let's go into our main main open close and let's see.
So let's create a global variable here and assign the return of the function to that variable and see
whether it's the same us or we have just went fogging purposes.
Okay let's call it fun you know this just
so we'll call a number here.
As you can see we have not written in a function here we just call in the function and we assign it
to return.
Finally we assign its return value to this global variable we've created code number.
So basically this function x is an assembly and we want to display it in C so the C this one here wouldn't
have any visual output in terms of LCD display on a microcontroller.
So in order to see this we have to go to the debug view and see our COBOL variable and see whether indeed
it is assigned one to one as our function says over here.
So let's do the first you compile and then you connect your board.
So once how port is connected with download onto the port okay and then we'll reset the port.
Now to verify whether our function works or if indeed we are able to access it from C we click here
to go to the debug view.
Okay we'll have that report for you now.
So basically
what you want is to highlight this right click and displayed.
And what is known as a watch window so you select here watch one window to basically we'll be watching
the changes that occur to this variable here and to display in decimal format you can uncheck this hexadecimal
display like this.
So now what we have to do is use this step controls to go through it line step by step to see whether
indeed the value changes and if the value is 1 to 1 which means indeed once one is shown India is shown
in C anyway.
If you are new to this debug view if you're not very conversant with it.
There is not of course known as the cortex and in internals hours which explains this and goes deeply
about what happens inside the machine code and inside the memory and registers when we run in cortex.
Applications you can take that quick course as well if you want to get more familiar with this but for
this course you don't need any familiarity all you need is to know these things and change for this
view.
So that being said let's continue.
So let's step what we have to do is click here.
Step over and as you can see this cropped mean in the next line to execute will be this line.
Line 6.
So let's step over again
and as you can see after executing this we have 1 to 1 that is value.
So 1 2 1 is indeed inside value.
So this show star were able to access the function that we wrote in assembly and see in fact to prove
this further.
Let's use the value let's use that value for our computation and see let's create a not a global treble
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
it cause I knew plus
to thirty times too.
Let's just let's let's assume we've written an algorithm a very complex algorithm in this function and
dysfunction known as No and we want to use the answer the algorithm provides in our C language it could
be something like this.
So as I said this example is very basic.
It could be as complex as you wanted.
You could write recursive algorithms that you could write algorithms for really really complex issues
and just call them here because as we know machine language executes faster than the C it takes less
cycles to go through the machine language.
So good programmers mix these two languages together.
So let's run this less components download onto our board
preset open our debugger or
Okay so let's add this new global variable to our watch window and see what answer we get whether indeed
the works.
So we have this here.
So let's step again using this step controllers here.
Let's step over
okay.
1 2 1.
On this the answer.
So that's the hexadecimal formless display in digital only see.
And as you can see indeed one two one plus two hundred and thirteen times two is 702.
So this is how you can write two functions in assembly and pull them to the C side of things.
And the next lesson we'll take a look at how to write variables in assembly.
After that then we'll look to the opposite direction of this meaning we'll take a look at how we can
write functions in C and call them in assembly.
We want to cover all grants here in assembly costs as I always say the foundations are very important.
So see you in the next lesson.