WEBVTT

00:00.000 --> 00:05.616
Week 15: Strings Above, Pointers Below, for exercise 49k_exec.

00:06.216 --> 00:12.631
A script steps this explainer, it was built with Claude, and its numbers are examples, not the lecture's.

00:13.160 --> 00:15.366
After the November 24 lecture:

00:15.966 --> 00:21.717
on Tuesday, December 1, you write build_process, part of exec, which starts a program.

00:22.317 --> 00:27.225
push_argv, given, lays out the program's arguments: this page steps it.

00:27.825 --> 00:30.991
No solution code here: only the machine's state.

00:31.520 --> 00:36.574
A command reaches exec as words: the program's name, then its arguments.

00:37.174 --> 00:45.141
The new program can read only its own memory, so exec puts the words on the program's own stack page, at its top.

00:45.741 --> 00:47.777
Two kinds of data end up there.

00:48.280 --> 00:51.842
Above, the strings, each ended by a NUL byte, a zero.

00:52.442 --> 00:58.237
Below, the array, argv: one entry per string, then a NULL entry, argv[6].

00:58.837 --> 01:02.415
Each of the others holds the address of its string's first byte.

01:02.920 --> 01:10.406
The lab typed this line into the reference, the finished kernel: run big lanterns whistle softly before daybreak.

01:11.006 --> 01:16.827
big is a given program. It ignores its arguments, but exec lays them out anyway.

01:17.427 --> 01:19.510
argc, the word count, is 6.

01:20.040 --> 01:26.897
The drawing is the top of the stack page, one row for every 8 bytes, the lowest address on the left.

01:27.497 --> 01:34.706
sp is a variable inside push_argv, not a register: it marks how far down the push has reached.

01:35.240 --> 01:41.486
The stepper starts with the stack page empty, and sp at its top, 0x1_1000.

01:42.086 --> 01:45.334
Beside it, the command's six words, and argc, 6.

01:45.934 --> 01:49.950
On the right, the console: these keys were typed into cargo run.

01:51.480 --> 01:55.857
The program's name goes first: "big", 4 bytes with its NUL.

01:56.457 --> 02:03.541
It starts at the left end of the top row; the 4 bytes above it, to its right, stay unused, drawn dashed.

02:04.141 --> 02:06.315
sp now points where big starts.

02:07.840 --> 02:11.551
"lanterns" is 9 bytes with its NUL: more than a row.

02:12.151 --> 02:17.441
It starts at the left end of a row, and its NUL is the first byte of the row above.

02:18.041 --> 02:20.919
The 7 bytes after that NUL stay unused.

02:22.440 --> 02:26.109
"whistle" is 8 bytes with its NUL: exactly one row.

02:26.709 --> 02:34.409
It starts at the left end of the next row down, and no byte above it is left unused: the string's box reads 0.

02:35.920 --> 02:38.276
"softly" is 7 bytes with its NUL.

02:38.876 --> 02:45.266
It starts at 0x1_0FD8, and 1 byte above it, to its right, stays unused.

02:45.866 --> 02:49.183
Each string sits below the last, at the start of a row.

02:50.680 --> 02:58.007
"before" is 7 bytes too: it starts at 0x1_0FD0, with 1 byte unused above it.

02:58.607 --> 03:03.416
sp follows each string down the page: its mark is on that row now.

03:04.920 --> 03:07.384
"daybreak" is 9 bytes with its NUL.

03:07.984 --> 03:16.323
It starts at 0x1_0FC0; its NUL takes the first byte of the row above, and 7 bytes stay unused.

03:16.923 --> 03:19.237
All six words are on the stack page.

03:20.760 --> 03:26.037
Now the array: one entry more than argc, 7 entries, 56 bytes.

03:26.637 --> 03:32.983
Below daybreak, sp minus the size is 0x1_0F88, the start of a row.

03:33.583 --> 03:35.109
Where does the array start?

03:38.620 --> 03:42.044
One row lower, at 0x1_0F80.

03:42.644 --> 03:46.492
The 8 bytes between the array's end and daybreak stay unused.

03:47.092 --> 03:50.441
The array's seven entries are drawn, still to be written.

03:51.960 --> 03:57.931
Now the entries are written. Each entry but the last holds the address where its string starts.

03:58.531 --> 04:03.873
argv[5] holds 0x1_0FC0, where daybreak starts.

04:04.473 --> 04:06.915
argv[6] holds 0: the NULL.

04:08.440 --> 04:13.430
push_argv returns, and its box shows the three values it returns.

04:14.030 --> 04:17.579
Every string and every entry is now on the stack page.

04:18.179 --> 04:19.533
Later, big runs.

04:20.040 --> 04:28.829
These keys were typed into cargo run, and now the typed line has its answer: run: pid 1 (big) exited with status 99

04:29.429 --> 04:31.843
Then the prompt, rv6$, is back.

04:32.360 --> 04:34.173
Two addresses for one string.

04:34.773 --> 04:42.549
The figure takes one entry from the stepper's last state: argv[5], which holds 0x1_0FC0.

04:43.149 --> 04:46.134
It leads to the bytes of daybreak, the NUL last.

04:46.640 --> 04:56.389
copyout, the kernel's copy into a program's memory, wrote those bytes in RAM at 0x87B6_AFC0, in this example.

04:56.989 --> 05:00.603
That RAM address is the model's example, not the lab's.

05:01.120 --> 05:04.531
push_argv is given, and the stepper is what it does.

05:05.131 --> 05:16.589
Its limits: MAXARG, 16, the most arguments a program may receive, and MAXARGLEN, 64, the longest a single argument may be, its NUL included.

05:17.120 --> 05:19.903
push_argv is given: you write none of it.

05:20.503 --> 05:31.472
You call it from build_process, the piece 49k asks you to write, and the next explainer, Four Registers Start It, draws what build_process must leave behind.

05:32.000 --> 05:37.242
The test runs programs of its own, with words of its own, and checks what they see.

05:37.842 --> 05:44.605
Its first step runs args with one and two, and the test prints: [ok] the program saw argc = 3

05:45.120 --> 05:51.947
Its second step runs echo with hello and world: [ok] echo printed its arguments: "hello world"

05:52.547 --> 05:59.114
Its other checks, a program longer than one page and a name exec cannot find, are the next page's.

05:59.640 --> 06:03.679
On the new stack page, the strings sit above and the pointers below.

06:04.279 --> 06:11.391
Each string goes below the last, rounded down to a multiple of 8; the array, to a multiple of 16.

06:11.991 --> 06:16.339
Next: the explainer Four Registers Start It, then Tuesday's session.
