[CS61C FA20] Lecture 13.6 - Compilation, Assembly, Linking, Loading: Example — Transcript
Full transcript
- 0:00and welcome back we're at our final
- 0:02lecture
- 0:03c-a-l-l we saw the whole process let's
- 0:06actually take an example
- 0:07let's take the most complicated program
- 0:09ever not let's just do
- 0:11hello world so this hello.c
- 0:14is not your normal hello world um i've
- 0:16got a main i'm ignoring my rx c arc v
- 0:19for now
- 0:20i've got a printf but my printf isn't
- 0:22just hello world my printf has
- 0:24two arguments it is hello string
- 0:28and then i have a percent s and then the
- 0:29world is going to be fed into that
- 0:31percent s
- 0:32so i really have two arguments to that
- 0:33print f
- 0:35two strings and one string has a percent
- 0:37s
- 0:38formatting directive and the other is
- 0:40the other string world and that's going
- 0:42to fit into that hello world to make
- 0:43hello world and i return 0. that's it
- 0:46so i say i'm done i save this i include
- 0:49my
- 0:49my library standard io so i know about
- 0:51printf and now i compile this
- 0:54what do i get this beautiful piece of
- 0:57code
- 0:58you could have hand authored you
- 1:01understand this but let's actually take
- 1:02a deep dive into
- 1:03what every single line does let's do
- 1:04this together
- 1:06so first i have a text segment a text
- 1:09segment says that i'm going to have
- 1:10the fo the following is going to be my
- 1:13code so
- 1:14text says that the directive for that
- 1:16dotted line 2
- 1:17says i need to align this code to 2 to
- 1:20the whatever that number is bytes so i'm
- 1:22saying this is going to be word aligned
- 1:23aligned to every 32 bits
- 1:27this is saying i've got a the following
- 1:29is a
- 1:30global symbol that main label that
- 1:33you're seeing here
- 1:34i'm declaring that to be a global symbol
- 1:35other people can know about
- 1:37okay i've got my main label
- 1:40as i have with most functions if you're
- 1:42going to make any function call from
- 1:44there and then return
- 1:45i've got to be able to make room for ra
- 1:48and maybe other arguments
- 1:49so here i am i'm going to reduce the
- 1:51stack pointer by
- 1:52four bytes to allocate some stuff there
- 1:56now first thing i always do is i store
- 1:58ra
- 1:59into sp12 the top guy the top of that
- 2:02frame that's my
- 2:03frame i'm going to store ra there so if
- 2:05i make a function call i'd be able to
- 2:06restore that and get back to where i was
- 2:08that's pretty cool now i've got
- 2:12two strings i'm going to reference if
- 2:13you remember this line let's go back
- 2:15i've got two strings here's two strings
- 2:16i need to be set up
- 2:18these two strings need to be set up as
- 2:20arguments to
- 2:21printf so how do i set those up well
- 2:24let's compute the address of string one
- 2:26here's string one down here
- 2:27here's string one down here here's
- 2:29string one dot here's string two here
- 2:32notice the the formatting string is in
- 2:34is in string one okay
- 2:36so i say louis add-i
- 2:39i get the high 20 bits put them in the
- 2:42upper
- 2:42immediate area and then i add i the low
- 2:4512 bits and i put that in there okay
- 2:47then i do the same thing here so this is
- 2:50in some sense
- 2:51this is going to be my load address
- 2:54of string 1 and string 2 into a0 and a1
- 2:56because remember
- 2:57i want to call printf printf has two
- 2:59arguments a0 and a1
- 3:02so here it is call printf i've set up a0
- 3:05and a1 i call printf
- 3:08now i now i just i'm done so now i
- 3:11restore this restore ra fix the stack
- 3:14pointer
- 3:16load into a0 my return value i put that
- 3:18zero in here and i say return
- 3:21it's pretty clean dot section r0 data
- 3:24says i'm going to be in a read-only data
- 3:26area from here on
- 3:27in okay so r0data says read are not zero
- 3:30o r o means read only data i'm saying
- 3:33this is going to be
- 3:35i'm gonna i'm aligning this to four
- 3:36bytes this is a little bit different
- 3:38than this align this says
- 3:39how many bytes i want to align the
- 3:40following and i align these based on
- 3:43a word aligned and now here below here
- 3:45is my data okay
- 3:46that's it now you're gonna say am i done
- 3:49well that's the
- 3:50that s file is there more work to do
- 3:52with the next stage well let's see what
- 3:53we've got
- 3:54let's see if the next stage is we
- 3:56assemble this down
- 3:58here's my main code okay starts at zero
- 4:01every every single guy starts at zero
- 4:03this is a file by itself so i start this
- 4:05at zero and then eventually
- 4:06the final resting place has a different
- 4:08value but i'm starting here from zero
- 4:10and now this is the this is assuming
- 4:12that this is like
- 4:13the first instruction this is four that
- 4:15means that's going to be this
- 4:16instruction okay so it's basically
- 4:18saying these are the addresses of what
- 4:19these guys are
- 4:20and this is just the file so in this
- 4:21file is just this
- 4:23okay just make sure you know this is all
- 4:24that contains in that file
- 4:26nothing else this is just for you to see
- 4:29that this is just for you to see what's
- 4:30happening
- 4:31and this is just for you to remember
- 4:32where things are but only this stuff
- 4:34here this is all that's stored in a
- 4:36dotto file okay does that make sense
- 4:38all right so remember we had to make
- 4:40room on the stack
- 4:41we to make room for four bytes on the
- 4:43stack i store
- 4:44our a now i've got my luau louis
- 4:48addie that's for my first string that's
- 4:50for my a0
- 4:52that's for string one here's my louis at
- 4:54i for string two i still got zeros here
- 4:56i don't know where those values are
- 4:58going to be so i'm just putting zeros
- 4:59i'm letting you know that this is what's
- 5:00going to be here okay
- 5:01and what's this one do you remember
- 5:02where this is right here what's this
- 5:04line do you guys remember what that next
- 5:05thing after i
- 5:05set up a0 and a1 what's my what's the
- 5:08next thing i do you're right
- 5:10i've got a jowl i gotta call printf
- 5:13this is the call to printf right here
- 5:15okay gotta remember what that is
- 5:17then i restore my stack sorry i
- 5:20load back ra i restore my stack i load
- 5:23in a0
- 5:24as my return value and i return okay so
- 5:26that's what main does
- 5:27and all i've got to do is figure out how
- 5:29to then those guys in red
- 5:30that's the job of the linker well done
- 5:35so what do i do i fill those in
- 5:39this is the value of string one boop
- 5:42this is the value not really the value
- 5:43but this is what has to be there in
- 5:45those values to be able to handle string
- 5:46two
- 5:47now a0 has some combination of this as
- 5:50the upper 20 and lower 12
- 5:51upper 20 lower 12 for string one and
- 5:54string two
- 5:54ie a0 and a1 this is where
- 5:58printf lives okay love that
- 6:02and well that's at least the uh that's
- 6:04at least the uh that 20 bits there for
- 6:06that
- 6:07and then that's all i need to do now
- 6:09you're going to say well that was great
- 6:11except that how did you calculate those
- 6:14values
- 6:15for this okay so the target's address
- 6:17for string
- 6:18one is 20 in hex
- 6:21in the upper 20 bits and a 10 in the
- 6:24lower 12 bits
- 6:25sounds good that doesn't seem crazy so
- 6:28here's my instruction sequence louie
- 6:31here's 20 here and add i the lower 12.
- 6:35seems reasonable there except it doesn't
- 6:38work
- 6:40why doesn't it work immediates
- 6:43in risk five are always sign extended
- 6:46we knew that but maybe we forgot about
- 6:48it a
- 6:50if you remember a is 10 10 has a 1
- 6:53in the upper bit so the highest the 12th
- 6:56bit
- 6:56the 11th bit by index but the 12th bit
- 6:59by counting from 1
- 7:00that's a one well when i sign extend
- 7:03it's going to be all ones
- 7:05so it looks like this i signed it to be
- 7:06all like this all
- 7:08ones see that all ones
- 7:12when i add this by the way what's all
- 7:13ones if i were to say like think about
- 7:14number two's complement what's all ones
- 7:17negative one so if i just look at the
- 7:19upper 20 bits
- 7:21and just add this value to this value
- 7:24just forget again i had only though
- 7:26those guys i don't this this is all
- 7:27zeros right so that's not going to
- 7:28affect the upper 20 bits okay a10
- 7:30doesn't mean the lower 20
- 7:31lower 12 bits so i'm fine with that but
- 7:33if i if i just consider those two
- 7:34numbers even though
- 7:35they're the upper 20 if i just consider
- 7:37them by themselves all f's
- 7:38which is really minus one and 20 what do
- 7:41you get
- 7:43you get one less than 20 which is one f
- 7:46so i'm off i'm off by exactly that one
- 7:49this is
- 7:50actually because this counts for a
- 7:51negative one i'm actually off by that
- 7:54one
- 7:54if that makes any sense so to fix
- 7:58it i take my 20 i add 1 to it
- 8:02to compensate for the fact that when i
- 8:03sign extended i'm going to lose the one
- 8:05and then when i add the a10 on the lower
- 8:0812 sign extend it remember you can only
- 8:10add 32 bits to 32 bits in the systems
- 8:12it's not like
- 8:12oh i only add the 12. now you gotta add
- 8:1432 to 32 to get 32. that's what our alus
- 8:16do
- 8:17our arithmetic logic units they only
- 8:18know how to add 32 to 32 to get 32.
- 8:20so then i'll get the right answer 28 10.
- 8:24so i've got to have this like
- 8:25compensating factor of this extra
- 8:28uh it's actually 2 to the 12. i got this
- 8:30extra 2 to the 12. that's up there
- 8:32to do that so what is this
- 8:35so by the way how do i figure out what
- 8:37this value is what do i figure out what
- 8:38this
- 8:38total value was this is going to be the
- 8:41immediate right the immediate that i put
- 8:42in there is going to be the a10
- 8:44well what is if i sign extended to all
- 8:46fs what does that become
- 8:47well if i have a number and it looks
- 8:49like it's negative right i have this
- 8:50number what
- 8:51is it let's figure out what it is all fs
- 8:53means the upper guy is a one if it's all
- 8:55upper guys and one
- 8:56two's complement is it means it's a
- 8:57negative number how do i figure what
- 8:58that value is
- 8:59invert the bits and add one and that's
- 9:01what the negative number was okay
- 9:03so let's take a look at this let's
- 9:04complement all fs and a10 well if i
- 9:07flip all those it becomes a zero flip a
- 9:10a
- 9:10is 10 10 remember 10 10 okay
- 9:1410 10 for a well the opposite of that is
- 9:17101. so there's a 5 there okay
- 9:21so that 101 becomes this 5.
- 9:24this one is one off of well flip that
- 9:27it's all ones except for the lowest guy
- 9:29that's an e
- 9:30so this guy is an e if it's a zero flip
- 9:33those bits it's an
- 9:34f 5ef is my value let's add that one now
- 9:37again
- 9:385ef remember to figure out what the
- 9:41two's complement is
- 9:42i flip it and add one so 5ef
- 9:45plus 1 is
- 9:495 5f0 0 is 15 20
- 9:52okay so that means this number
- 9:56was negative 15 20. okay
- 9:59so minus 15 20.
- 10:03so here we go instruction sequence is
- 10:06minus 15 20. add i and by the way
- 10:10let's do this here we go loop
- 10:14this calculates 28 10.
- 10:17so therefore i'll go back here so
- 10:19therefore
- 10:21what i have is this number
- 10:25is minus 15 20 which is what i want
- 10:28i want that number and i want this one
- 10:32to be 21. remember so the plus 1 goes up
- 10:36there i need to add 2 to the 12. that's
- 10:3821.
- 10:39the lower side is minus 15 20. and that
- 10:41calculates the address of string 1. so
- 10:43remember
- 10:44louis 21 in hex at i
- 10:47minus 15 20. and that's why whoops let
- 10:51me show you here
- 10:52that's why look look this is
- 10:55louis 21 at i
- 10:591520 huh okay
- 11:02pretty cool right in conclusion
- 11:07what does a compiler do it takes a
- 11:09high-level language
- 11:10here c converts it down to the assembler
- 11:13language
- 11:14which you can have different assembler
- 11:15languages if you changed a different isa
- 11:17it'd be a different assembly language
- 11:19risk five is different than mips okay so
- 11:21those are different assembly languages
- 11:23we're living in the risk five world now
- 11:25assembler removes the pseudo
- 11:27instructions sets up two tables
- 11:29a symbol table of all the symbols it
- 11:30knows about in that file and a
- 11:32relocation table
- 11:33of all the things that need to be fixed
- 11:35in the linker stage
- 11:36the linker goes to the symbols takes all
- 11:39of them in aggregate figures out are
- 11:40there any duplicate symbols error
- 11:42or any missing symbols error okay if i'm
- 11:45calling somebody but i have no symbols
- 11:46there error also once i have them
- 11:49once that's all covered i have to now
- 11:51place them all together concatenate all
- 11:52the text first
- 11:54all the text first all the data next and
- 11:57fixing those links i've got it now
- 11:59working it out out
- 12:00and sets whatever and makes use of
- 12:02whatever debugging that persists that
- 12:03continue there okay so whatever other
- 12:05header information in the edit out file
- 12:07done the loader takes that as we said
- 12:10before
- 12:11sets up the stack takes it and takes a
- 12:13disk disk file
- 12:14makes it make some memory for the system
- 12:16copies the text and data into that
- 12:18memory this is i'm summarizing this
- 12:19whole lecture in two in one minute
- 12:21copies all the all that text and data
- 12:23from edit out
- 12:24into that memory sets the stack pointer
- 12:26with any arguments on the command line
- 12:28sets up a0 and a1 to be the value of arc
- 12:31c and arc v
- 12:33and sets the stack pointer clears the
- 12:35registers and says
- 12:37go and watch his back happily as a as a
- 12:40clam as this thing continues to run and
- 12:42when it's all done
- 12:42whatever the return value was it passes
- 12:45it back through through the os
- 12:46to the top level and we're all done
- 12:50amazing that's the goal that's the high
- 12:52level picture
- 12:53and you now understand how compiling
- 12:56assembling linking and loading works in
- 12:58a risk five system
- 12:59congratulations give yourselves a hand
- 13:01high five
- 13:03well done good stuff we'll see you folks
- 13:05at the next module
- 13:06take care
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 13.6 - Compilation, Assembly, Linking, Loading: Example by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,489 words across 389 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.