Okay, now we are ready to start a new
section.
We just saw in the previous section
Introduction to Architecture and Machine
Code and how it relates to C, and now we
are ready to start assembly programming,
okay?
So this actually is going to teach you
about movie movie instructions.
Not movie instructions, move instructions
that move data between registers and
memory and between registers themselves
and what their operands.
we are also going to see the various ways
we have available to address memory to
specify memory locations.
then we can look at an example called
swap, that just exchanges two pieces of
data in memory, and both in 32 and
64-bits.
So that's going to be an opportunity for
you to see the difference between 32-bit
assembly, and x86, and x86-64.
Then we're going to look at arithmetic
operations, how to use condition codes,
how to do branches.
how to write loops with assembly, and
then we're going to end with switch
statements.
So if we recall from last section, we
there's three basic kinds of
instructions.
Instructions that move data between
memory and registers, instructions that
perform arithmetic operations, like
addition for example.
And it could have as operands both a
memory or or registers, and then the
instructions that transfer control.
Instructions that change the flow of
execution of your program in the
processor, okay?
So, now let's start.
We're going to see now is instructions
that move data between registers and
memory.
There's two basic types of instructions.
One is called the of that type.
One is called load.
A load takes an address as a parameter,
goes to memory, gets that, the data
stored in that address and puts it into
the register, okay?
A store does the opposite.
A store takes a register as a parameter
as well and also an address and takes the
data off the register.
And stores in the specified memory
address and recall that memory is just,
is index just like an array, okay?
You have an array or a table, and it has
multiple entries.
So you're going to access that using an
index.
And the point of an index in an array or
in a table, the memory is an address, an,
a memory location.
Okay, so we saw briefly that the last
section, that there are eight general
purpose registers in IA32.
Of the eight registers, six of them,
these six here are general purpose.
And these two here, the esp and ebp have
spectial meaning, and we're going to see
how they're special in a, in a little
bit.
So there are multiple types of
instructions to move data.
They all start with these three letters
here mov.
Then this x here which in fact could be
one of these three b, w, or l depending
on the amount of data that's being moved.
If you use movl, l says for long words so
you're going to be moving 4-bytes worth
of data, okay?
If you use movw you're going to be moving
two bytes worth of data, and if you use
movb you're going to be using, you're
going to be moving 1-byte worth of data,
okay?
now in move instructions takes two
operands, where the data comes from and
where the data goes to.
So now let's, let's look at the specific
instructions.
Movl source, and destination, so move
data from the source to the destination.
And these are operands, these are the
source, this is a source operand and
destination operand.
And here's the type of operands, that you
could have in a move instruction.
The first one's called Immediate, think
of it as a constant.
This is a constant data, constant integer
data imbedded in the instructions itself.
For example, you could have x 400 as one
of, as one of your operands and it only
makes sense to be a source.
This is doesn't make to have a constant,
it doesn't make sense to have a constant
as a destination.
So a constant can only be a source of
your move operation, okay?
So now the other type of operand is a
register, okay?
It could be any of the eight registers
here that we have, okay?
In for example, if we do mov eax comma
edx, what are we going to be doing?
This'll eax, and edx are are operands, so
eax in this case is going to be the
source, and edx is going to be the
destination.
And we're going to get the contents in
eax, in stored in, in edx, okay?
So as I said before, esp and ebp, are
reserved for special use, but they can
still be used as operands.
Now, the third and final type of operant
in moving structures is memory.
So, since we're talking about movl and we
move 4-bytes of data that means that an
address there is, which is be specifying
the beginning of this four wide, 4-bytes
word, that's going to be used as an
operand.
Okay, in the one way it is an operand, is
to use to specify memory operand.
Here's an example.
It's to put, for example, a register IN
parenthesis, when you're doing that you
see that eax contains and address.
And now the operant for your move
abrasion is the data containing the
address that is stored in riser eax.
But as we'll see later there are many
many types of address modes many ways of
specifying addresses.
This is just one of them and so, the many
combinations of moving structures.
So for example, continuing with our movl
instruction, so the source can either be
an immediate value, a registered value or
a memory value, okay?
Suppose that you have an immediate as a,
as a source operand, and then you can
have a register as a destination operand.
Here's what we have, we have four is a
con, that's an example here.
Have an example here.
four is a constant, so it is a source
operand eax is the register and what it
is going to do, it is going to store
value x4 in register eax.
Now, we can also have memory as a
destination, so if we do movl, this
constant minus 147 in decimal here, and
store to eax in parentheses and as I just
told you, the previous slide This says
that now we're going to store it in a
memory location specified by the address
stored in register eax.
Now, I'll continue our examples here.
We could have register as a source and a
register as a destination, that's the
simplest case, that's a simple case,
right.
And here we have, we're going to get the
contents of eax and copy to register edx.
Now, we can go from register to memory.
So, for example, we can get the contents
of eax and store into, remember this is
in parentheses, so, into the memory
location specified by the asterisk stored
in register edx.
Finally, we could have memory as a source
operand and registered as a destination
operand.
This will be a type of load, you're
loading memory into, loading memory
dating to a register.
Okay, so here we're going to get the
contents of the location specified by
register or eax in store it in register
edx.
You cannot do memory-memory transfer with
a single instruction.
So, how would you do that, then?
Well, you move it to a register.
You could get data from memory to a
register.
That's step number one, and then you can
get data from a register.
So memory, that will be step number two.
That's how you move data from memory to
memory.
So now let's see what's the C Analog of
all of these operations.
So again, I have for example variable
eight that happens to be mapped in a
register.
and we store constant hex four and
another example here.
For example, I have this, this pointer
p_a.
We're going to de-reference the pointer
and say we're going to store the constant
minus 147 into the register, into the
location specified by pointer p_a.
Okay, so now since memory.
That means that since this is a pointer,
it's on the left-hand side, it's going to
be the destination.
That's why here in the destination, we
have a memory operand.
And if we look at the, the address held
by pointer p_a, that's what's contained
in register eax.
So now these other three cases here are
also pretty simple.
They've had two register, two variables
mapped into register, var d and var_a.
We're just copying the value from var_a
to var_d.
And then here I have if I have var_a,
which could be stored in a register is
stored in a register.
And I copy to a pointer, same thing here.
Pointer p_d syncs memory since restoring
pointers point somewhere in memory, the
destination often has to be memory.
And finally, here what we're doing is
loading data in this last example here.
We're loading data from memory so we have
a pointer here on the right-hand side.
Inter-registering, in this case is
[INAUDIBLE] these happen to be matched to
a register.
So let's look at the various ways we have
to address memory.
So these are the basic addressing modes.
There are many others, but the first one
is called indirect access.
Indirect just says that now the the
register, contains an address, and then
we're going to use that we're going, to
use the contents of the register as an
address to memory.
So memory, as, as we said memory was just
like an array, okay?
So, that means that I will have an index
here, and inside this index, is the
contents of the register specified inside
the address expression.
An example here so before, probably I'm
repeating this for the third time.
Now this register ecx holds an address
using the parentheses here, we say now we
want the contents of memory whose address
is stored in ECX.
Now this is a basic one.
The other type of addressing, we'll just
call it displacement.
And the way we specify is we have the
little you know a constant outside our
parenthesis here.
Now all it does is it adds that constant
to the base register.
So register R specifies a memory address,
but the final memory address is obtained
by adding the con consultant register
with with this constant D.
For example, here what we're doing is
we're getting these the contents of ebp.
Okay, so the contents of ebp, epb we're
adding eight.
Okay, and we are using that as the
address to memory, and we're storing that
into edx.
Easy, right?
So now let's use this basic addressing
modes in example.
So you have this C function called swap
that does default.
It takes two pointers as a parameter and
it gets the contents of XP, stores and,
and stores into a temporary variable c0.
It gets the contents of y, of the pointer
yp and stores into, into temporary
variable t1.
And then stores t1 into xp and t0 into
yp.
So just swapping the contents pointed,
swapping the locations pointed by these
pointers.
Now, as you see here, so okay, now this,
this, this is the assembly version of our
C code, the C code we just saw.
And so we have three basic pieces here.
One's called a set up.
It's getting and rerading the parameters
and, and, and setting up the stack and so
on.
We have the body, that is the body of the
function.
The body of the execution, and then we
have the finish up, where we restore the
stack to its original location.
We restore register values and and then
return to the caller.
So let's see how, how stack works.
Now, we have our the body, let's focus on
the body here, that's the body of of our
swap function.
And we have six assembly instructions.
So let's go now step by step.
And the registers we're using here,
here's the mapping of register to values.
So, yp here happens to be mapped into
register ecx, xp here happens to be
mapped into register edx.
Now t0 was mapped into register eax.
And t1 was mapped into register ebx.
And this mapping is a choice of the
compiler, okay?
The compiler chooses how to map the
variables into, into registers.
Okay, so now on the, on the right hand
side here we have what each one of these
assembly instructions is representing.
And this is the state of our stack in the
beginning of our execution.
Okay, so now we are ready to, to, to see
how this gets executed step by step.
Okay, so now, we have, here we have our
registers that's that, when we begin
executing.
And recall that both x, yp, and xp are
addresses, right?
So, they're pointers, that means that
the, they can't, they're pointers, so
they're variables that hold addresses and
the addresses the address of yp is 120
which is right here.
So and that means that yp points to this
location and xp is 124.
That means that xp points to this
location here.
And our registers and our ebp or stack
pointer, is located in address 104.
So, ebp points to here.
Now we're going to see that a lot of the
values are relative, a lot of memory
locations are relative to the stack
pointer.
Let's execute a versus structure now.
When we execute this move here, what are
we doing?
Well, we are storing the, the contents of
yp as a pointer into ecx.
So, since yp is stored 12-bytes away from
ebp, we're going to use as a source for
print a displacements address mode.
So, the way the address is obtained is by
getting ebp adding 12 and we're going to
use that memory that adds up and then
store it in this address, that's what
happens.
So we've got 120 And store it, store it
in ecx.
Great, so what we did there, we just got
then the address of yp, and stored in
ecx.
The same thing for xp, but now note that
this changed to 8 now, because xp's
stored 8 bytes away.
From EBP.
Great.
So now I read the X codes at one twenty
four which is the address held by pointer
XP.
So what, what we want to do now is store
the contents of the pointer YP.
Into register eax.
But remember, that we had just put yp
into ecx, so now ecx holds an address.
That means if we just use ecx as, as an
address, we're actually getting the data
pointed by YP.
And since yp was high out /g, add this
one to any, that is pointing right here.
Okay?
When we execute this instruction we're
getting the contents pointed by yp, that
happens to be stored in ecx, and loading
it into eax.
That's what happened here, okay?
All right, so now that's the next step,
we're doing the same thing for xp now.
Were getting the contents of the address
pointed by xp and loading into ebx.
But the point of xp was stored in
register edx so we use a memory.
Memory Source over here to get that in
stored in edx and so we get and that
happens to be.
We have edx right which is yes.
So edx is a destination, edx is 124.
That's an address so we're going to use
that.
So, we got this as the address here.
We got that contents there, and store it
in edx, because that's the destination
register.
So, what are we going to do now?
Well, now we're going to get the contents
of eax, which was contents of ip and
store in xp because we're swapping it,
right?
And we still have the address of xp in
edx.
So, now we have this as the destination
operand.
So when, when, when we do that, well
what's going to happen, this gets updated
with the, with the contents of eax.
And same thing happens when we're storing
to yp.
And note that we just left the addresses
of the pointers into, into the register,
because we use them to read them, and
then later to write to them.
Wyszukiwarka
Podobne podstrony:
t informatyk12[01] 02 101r11 012570 01introligators4[02] z2 01 nBiuletyn 01 12 2014beetelvoiceXL?? 01012007 01 Web Building the Aptana Free Developer Environment for Ajax9 01 07 drzewa binarne01 In der Vergangenheit ein geteiltes Land LehrerkommentarL Sprague De Camp Novaria 01 The Fallible Fiendtam 01 c4yf3aey7qcte73qcpk4awpowae4en5ggim26tiwięcej podobnych podstron