Hello.
So in this next lesson we quaint.
Take a look.
How to declare variables in assembly.
Show us.
We always do.
I create a new photo on my end and call this one variable
in sin and then I'll just name the project variable and then open and select our board t up for C 1
2 3 0 h 6 p.m.
okay and then under CMC is to call on a device to startup and then we come here.
Targets options were set to the 16 mega heads and then debugger distilleries I C D I OK we're all set.
So like our previous program we're going to have to follow us here.
One assembly and then one C we do some of the work in assembly and cool it in C so I'll create a first
for here and then I'll press control X to save this empty file and what's the name of the group on this
one and I'll call it function Dot.
S just like the first program we created and I would say this one too and pull this one main let's see
after the double click here and then both files.
Then we could and then we start by right now the content of our function not C.
And over here we're going to introduce something new so far in our assembly code we've declared the
directive area and we've specified that the area is the code area.
And this lesson we're going to specify another area known as the data area.
That is where we would store our data.
So let's take a look at how this would go.
This is going to have two areas.
So there's the clear area.
What area is it.
It's the data area.
And then there's a line and a line 2 and then to create a variable you give the name of the variable
US label.
Let's call this scramble amount.
That's the name of our variable and we use the directive space and then we indicate the amount of space
we require this number for each space as abides.
So if we won here this would be one byte space which is equal to AIDS bits but we want 32 bit.
We want our variable amount to be a new 32.
So for here given this 32 bit.
That being said.
So what we have to do to make this variable callable outside this file.
We have to export it.
So we'll use the export keyword export amount.
So we've declared the we've declared a data area in order to make this visually appealing.
I usually do this for beginners.
I declare one area and put a line and then declared a second area.
So you can do this.
This not necessary but I think this would help you understand it entirely a little way to the next area.
Just normal area code area where we put a program code.
So we start with the area directive and then we start with the text to make it callable in C.
Then we make sure we tell them what area code and it's really only a line
a line 2 and it's thumb.
We write and inform instructions and we're going to create a function that function to contain our variable.
So we might as well export the function.
Otherwise we wouldn't be able to find it.
Let's call this function and march function for that amount.
Func OK let's just call it this.
So you can see the difference.
These two things these two items we export to this first one is variable code amount and this is a function.
So we want to use them outside this file hence export.
So we come here to write the contents of the function start with the function name and label to amount
on the school C
and we come here and basically what we're going to do here is load the variable here and put a value
in the variable so we're going to do by do that by pointing to the variable and then swap in swap into
value from one register to the variable you can think of this as how we manipulate the bids in our GC
GPI registers yeah our RC GC GPI and then a standard GPA it will registers registers festive for we
used to use the Delta are two point to point to these registers and remember the registers where the
registers with names like GPI both data underscore ah they were just place holders there were names
with give into the address to the address of that particular registers.
So you can think of the word amount as such a name such a name would place and a location in memory
which would be served here a space for two just like we did just like we did before by writing system
c c l I received GC keeping on a score Ah it's just an example of this what we did before yeah we pointed
first and then we toward add value later by using L2 R and then by doing this as you are one to get
the funding into our one two.
So after these two lines we end up with whatever value comes here in this register.
So this time we point to our variable amount.
And then let's move the value to another register let's say we want to store the number 89 in our variable.
So we have 89 nine here.
So to store we just SDR is a shorthand 0 0 and then I 1 remember as the r source the source is the number
eighty nine.
And then we store in what are is pointing to which is the memory location of all of our variable to
at this point we've stored 89 in the variable amount and then we returned from the function x r and
then we line and we.
And so we've created a variable here and you can create as many variables as you want.
Yeah.
So this what it looks like a new addition to this side would be declaring the data area and declaring
this you've not seen this before.
So pay closer attention to it.
So now that we've finished with this and we've exported this and this we can go to the seaside to to
right out the C code and let's let's conclude it as to the end so that we can be able to use you into
theta to include best and that's h.
Sorry we have to use the keyword external to call our variable cos it's external.
Yeah it's literally external
and the name of the variable is amount plus we gave it and we have our function which is a int to return
a good amount on this call in C and tennis and forward
shall we have this in this too.
That's great on Main function in Spain.
And we have me here.
So let's call our function our function from the assembly scored amount amounts func.
Yeah okay.
So let's go back and take a look at the function again of our voice here it's empty.
It is through this function that we add something to the variable.
That is why we have to call this function here.
This just for this experiment to see that actually we've stored something in the variable.
So in order to do that remember this is the global variable here.
There's this fair amount.
We have it in C here.
So we can just read this and see whether indeed its value is eighty nine after the function has finished
running.
So all we have to do is call the amount here.
You can just call amount we assign it to another variable.
So let's create a global variable here call you in 32 weeks to end and let's call it C amount we just
create this global variable and then we assign C amount
to the the amount value.
So remember this amount is this amount is the same thing if we are to change to name to something else
the code wouldn't work.
So we just want to read the value of this file variable in assembly and displayed in this new variable.
This is just for display sake.
If we wanted to do a computation we wouldn't need this redundancy of writing one other global variable
to display this in a way I wouldn't confuse you.
Let's check.
And please remember to put what I do not understand in the comments line you would receive a prompt
reply.
So what we have to do now is to compile and we have zero errors with download onto our board
and we reset our board when we are done with that.
We just opened the door for you
and this time what we interested in is this is this global variable here amount we can just add it to
the watch window to see what happens in it.
Come on do this highlight right click and then up to 21
and once that is done we can just step through the code you can check this to display in decimal form
so once we done we can just pick over and see the changes that occur in the code so we can see we have
the value 89 89 is stored in our variable here.
This extend or variable and we've read in it from our new global variable.
So indeed our variable is working from assembly to see.
So show you more.
Let's say we were just closed as we cleaned this.
We don't run this C function or the code runs all right.
A variable wouldn't work cause it was the function that was doing the assignment for us so yeah
let's see
if she can see nothing happened.
Yes.
So yeah this is how to write a variable in assembly.
I hope you get to so in the next lesson it would take a look at how to import functions from C and use
them in assembly.
C You.