Hello, this is Dr. Miller, and this
is Episode two point three of
Assembly.
So today we're going to learn about
segments and executing functions,
segments.
So different segments have different
meanings inside of the assembler.
So we have the data section
and this is used for string and
global variables.
It's initialized data and then it's
reading. Right. So we can read data
from it and we can write data to it.
We also have the basis, so
the basis for initialize
variables and it is
both read and write additionally,
but it doesn't take up space.
And so you might say, well, why do I
want one or the other?
So the data is for something that we
would care about and we want to
actually give it a value.
The BBS's is going to be for
four Buffer's maybe.
So if you were going to read a file
from disk, you might put that
in the box and then
read the data from the disk instead
of wasting space in your binary
with useless data.
And then we have the text.
And so that is where our code goes
and this is read and execute,
meaning that the computer can read
it and then it can run it, but it's
not going to ever change it.
And that's the way your program
should be set up.
Now, people who are attacking
different things might change it so
that the text segment
is read, write, execute, so they can
write additional instructions
or some interpreters might modify
their text segment so that it is
read, execute and write.
So those are different
ways that you can change that.
But this is generally how our
assembly is going to work.
So in the data section, we can
define different pieces of data so
we can, as we've talked about
before, define a bite, define
a word or define a word.
And this is going to take up one
bite. This will take up two bites
and this will take up four bites.
And then for convenience, we can
define a whole bunch of bytes.
Right. So this is a string.
So each one of these is going to be
a bite. And so it's a lot easier
than trying to figure out what is
the ASCII or hex
value of each one of these.
We just type them in and then the
assembler will take care of that.
Functions.
So when we're executing a function,
we use the mnemonic call
for calling a function
and then the operand is a function
name that we're going to call.
So, for example, we could define a
string and then put that address
in X and then we'll call this
function called print string,
which prints what is pointed to by
X.
And we've got to make sure that we
have a null, terminated string.
So let's go ahead and do that.
So I've already created a project
and make here two point three
went into that directory and
already built it just so that
everything was ready.
I'm going to incommode
and I'll define a string.
Now, one of the things that
oftentimes my students forget
is to put in a zero
in here so this will null, null,
terminate our string.
So no means that this has the actual
zero byte inside
of it.
And so, as it said, what we're going
to do here, move
into X
a
call print
string, go
ahead and write and quit
type make.
Hopefully everything worked.
So we can see it printed off Hello,
World, but it didn't print off a new
line, which is slightly annoying.
So let's go ahead and add a new line
in here.
So a new line is the character 10.
Go ahead and that
run make.
And now we've got a new line at the
end of our hour program.
All right. So we just executed
a function that was called print
string, given that null, terminated
string that we had in there.
So inside of the
template, we have a bunch of
functions that have been created
that we can easily use.
So we have redirect, which
reads an integer from the user
and then stores that result in X.
We have print in it, which will
print an integer that's in X.
We have print and now which will
print to a new line
and then we have read character.
So this reads a single character
from the user and stores
it in X and it's actually in the
lowest half.
So Al of that register.
And we also have print string, which
we saw in the previous slide.
So we're going to do a little
example here, so we're going to go
ahead and print a prompt for the
user to enter two numbers.
We'll read those then, and then we
will add them together and go ahead
and print the result.
So I'll go ahead and use the same
program that I had before.
I want to take off the new line
here, because I think it looks
better this way.
So I'll go ahead and move my prompt
into X
Colbran string, and
then I'll want to read that
number from the user
so you can look in the slot in
the previous part of the lecture,
but we're going to go ahead and call
read it.
So now the result is an X,
so I'm going to go ahead and move
that to SBX.
So I'm just copying the data that's
in X into SBX.
Now once again, I will move
X prompt.
Call print
string,
read another number
and said, I have one of my numbers
in SBX and after this I
should have the other one inside of
Yak's.
So now I can go ahead and say, add
yak's SBX.
So that will add
the value that's an X to
the value, that's an SBX and save
the results into X,
and now I can call my
print end function, which
will print whatever is an X,
and I'll add a new line at the end.
Go ahead, make.
I didn't mess up,
so let's pick two numbers, let's say
98 and.
12.
So our result got printed one
hundred and ten,
right, and we could have done a
couple other things to make it maybe
a little bit nicer.
So, for example, let's
suppose that we wanted to print an
additional prompt
saying that the result and so I'm
going to have to save Yak's after
this in so I might have to do move
SBX yak's.
So save my results.
And I have
number one
and
number two in
SCIEX.
So let's let's do our result.
If you are not an insurgent,
you type exit will delete
characters.
Oh.
OK, so my.
Print off my string
and now down here, I needed to
move into X
X.
So now I've moved X into
X and then added the two together
and then called my print
print line.
So if we messed up,
no.
So I had ninety eight
and 12.
My result is one hundred and ten.
So in summary, we talked about
segments, we talked about executing
functions, and then I gave an
example of how we can execute
functions to solve a very
simple mathematical problem.
I'm just reading two numbers from
the user and then adding the results
together.
So in future lectures, we're going
to talk about extending numbers.
So extending the sign on signed
numbers that are stored to
complement and then doing
multiplication and doing division.
If you have questions, you can
contact me at Miller, M.J., at you
and Keda, Aidoo, and you can
find me on Twitter at Milhouse 30.