WEBVTT

00:01.580 --> 00:03.199
Hello, this is Dr. Miller, and this

00:03.200 --> 00:04.670
is Episode eleven point six of

00:04.970 --> 00:05.970
Assembly.

00:06.940 --> 00:07.989
Today, we're going to talk about

00:07.990 --> 00:09.607
memory offsets and then debugging

00:09.850 --> 00:10.850
and disassembly.

00:12.030 --> 00:13.080
Memory offsets.

00:14.050 --> 00:15.324
So when we have registers,

00:16.059 --> 00:17.679
as we've noted before, Lowden's

00:17.680 --> 00:19.600
store, are how we get

00:19.810 --> 00:22.149
data from memory into

00:22.150 --> 00:23.150
a register.

00:24.080 --> 00:26.089
And so the.

00:27.230 --> 00:29.060
Assembler can only load a bite

00:29.420 --> 00:31.279
and then do some

00:31.280 --> 00:33.140
shifting on on that bite,

00:34.280 --> 00:36.829
it can't do math operations

00:36.830 --> 00:38.447
and there are only certain values

00:39.020 --> 00:40.229
that can actually be loaded.

00:40.550 --> 00:41.873
And so here's this from the

00:42.140 --> 00:43.140
specification.

00:43.640 --> 00:45.680
So if we have a move operation,

00:46.430 --> 00:47.557
it can only have values

00:48.560 --> 00:49.760
from zero to

00:50.990 --> 00:52.117
four FS, so we can only

00:53.000 --> 00:55.219
move up to two bytes into a register

00:55.220 --> 00:56.229
given an immediate.

00:57.380 --> 00:58.948
And so if we have a 32 bit value

00:59.540 --> 01:00.649
that we want to load into a

01:00.650 --> 01:02.359
register, we actually have to do it

01:03.440 --> 01:04.939
through multiple operations.

01:04.950 --> 01:06.518
So we basically have to load and

01:06.830 --> 01:08.839
offset that will then allow it to

01:08.840 --> 01:10.069
read more data than

01:10.910 --> 01:12.080
a sixteen bit number.

01:12.590 --> 01:14.299
And this is because each arm

01:14.300 --> 01:16.064
instruction is thirty two bits long,

01:16.220 --> 01:17.929
and so therefore there is no way for

01:17.930 --> 01:18.930
you to load a 32 bit

01:19.760 --> 01:21.524
number when you have to have part of

01:21.620 --> 01:23.059
the instruction has to be the op

01:23.060 --> 01:24.579
code to say what data operation

01:25.040 --> 01:26.040
am I going to do.

01:26.870 --> 01:28.389
And so when we do that, when we

01:28.760 --> 01:30.524
went to low to register from memory,

01:30.650 --> 01:31.910
we basically have to

01:33.350 --> 01:34.820
sort of jump off of a location

01:35.660 --> 01:37.459
that has where we want to get that

01:37.460 --> 01:38.460
data from.

01:39.820 --> 01:41.349
And so, for example,

01:41.710 --> 01:42.886
if we do a low register,

01:43.540 --> 01:45.519
our zero and then we put an equal

01:45.520 --> 01:47.620
sign and then this number here,

01:48.010 --> 01:49.627
it'll actually generate some code

01:49.840 --> 01:51.789
saying there's going to be some data

01:51.790 --> 01:53.319
here and that data is going to be

01:53.320 --> 01:54.909
zero one one, two, two, three,

01:54.910 --> 01:55.910
three, four, four.

01:56.750 --> 01:58.416
And then you notice that it's it's

01:58.430 --> 01:59.899
actually converted this from Lord

01:59.900 --> 02:01.730
Register of the Equals sign

02:02.240 --> 02:04.400
to it has a PC and then A

02:05.210 --> 02:07.129
it has a negative there,

02:07.130 --> 02:08.440
but it's not really negative rate,

02:08.870 --> 02:10.219
zero additional bytes.

02:10.729 --> 02:12.439
And this is because in

02:13.070 --> 02:14.638
the instruction pointer actually

02:14.810 --> 02:16.159
points a couple of bytes ahead.

02:16.770 --> 02:17.799
And so it is actually

02:18.590 --> 02:20.029
pointing to bytes ahead.

02:20.030 --> 02:22.039
And so that is this location down

02:22.040 --> 02:23.755
here. So it's that offset zero from

02:23.870 --> 02:25.939
the program counter and the program

02:25.940 --> 02:27.649
counter actually points to here when

02:27.650 --> 02:29.330
it's executing this instruction.

02:30.230 --> 02:31.994
So it's a little bit odd, but that's

02:32.240 --> 02:34.239
the way the assembly works now,

02:34.250 --> 02:35.749
it'll be a little bit easier to see

02:36.410 --> 02:37.410
in our next example.

02:37.970 --> 02:39.538
So here we have several bites we

02:39.800 --> 02:41.417
defined. So we got our half words

02:42.020 --> 02:42.799
and our words.

02:42.800 --> 02:44.360
So half of it is 16 bits

02:44.780 --> 02:46.610
and a forward is 32 bits.

02:47.030 --> 02:48.259
And we can see here inside of the

02:48.260 --> 02:50.149
code that we're loading our zero

02:50.150 --> 02:51.889
gets the value of a well, it's going

02:51.890 --> 02:53.556
to jump down here at the end to an

02:53.750 --> 02:55.189
offset that points to a

02:55.730 --> 02:57.589
or an offset that points to B

02:57.620 --> 02:58.620
or C or D,

03:00.080 --> 03:01.489
and then the words, these are going

03:01.490 --> 03:03.409
to take up two bites

03:03.410 --> 03:04.669
and these words will take up four

03:04.670 --> 03:05.569
bites. So we'll see those

03:05.570 --> 03:07.187
differences in the addresses that

03:07.250 --> 03:08.250
get generated.

03:09.560 --> 03:11.059
So what I've done here is I've taken

03:11.060 --> 03:12.469
the original code and then compared

03:12.470 --> 03:13.744
it to the disassembly, the

03:14.090 --> 03:15.619
disassembly I got from

03:16.100 --> 03:17.374
using object dumps, object

03:17.990 --> 03:19.429
dump and then the minus two is

03:19.430 --> 03:20.839
disassembly. And then this is the

03:20.840 --> 03:21.889
binary that we want.

03:22.460 --> 03:24.349
And then it generates a lot of

03:24.440 --> 03:25.665
source code. And so I use

03:26.360 --> 03:27.879
grep to filter it and only look

03:28.190 --> 03:30.139
for the text main with a bracket.

03:30.890 --> 03:32.389
And so that will give me all of the

03:32.390 --> 03:33.859
code that is in here, which is why

03:33.860 --> 03:34.860
this is highlighted.

03:35.120 --> 03:36.786
And then a 30 means print 30 lines

03:37.280 --> 03:38.659
after you find that match.

03:40.160 --> 03:42.020
And so we can see here that

03:43.160 --> 03:44.728
the disassembly figures out what

03:44.750 --> 03:45.750
this offset is.

03:45.800 --> 03:47.909
So says Main plus Xerox

03:47.990 --> 03:49.430
to see for both of these.

03:49.910 --> 03:51.049
But if you look at the program

03:51.050 --> 03:52.699
counter, again, it's relative to the

03:52.700 --> 03:53.599
program counter.

03:53.600 --> 03:55.819
So this is twenty eight bytes away.

03:56.180 --> 03:57.919
Is this offset down here?

03:58.640 --> 04:00.249
And if we take to see if we take one

04:00.390 --> 04:02.539
four seven zero and add to see

04:02.540 --> 04:04.669
we end up with one zero four nine C,

04:05.360 --> 04:06.977
and so that is this location down

04:07.220 --> 04:08.220
here.

04:08.560 --> 04:09.609
And then these are going to be

04:09.610 --> 04:11.031
pointers to the actual memory

04:11.050 --> 04:12.520
addresses, and so when it does

04:12.910 --> 04:14.086
a load register is it is

04:14.770 --> 04:16.359
bouncing down here and getting this

04:16.360 --> 04:17.649
value and copying it in.

04:18.220 --> 04:19.389
And we can see that the first one

04:19.390 --> 04:21.007
was a word. And so we see that it

04:21.220 --> 04:22.220
takes up addresses C

04:23.110 --> 04:25.029
and D, and then the next one

04:25.030 --> 04:26.059
takes up E and F, and

04:26.980 --> 04:28.156
then the next one, which

04:28.810 --> 04:30.035
I think is C, it takes up

04:31.060 --> 04:32.579
zero one, two and three because

04:33.010 --> 04:34.627
it's four bytes and this one will

04:34.630 --> 04:36.099
take out four or five, six and

04:36.100 --> 04:37.100
seven.

04:37.610 --> 04:39.109
And so we can see how these these

04:39.110 --> 04:40.850
addresses keep going down

04:41.660 --> 04:43.699
and these two are the same right

04:43.760 --> 04:44.789
main plus to see, but

04:45.800 --> 04:47.074
the is the program counter

04:47.750 --> 04:49.429
is relative. It's pointing eight by

04:49.430 --> 04:50.557
eight bytes forward for

04:51.380 --> 04:52.380
the program counter.

04:54.110 --> 04:56.029
And then in order to another

04:56.030 --> 04:57.499
way to visualize this, so in the

04:57.500 --> 04:59.119
previous example, I used object

04:59.120 --> 05:00.949
dump, but additionally, there

05:00.980 --> 05:03.079
are professional tools that can

05:03.080 --> 05:04.699
allow you to basically look at the

05:04.700 --> 05:05.700
code.

05:05.750 --> 05:07.269
And so when I compiled my code,

05:07.670 --> 05:09.042
I have debugging symbols in.

05:09.380 --> 05:11.239
And so the disassembly actually

05:11.240 --> 05:12.679
knows the names of the variables

05:12.680 --> 05:13.680
that I used.

05:14.180 --> 05:16.009
If we were to remove those debugging

05:16.010 --> 05:17.209
symbols, that wouldn't actually have

05:17.210 --> 05:18.210
a name here.

05:18.860 --> 05:20.630
And we just have this data offset,

05:21.080 --> 05:22.249
and again, we can see that it

05:22.250 --> 05:23.869
automatically goes and calculates

05:23.870 --> 05:25.487
that for us and says, OK, this is

05:25.730 --> 05:27.769
data 104 and I can see

05:28.100 --> 05:29.569
this one is also the same.

05:29.570 --> 05:30.949
But again, those are relative

05:30.950 --> 05:31.950
program counters.

05:32.600 --> 05:34.369
And then we can see the same thing

05:34.370 --> 05:35.370
in Eita Pro.

05:35.600 --> 05:37.309
So Binary and India is a fairly

05:37.310 --> 05:38.390
inexpensive one.

05:38.870 --> 05:41.060
Either pro is it's a much more

05:41.570 --> 05:43.399
expensive dissembler that you

05:43.400 --> 05:44.779
can purchase for commercial

05:44.780 --> 05:45.780
purposes.

05:45.950 --> 05:47.420
But again, it shows us what we

05:47.570 --> 05:49.579
writing code versus what we actually

05:49.580 --> 05:51.709
get from the binary.

05:52.220 --> 05:53.779
And so it's going to, again, have

05:53.780 --> 05:55.520
these these relative offsets in

05:55.550 --> 05:56.550
their.

05:57.620 --> 05:59.749
All right, debugging, so

05:59.750 --> 06:01.318
a lot of the times will probably

06:01.430 --> 06:02.659
basically want to use the same

06:02.660 --> 06:04.309
debugging tools on

06:05.690 --> 06:07.219
the Raspberry Pi that would be used

06:07.220 --> 06:08.259
on regular Linux.

06:08.270 --> 06:10.249
So, for example, when I go into bug

06:10.250 --> 06:11.389
something, I'll make sure that I

06:11.390 --> 06:12.390
install GEF

06:13.220 --> 06:15.019
and GDB should be installed.

06:15.980 --> 06:17.499
One issue that I've run into is

06:17.810 --> 06:19.819
that Jegede beat is not pick

06:19.820 --> 06:21.529
up on the transition from regular

06:21.530 --> 06:22.519
mode to thumb mode.

06:22.520 --> 06:23.809
And we'll talk about that mode.

06:24.350 --> 06:26.360
And so our debugger has a bug in it.

06:27.440 --> 06:28.669
So as long as you're not doing that,

06:28.670 --> 06:30.639
the bug works in the same way.

06:31.680 --> 06:33.119
And additionally, we have programs

06:33.120 --> 06:34.949
like estrus, and this will

06:34.950 --> 06:36.539
work on Zaidee six, too, but it'll

06:36.540 --> 06:38.369
trace system calls and signals

06:38.370 --> 06:40.060
and we'll see some examples here.

06:40.680 --> 06:42.346
So, for example, all of the system

06:42.750 --> 06:44.249
calls related to

06:44.970 --> 06:46.860
just printing off the number 42,

06:47.700 --> 06:48.925
we can see that it's it's

06:49.620 --> 06:51.509
calling all of these system calls

06:51.510 --> 06:52.859
that allow us to

06:53.730 --> 06:54.869
have our user name.

06:54.870 --> 06:56.429
We're running the exact command,

06:56.430 --> 06:58.139
which is what the show will do.

06:58.480 --> 07:00.195
And then it gets down to the system

07:00.390 --> 07:01.410
call of. Right.

07:01.470 --> 07:03.149
And we'll see that that prints off

07:03.150 --> 07:04.290
the number 42.

07:05.250 --> 07:06.719
But there's a lot of information

07:06.720 --> 07:08.043
that can be gained by that.

07:08.100 --> 07:09.449
If you have a binary that you don't

07:09.450 --> 07:10.619
understand and you're trying to look

07:10.620 --> 07:11.790
at the assembly for it.

07:16.000 --> 07:17.379
And then with GDB, we're going to,

07:17.380 --> 07:19.060
again, have the same commands

07:19.390 --> 07:21.370
and we'll notice that in, again,

07:21.640 --> 07:22.889
GEF is installed here.

07:23.780 --> 07:24.799
We can see that it lists the

07:24.800 --> 07:25.976
registers and so you can

07:26.690 --> 07:28.209
see the regular name for these,

07:28.670 --> 07:30.320
so are zeroth are 12,

07:30.740 --> 07:32.112
are 13 is our stack pointer.

07:32.570 --> 07:34.187
So it points to the stack are 14,

07:34.850 --> 07:35.879
is the link register.

07:35.930 --> 07:38.089
AR 15 is the program

07:38.090 --> 07:39.090
counter.

07:39.170 --> 07:40.689
And so we can see all of those.

07:40.700 --> 07:42.049
And then the typical names that

07:42.050 --> 07:43.814
they're given, you can see the value

07:43.850 --> 07:45.620
that it has and then you can see,

07:46.010 --> 07:47.359
well if that's a pointer, what is a

07:47.360 --> 07:49.249
point to. So for example, when you

07:49.250 --> 07:51.499
start a program are zero

07:51.500 --> 07:53.149
is how many arguments you have

07:53.420 --> 07:55.429
are one is a listing

07:55.430 --> 07:57.259
of those and then are two is

07:57.260 --> 07:59.059
the pointer to the environment.

07:59.060 --> 08:00.649
And so our one has gotten changed

08:00.650 --> 08:02.179
here in the last instruction that I

08:02.180 --> 08:03.180
executed.

08:04.010 --> 08:05.659
You can also see exactly where you

08:05.660 --> 08:07.780
are as far as the stack.

08:07.790 --> 08:09.949
So we have a pointer to the stack

08:11.180 --> 08:13.399
and then our disassembly

08:13.400 --> 08:15.109
showing which line is the next line

08:15.110 --> 08:16.370
that's going to get executed.

08:16.760 --> 08:18.169
And again, it'll show things like

08:18.170 --> 08:19.939
program counter and then

08:20.210 --> 08:22.220
number twenty Ximenes plus twenty

08:22.940 --> 08:24.349
from that location is where it's

08:24.350 --> 08:26.149
loading that data from.

08:27.260 --> 08:28.789
And so you can see all of that in

08:28.790 --> 08:30.619
the disassembly, just as we

08:30.620 --> 08:32.719
had before, some additional

08:32.720 --> 08:33.847
commands. So if you are

08:34.610 --> 08:36.259
interested in virtual memory, you

08:36.260 --> 08:38.029
can use the map to see

08:38.419 --> 08:40.183
what memory addresses are are mapped

08:40.549 --> 08:41.549
to our binary,

08:42.409 --> 08:43.788
what libraries are loaded, you can

08:43.789 --> 08:44.869
use X Files.

08:45.200 --> 08:46.719
And then there's also a process

08:47.030 --> 08:48.549
memory map, and that's the info

08:49.070 --> 08:50.070
proc map, the spaces

08:51.080 --> 08:52.609
in between. And so those are some

08:52.610 --> 08:54.229
commands you can run once you've

08:54.230 --> 08:55.850
started a process that's running.

08:56.310 --> 08:57.859
You can get information about the

08:57.860 --> 08:59.477
virtual memory map and the loaded

08:59.570 --> 09:01.100
files and the process memory.

09:03.370 --> 09:05.134
Additionally, we can do the decisive

09:05.770 --> 09:07.599
a function and will show us all of

09:07.600 --> 09:09.429
the assembly for a particular

09:10.390 --> 09:12.279
function that we might have

09:12.580 --> 09:14.259
defined or we might want to look at.

09:17.380 --> 09:18.999
And then if you're starting off and

09:19.000 --> 09:20.799
you have a C program and

09:21.130 --> 09:22.749
you want to figure out exactly how

09:22.750 --> 09:24.609
it works, well, you can

09:24.880 --> 09:26.710
generate a listing for that

09:27.070 --> 09:28.070
C program.

09:28.870 --> 09:30.580
And so basically, this will show you

09:31.270 --> 09:32.919
the high level code and then what

09:32.920 --> 09:34.600
low level code gets created

09:35.080 --> 09:36.844
and you can see, for example, we got

09:36.850 --> 09:38.739
some locations here that are defined

09:38.740 --> 09:39.849
and those are probably not as

09:39.850 --> 09:42.009
useful. But you can see commands

09:42.010 --> 09:43.774
like, you know, push these registers

09:44.620 --> 09:46.188
and then move this register into

09:46.450 --> 09:47.822
this register and eventually

09:48.730 --> 09:50.347
a branch of the link for print F.

09:50.740 --> 09:52.149
Right. It ends up getting called

09:52.150 --> 09:53.469
because we're printing off something

09:53.470 --> 09:54.940
and then these registers get set

09:55.450 --> 09:57.039
are zero r one or two.

09:58.150 --> 09:59.424
And so the benefit of this

10:00.070 --> 10:01.540
is it allows you to instead of

10:01.840 --> 10:03.489
trying to reinvent the wheel, how do

10:03.490 --> 10:05.470
I call printf or how do I call this?

10:05.890 --> 10:07.119
If you know how to do it in a high

10:07.120 --> 10:08.979
level language, then

10:08.980 --> 10:10.689
you can reverse engineer and figure

10:10.690 --> 10:12.759
out how you would do it in raw

10:12.760 --> 10:13.760
assembly.

10:13.870 --> 10:15.609
And so it kind of gives you a little

10:15.610 --> 10:17.178
link between what the high level

10:17.200 --> 10:18.819
code be involved with some low level

10:18.820 --> 10:19.820
code B.

10:21.890 --> 10:23.149
All right, so today in our lecture,

10:23.150 --> 10:24.799
we talked about memory offsets and

10:24.800 --> 10:26.515
then some debugging and disassembly

10:26.690 --> 10:27.690
commands.

10:29.060 --> 10:30.109
In the future, we're going to talk

10:30.110 --> 10:31.874
about saving registers and an armory

10:32.210 --> 10:33.210
indexing.

10:34.590 --> 10:35.815
So why can't armload a 32

10:36.570 --> 10:37.570
bit address?

10:39.910 --> 10:41.649
Because there is no room in a for by

10:41.660 --> 10:43.375
top code to load a four byte memory

10:43.510 --> 10:44.510
address.

10:46.270 --> 10:47.649
If you have questions, you can email

10:47.650 --> 10:49.869
me Miller, MJ at Kincaide Edu

10:49.870 --> 10:51.429
and you can find me on Twitter at

10:51.430 --> 10:52.210
Milhouse 30.

