[CS61C FA20] Lecture 13.3 - Compilation, Assembly, Linking, Loading: Assembler — Transcript
Full transcript
- 0:00and welcome back so one down three to go
- 0:03let's take a look at what the assembler
- 0:05does so again here we are in the big
- 0:08picture
- 0:08the assembler's job is to take in the
- 0:10desk file that's that assembly language
- 0:12program the risk five that you've
- 0:13written that you're learning to write
- 0:14from the last couple of weeks
- 0:16and the goal is to produce object code.o
- 0:19and you're going to ask yourself and
- 0:20hopefully you'll see from this
- 0:21why do we do this why don't we just go
- 0:22straight from the assembler to produce
- 0:24the actual executable you're going to
- 0:26see what there's a two-step process and
- 0:28it does help to have
- 0:29a layer of linking in there we'll
- 0:31explain that a little later
- 0:32so again the input to the system of the
- 0:35assembler is dot s file that's risk five
- 0:37if you're in that isa the output is
- 0:40object code
- 0:41and this is gonna take contain tables
- 0:43and really that's the reason the reason
- 0:44we don't just go straight there because
- 0:45we want these
- 0:46information tables to be able and part
- 0:48of this is to be able to have
- 0:51lots of different lots a big program
- 0:54broken into lots of.c files
- 0:56each of the dot cs compiles down to dot
- 0:58os and then those.os at the last stage
- 1:00are linked together to make one
- 1:01executable
- 1:02and that is really the benefit of having
- 1:04these three layers we'll talk about that
- 1:05when we see that picture
- 1:07so the assembler also is going to read
- 1:09and use directives we'll talk about that
- 1:10in a slide or two
- 1:12replace the pseudo instructions you
- 1:13learned about that last last lecture
- 1:15the pseudo instructions like move t1 t2
- 1:18gets replaced by the add-i
- 1:19instruction our goal is to produce the
- 1:22machine language.o files
- 1:23and create create the object file so
- 1:26what are some directives that we have
- 1:28so some of the directors we have are
- 1:31here listed here these five directives
- 1:33tell the system it's not a line it's
- 1:35like a directive it's not a lie i mean
- 1:36it is a line it takes up a line in the
- 1:38code but
- 1:39it says what to do at each of these
- 1:40stages so
- 1:43dot text says the following from here
- 1:45till the next time you see a directive
- 1:47this is going to go in the text segment
- 1:49that's the machine code that's the
- 1:50actual code that you ran
- 1:51you ran this down and the actual hello
- 1:54world add one to two
- 1:55subtract load word that is down in the
- 1:58dot text segment
- 2:00the dot data says to it from the the
- 2:02following is going to be data i'm going
- 2:04to use if you have a big array that'll
- 2:05live in the dot data section
- 2:08dot global with the symbol says this is
- 2:10where this is the space
- 2:12in the static area where the following
- 2:14symbol is going to live
- 2:17dot string says the following is just
- 2:19text string
- 2:20take it literally as that you'll see
- 2:22that at the end of the lecture of that
- 2:24and dot word if i want to store some
- 2:26values there
- 2:27some raw memory words i'm going to store
- 2:29those in the dot word area and label it
- 2:31as such
- 2:33so here's we saw the first one we saw
- 2:35the first example of a move which which
- 2:37gets translated to an add-i that's not
- 2:39magical you saw that before how about
- 2:42negate
- 2:43i take t1 and negate it it was a
- 2:44positive 5 i want to make it negative 5.
- 2:46kind of nice i don't worry about how to
- 2:48do that here's how you do that you take
- 2:50you take zero you subtract t1 from it
- 2:53and now that's
- 2:54zero minus t1 is negative t1 store that
- 2:56in t0
- 2:57same idea as negate but a little easier
- 2:59to write in the gate than it is to write
- 3:00the sub thing and get the maybe got the
- 3:02order wrong i just call it negate
- 3:04negate t1 put it in t0 next
- 3:07load immediate li that says load this
- 3:10immediate and directly put it into t0
- 3:12now you can realize that that's the same
- 3:14as saying add i immediate
- 3:16to zero and then put into zero but it's
- 3:18kind of nice to have just load immediate
- 3:20directly
- 3:21how about not this is the bitwise knot
- 3:25this isn't not a single bit this is 32
- 3:28bits of not
- 3:29okay so how do you do that well you xor
- 3:32with minus one what does minus one look
- 3:34like
- 3:35you know what minus one looks like in in
- 3:38two's complement
- 3:39yeah all ones all ones all
- 3:42ones so you take all ones and if you xor
- 3:45with that immediate
- 3:46xor says if it's a zero keep it xor says
- 3:50if it's a one
- 3:50flip the bit so this flips the bits of
- 3:53all of t1
- 3:54and all of t1 bits get flipped and get
- 3:57stored into t0
- 3:58pretty cool so now you get to say not
- 4:00and that's a 32-bit wise
- 4:02knot love it branch of equal to z
- 4:06zero if t zero
- 4:09is zero then you branch if that's equal
- 4:11to that so your branch of t zero is
- 4:13equal to zero
- 4:14i mean nice branch of the loop so here's
- 4:16another way of saying it
- 4:18branch of equal t zero to zero it does z
- 4:21t zero equals zero if so branch to the
- 4:25loop
- 4:25same idea of saying branch of equal to
- 4:27zero to zero just again a short shortcut
- 4:29way of doing that i'd much rather write
- 4:31the code on the left than the code on
- 4:32the right
- 4:33and that's what's nice about having
- 4:34these pseudo instructions load address
- 4:37so i've got an address here and i want
- 4:40to store that in t0 where does this live
- 4:42where does that label live load that
- 4:44address into t0
- 4:46sounds great well there's two ways of
- 4:48doing it you can load
- 4:49upper immediate which loads 20 bits into
- 4:53t0 okay so i'm going to take the 20 bits
- 4:55the upper 20 bits of that of that label
- 4:58and put it into t0
- 4:59and i said you know what now i also need
- 5:02to stuff the lower 12 bits
- 5:03how do i so now i've i've kind of
- 5:05slammed in 20 bits on the high side
- 5:07how do i store the lower 12 bits well
- 5:10add i
- 5:11you know this it kind of makes sense
- 5:13right you know that there's no way
- 5:16for me to in one instruction take 32-bit
- 5:19not only have the instruction have the
- 5:21register label and have the string
- 5:22itself
- 5:23i mean have the have the label itself
- 5:24that doesn't make any sense so i
- 5:26say i'm going to take the top 20 bits do
- 5:29it in one stage and then
- 5:30add i the lower 12 bits by saying add i
- 5:33that to t0 and put it there so that's it
- 5:37or
- 5:38i could do another thing both of these
- 5:40are similar but they're different
- 5:41in a subtle way add upper media to the
- 5:43pc this is a pc relative operation
- 5:46same idea so the same the bottom is the
- 5:48same this is the same but add
- 5:49uppermediate is a pt
- 5:51pc relative operation again two ways to
- 5:53say load address one is more static
- 5:55addressing and one is pc relative
- 5:57addressing
- 5:59so let's don't forget the following
- 6:02we've got to sign extend our immediates
- 6:04so this is an immediate
- 6:06this is an immediate uh this is an
- 6:08immediate here
- 6:09this is an immediate okay those have to
- 6:11be sign extended because
- 6:12at i in the actual data path and when we
- 6:15build the machine we're gonna see this
- 6:16in the next lectures when we build the
- 6:18machine out of this
- 6:19i can't just add 12 bits i've got to add
- 6:2132 bits to 32 bits to get 32 bits
- 6:23so how do i take a 12 bit value and make
- 6:25it 32 i've got to sign extend those guys
- 6:28don't forget also this is another
- 6:30important part that when i'm branching
- 6:32i'm counting by the half words that's
- 6:35one of the risk five features
- 6:37some some might say miss features where
- 6:38you're counting by the half words not
- 6:40the words so
- 6:42all right so now you've seen we've done
- 6:45the first stage we should
- 6:46replace our pseudo instructions the next
- 6:49step is we've got to produce some
- 6:50machine code
- 6:51okay so the object file so what do we do
- 6:55for my object code well if i've got some
- 6:57simple cases the simple cases is always
- 6:59the ads the add subtracts logical shifts
- 7:01they're just numbers
- 7:03we can play with that and all the
- 7:04information i need is in the instruction
- 7:0632 bits tells me everything i need to
- 7:08know don't need to do much about those
- 7:10what happens when i get to branches and
- 7:12jumps well those are pc relative
- 7:14okay whenever you have pc relative you
- 7:16got to query what the pc is and then use
- 7:17that somewhere
- 7:18in that process so here's the nice thing
- 7:21about that once my pseudo instructions
- 7:23have been replaced by real ones
- 7:24sometimes one becomes two instructions
- 7:26you saw that before
- 7:27once that's been done i know the
- 7:28relative distance between where i am and
- 7:30where
- 7:30to where i want to go so if i have a
- 7:33branch of equal and i need to be
- 7:34counting by those half words
- 7:36i can now i know exactly how far away
- 7:39to be able to then count by half words
- 7:41how many how many half words to jump
- 7:42there
- 7:43to branch there and then replace that
- 7:45number so i actually can replace that
- 7:46number i'm pretty happy with that
- 7:48so those are handleable meaning at the
- 7:49out when i'm done with when i'm done
- 7:51with the dot oh file
- 7:52those final values are there i don't
- 7:53need to count wait for someone later
- 7:55stage
- 7:55so i can just count those numbers put
- 7:57those numbers in that field and i'm done
- 7:59how many half words do i need to branch
- 8:00i'm done so that's actually easy
- 8:02so far this is the easy case this is
- 8:06one of the so this is one of the
- 8:06challenges we have
- 8:09what if i'm branching forward
- 8:12well that's nice you can should be able
- 8:14to branch anywhere right i should be
- 8:15able to go here and branch there and
- 8:16branch there
- 8:17you know if i had a go to that's kind of
- 8:19a different thing but the point is i
- 8:20want to branch i want to do a branch
- 8:21here and i have a label i want a branch
- 8:23but what if and here's this piece of
- 8:24code that says you know set t2 is nine
- 8:27if two two is greater than zero set less
- 8:30than
- 8:30and if it's if uh if t2 is less than or
- 8:33equal to zero go to l2
- 8:34otherwise uh i subtract one from t2 and
- 8:37then
- 8:38jump back to l1 so this is a little loop
- 8:40that does something
- 8:42well by the time i get to here
- 8:45by the time i get to here i'm going to
- 8:48l2
- 8:49if i'm walking down okay a single pass
- 8:52i'm walking down i got here okay
- 8:54l2 problem is i haven't seen l2 yet i'm
- 8:57walking down
- 8:57line one line two line three i don't
- 8:59even know where l2 is
- 9:01l2 min it might be an error l2 might i
- 9:03mean maybe it was hand coded by a
- 9:04student and it has an error and l2 was a
- 9:06label that mistyped it
- 9:07it's not there at all well that's going
- 9:08to be a challenge what happens
- 9:10when l2 is below what happens if is does
- 9:12exist but it's below
- 9:14well here's how we have to do we have to
- 9:17make two passes
- 9:18so we can't just count one by one a one
- 9:21pass system one by one by one
- 9:22i get to a label if i haven't seen it
- 9:24yet if i if it's a label i've seen
- 9:26before i get it because i've already
- 9:27replaced these two instructions i'm
- 9:28walking my way down i've seen it before
- 9:30okay that's the line i can count back
- 9:31and i got that no problem i make a note
- 9:33of where it was i can go back
- 9:34but if i haven't seen the label before
- 9:36that's gonna be a challenge so one pass
- 9:38is
- 9:38so what we do is two passes the first
- 9:40path well three passes the first pass is
- 9:42replace the pseudo instructions
- 9:43the second pass is make a note of where
- 9:45all the labels are
- 9:46the third pass is when i see the label
- 9:48in the branch now i know where all the
- 9:50labels were that was the second pass
- 9:52i said the third there's two passes to
- 9:54do this and the first the first sister
- 9:56you can replace and remember the labels
- 9:57are
- 9:58but the idea of all this is the second
- 10:01what we call it two passes
- 10:03we we don't really count the replaces
- 10:05instructions as a pass
- 10:06we do that and now i have two full
- 10:08passes once that's done go one remember
- 10:10the labels
- 10:10go two and now if i see a label i know
- 10:13how far
- 10:14in half words to go forward for my
- 10:16branches okay so i've got that covered
- 10:18second passes uh notes where the labels
- 10:20are and then puts the number in now i
- 10:21can
- 10:22hardco hard code what that branch value
- 10:24is and i'm done
- 10:25so that's pretty cool we cover that case
- 10:27now
- 10:29here's an example here jump to l1 jumps
- 10:32back
- 10:33uh three words so that's six half i'd
- 10:35store six there that's
- 10:36actually minus six there and this says
- 10:38three words forward or six half words
- 10:40i'd store
- 10:40plus six in that in that branch value
- 10:43there
- 10:44okay
- 10:47how about this one what about pc
- 10:49relative jumps
- 10:50and branches so here's jump
- 10:53offset if i say jump to this offset it's
- 10:56a pseudo instruction it actually expands
- 10:58to
- 10:58jump and link zero write something to
- 11:01zero it doesn't change it at all
- 11:02and to that offset okay so again
- 11:06this is great this is also
- 11:09position independent code pic i just
- 11:11count the number of half words between
- 11:13the target and the jump
- 11:14and i'm ready to go so this is a pc
- 11:15relative jump anything that's pc
- 11:17relative when you hear the word
- 11:18pc relative that means position
- 11:20independent code because my pc is
- 11:22something that works with me i'm here
- 11:23i'm here i'm here program kind of tells
- 11:24me where i am
- 11:25pc relative says relative to where i am
- 11:26it's not absolute it's
- 11:28relative and so those relative things
- 11:29are great that produces position
- 11:31independent code we love position
- 11:32independent code
- 11:33because i don't have to move it later i
- 11:35can now if i if it happens to get
- 11:37relocated someplace different it's still
- 11:38relative to where it is and it's still
- 11:40it's you know minus three minus three is
- 11:42gonna be wherever if i end up moving
- 11:43this piece of code
- 11:44up and down in the final location it
- 11:47doesn't matter minus three is still
- 11:48minus three from this finger to this
- 11:49finger
- 11:50still is those two minus three as i move
- 11:51it in a global position
- 11:53in the final a dot out so that's great
- 11:57how about a reference to static data so
- 11:59load address
- 12:00becomes a louis add-i um if i want
- 12:03position independent code
- 12:05i talked about that before i want to use
- 12:06the add upper immediate but if i if i
- 12:09use it louis if i use the louis add-i
- 12:11these require the full 32-bit address
- 12:13the final load the final resting place
- 12:15not relative to where it is in my.o file
- 12:17but the final resting place in a dot out
- 12:19and that's going to be trouble so that
- 12:20isn't position independent code at all
- 12:22so those we can't determine yet a louie
- 12:24added we cannot determine yet
- 12:25we cannot add you at eyepiece add upper
- 12:28immediate pc
- 12:29but we can't for a louis okay so
- 12:32here's the idea we also need
- 12:35to build some tables sometimes dot c
- 12:38dot c dot c dot c becomes that s dot s
- 12:41dot s
- 12:42and then there are some symbols that can
- 12:43be used like an x turn
- 12:45by another c file so i need to be able
- 12:48to know which
- 12:49symbols are available to me so these are
- 12:52any labels
- 12:54if i have a function here i want to be
- 12:56able to jump to this function call i'm
- 12:58going to you know call that function and
- 12:59another guy i need to know
- 13:00that label in this function in this
- 13:02program can be called by this dot c
- 13:05program okay if you're if you load
- 13:07math.h
- 13:08and you want to call sine you didn't
- 13:10write sine sine lives over there in
- 13:13math.math.lib math.a so you need to be
- 13:15able to get a label of does sine
- 13:17exist there as a symbol i can use so
- 13:20every time you produce this object code
- 13:21you have to declare
- 13:22what are the symbols that people could
- 13:24use could jump to could call to
- 13:26also there's some data that i have that
- 13:28people are using let's say i have a
- 13:29global
- 13:29array that's going to be used by this.c
- 13:31file you shouldn't have to copy that
- 13:32global right here should be able to
- 13:33reference it so how do you reference
- 13:35that external guy so you want to be able
- 13:36to reference the labels
- 13:37and some data from external files that's
- 13:40the idea so labels and data are two
- 13:42things that you have in your symbol
- 13:43table
- 13:45the relocation table is the table of
- 13:48things that are to do
- 13:49remember if you can directly write the
- 13:5132 bits
- 13:53write them out add i add just write it
- 13:56out i have all the things i need
- 13:57go pc relative i've got all i need go
- 14:00after you've done that you know the two
- 14:03passes we talked about before
- 14:04but there are some things i need to know
- 14:06the actual
- 14:07absolute address of where things are and
- 14:10i can't know that until i finally have
- 14:11the what we call the final resting place
- 14:13in the eight out out
- 14:14and the final executable so the
- 14:16relocation table says
- 14:17this is where kind of it's almost like
- 14:19you put in a star saying to fill in
- 14:21later
- 14:22the relocation table is the list of
- 14:23things you need to fill in label
- 14:25fill in later when you finally link them
- 14:27together in fact that's what
- 14:28linking means link means fix those link
- 14:30addresses fix those
- 14:32things like where i say to do work later
- 14:34star do work later here
- 14:36that's actually what the link is going
- 14:37to do is fix those fix those links
- 14:39so anytime i have an absolute label by a
- 14:41jowl or jalar
- 14:43those things or in la i need to be able
- 14:46to know what the final location is be
- 14:48able to load that address into it and i
- 14:50don't it's not about relative it's not
- 14:51the actual address in the final
- 14:53resting place also any data in the
- 14:56static section if i have data static
- 14:57section i don't even know where the
- 14:58static section is at this point at the
- 14:59level at the level of the assembler i
- 15:01don't even know what that static section
- 15:02is that's the job of the linker to kind
- 15:03of create that
- 15:04so all of that's going to be done and
- 15:06labeled it's going to be put on the list
- 15:08of to-do's in my relocation table
- 15:10okay symbol tables are things that could
- 15:12could be called in my program things
- 15:13that could be called or my data that's
- 15:15going to be exportable or my labels that
- 15:16could be called into
- 15:17that's my symbol table my relocation
- 15:19table are all the ones that i didn't
- 15:20really have the final values there so
- 15:22make a star and add them to the
- 15:23relocation table so that somebody later
- 15:25the linker will replace those and fix
- 15:27those later pretty cool
- 15:29so last slide that was a big piece this
- 15:31is a long i'm like 15 minutes into this
- 15:33lecture now
- 15:34the object file format contains almost
- 15:37by the way all four
- 15:38file formats you've seen have a header
- 15:39you saw ppm in your project
- 15:41that had a header so the header is it
- 15:43tells you kind of
- 15:44what's the size of things like the
- 15:46following file contains
- 15:48the following movie contains the
- 15:50following file contains the following
- 15:51pieces and they live here here here and
- 15:53here so you can just jump into those
- 15:54guys
- 15:55what's the text segment what's the
- 15:56machine code what's the data i'm using
- 15:58what's all the raw data i'm going to be
- 16:00storing all those all those um
- 16:02strings all those uh all the
- 16:06hard-coded fibonacci series the
- 16:07fibonacci of the first thousand
- 16:08fibonacci's that's in the dot data area
- 16:10that's stuff that's gonna be there
- 16:11be usable it's hard-coded data put it
- 16:13right there i have my relocation table
- 16:15again relocation is the stuff to fix the
- 16:18things i didn't have those absolute
- 16:19addresses i need to be able to fix those
- 16:20that's like the work to do
- 16:22symbol table what are all the symbols
- 16:23that other people can call in to me to
- 16:24get to
- 16:25get access to my functions or my data
- 16:28any debugging information remember we
- 16:29talked about the translator says hey
- 16:31let's do some work and add more
- 16:32debugging information that can be a ton
- 16:33of stuff
- 16:34so all the debugging information is also
- 16:36in the object file format that's all dot
- 16:37oh
- 16:38and for example the standard file format
- 16:40is elf and here's a link to that okay
- 16:42next up the linker i'll see you there
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 13.3 - Compilation, Assembly, Linking, Loading: Assembler by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 3,634 words across 552 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.