YouTube2Text

[CS61C FA20] Lecture 13.6 - Compilation, Assembly, Linking, Loading: Example — Transcript

by CS 61C Departmental · 2,489 words · 389 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back we're at our final
  2. 0:02lecture
  3. 0:03c-a-l-l we saw the whole process let's
  4. 0:06actually take an example
  5. 0:07let's take the most complicated program
  6. 0:09ever not let's just do
  7. 0:11hello world so this hello.c
  8. 0:14is not your normal hello world um i've
  9. 0:16got a main i'm ignoring my rx c arc v
  10. 0:19for now
  11. 0:20i've got a printf but my printf isn't
  12. 0:22just hello world my printf has
  13. 0:24two arguments it is hello string
  14. 0:28and then i have a percent s and then the
  15. 0:29world is going to be fed into that
  16. 0:31percent s
  17. 0:32so i really have two arguments to that
  18. 0:33print f
  19. 0:35two strings and one string has a percent
  20. 0:37s
  21. 0:38formatting directive and the other is
  22. 0:40the other string world and that's going
  23. 0:42to fit into that hello world to make
  24. 0:43hello world and i return 0. that's it
  25. 0:46so i say i'm done i save this i include
  26. 0:49my
  27. 0:49my library standard io so i know about
  28. 0:51printf and now i compile this
  29. 0:54what do i get this beautiful piece of
  30. 0:57code
  31. 0:58you could have hand authored you
  32. 1:01understand this but let's actually take
  33. 1:02a deep dive into
  34. 1:03what every single line does let's do
  35. 1:04this together
  36. 1:06so first i have a text segment a text
  37. 1:09segment says that i'm going to have
  38. 1:10the fo the following is going to be my
  39. 1:13code so
  40. 1:14text says that the directive for that
  41. 1:16dotted line 2
  42. 1:17says i need to align this code to 2 to
  43. 1:20the whatever that number is bytes so i'm
  44. 1:22saying this is going to be word aligned
  45. 1:23aligned to every 32 bits
  46. 1:27this is saying i've got a the following
  47. 1:29is a
  48. 1:30global symbol that main label that
  49. 1:33you're seeing here
  50. 1:34i'm declaring that to be a global symbol
  51. 1:35other people can know about
  52. 1:37okay i've got my main label
  53. 1:40as i have with most functions if you're
  54. 1:42going to make any function call from
  55. 1:44there and then return
  56. 1:45i've got to be able to make room for ra
  57. 1:48and maybe other arguments
  58. 1:49so here i am i'm going to reduce the
  59. 1:51stack pointer by
  60. 1:52four bytes to allocate some stuff there
  61. 1:56now first thing i always do is i store
  62. 1:58ra
  63. 1:59into sp12 the top guy the top of that
  64. 2:02frame that's my
  65. 2:03frame i'm going to store ra there so if
  66. 2:05i make a function call i'd be able to
  67. 2:06restore that and get back to where i was
  68. 2:08that's pretty cool now i've got
  69. 2:12two strings i'm going to reference if
  70. 2:13you remember this line let's go back
  71. 2:15i've got two strings here's two strings
  72. 2:16i need to be set up
  73. 2:18these two strings need to be set up as
  74. 2:20arguments to
  75. 2:21printf so how do i set those up well
  76. 2:24let's compute the address of string one
  77. 2:26here's string one down here
  78. 2:27here's string one down here here's
  79. 2:29string one dot here's string two here
  80. 2:32notice the the formatting string is in
  81. 2:34is in string one okay
  82. 2:36so i say louis add-i
  83. 2:39i get the high 20 bits put them in the
  84. 2:42upper
  85. 2:42immediate area and then i add i the low
  86. 2:4512 bits and i put that in there okay
  87. 2:47then i do the same thing here so this is
  88. 2:50in some sense
  89. 2:51this is going to be my load address
  90. 2:54of string 1 and string 2 into a0 and a1
  91. 2:56because remember
  92. 2:57i want to call printf printf has two
  93. 2:59arguments a0 and a1
  94. 3:02so here it is call printf i've set up a0
  95. 3:05and a1 i call printf
  96. 3:08now i now i just i'm done so now i
  97. 3:11restore this restore ra fix the stack
  98. 3:14pointer
  99. 3:16load into a0 my return value i put that
  100. 3:18zero in here and i say return
  101. 3:21it's pretty clean dot section r0 data
  102. 3:24says i'm going to be in a read-only data
  103. 3:26area from here on
  104. 3:27in okay so r0data says read are not zero
  105. 3:30o r o means read only data i'm saying
  106. 3:33this is going to be
  107. 3:35i'm gonna i'm aligning this to four
  108. 3:36bytes this is a little bit different
  109. 3:38than this align this says
  110. 3:39how many bytes i want to align the
  111. 3:40following and i align these based on
  112. 3:43a word aligned and now here below here
  113. 3:45is my data okay
  114. 3:46that's it now you're gonna say am i done
  115. 3:49well that's the
  116. 3:50that s file is there more work to do
  117. 3:52with the next stage well let's see what
  118. 3:53we've got
  119. 3:54let's see if the next stage is we
  120. 3:56assemble this down
  121. 3:58here's my main code okay starts at zero
  122. 4:01every every single guy starts at zero
  123. 4:03this is a file by itself so i start this
  124. 4:05at zero and then eventually
  125. 4:06the final resting place has a different
  126. 4:08value but i'm starting here from zero
  127. 4:10and now this is the this is assuming
  128. 4:12that this is like
  129. 4:13the first instruction this is four that
  130. 4:15means that's going to be this
  131. 4:16instruction okay so it's basically
  132. 4:18saying these are the addresses of what
  133. 4:19these guys are
  134. 4:20and this is just the file so in this
  135. 4:21file is just this
  136. 4:23okay just make sure you know this is all
  137. 4:24that contains in that file
  138. 4:26nothing else this is just for you to see
  139. 4:29that this is just for you to see what's
  140. 4:30happening
  141. 4:31and this is just for you to remember
  142. 4:32where things are but only this stuff
  143. 4:34here this is all that's stored in a
  144. 4:36dotto file okay does that make sense
  145. 4:38all right so remember we had to make
  146. 4:40room on the stack
  147. 4:41we to make room for four bytes on the
  148. 4:43stack i store
  149. 4:44our a now i've got my luau louis
  150. 4:48addie that's for my first string that's
  151. 4:50for my a0
  152. 4:52that's for string one here's my louis at
  153. 4:54i for string two i still got zeros here
  154. 4:56i don't know where those values are
  155. 4:58going to be so i'm just putting zeros
  156. 4:59i'm letting you know that this is what's
  157. 5:00going to be here okay
  158. 5:01and what's this one do you remember
  159. 5:02where this is right here what's this
  160. 5:04line do you guys remember what that next
  161. 5:05thing after i
  162. 5:05set up a0 and a1 what's my what's the
  163. 5:08next thing i do you're right
  164. 5:10i've got a jowl i gotta call printf
  165. 5:13this is the call to printf right here
  166. 5:15okay gotta remember what that is
  167. 5:17then i restore my stack sorry i
  168. 5:20load back ra i restore my stack i load
  169. 5:23in a0
  170. 5:24as my return value and i return okay so
  171. 5:26that's what main does
  172. 5:27and all i've got to do is figure out how
  173. 5:29to then those guys in red
  174. 5:30that's the job of the linker well done
  175. 5:35so what do i do i fill those in
  176. 5:39this is the value of string one boop
  177. 5:42this is the value not really the value
  178. 5:43but this is what has to be there in
  179. 5:45those values to be able to handle string
  180. 5:46two
  181. 5:47now a0 has some combination of this as
  182. 5:50the upper 20 and lower 12
  183. 5:51upper 20 lower 12 for string one and
  184. 5:54string two
  185. 5:54ie a0 and a1 this is where
  186. 5:58printf lives okay love that
  187. 6:02and well that's at least the uh that's
  188. 6:04at least the uh that 20 bits there for
  189. 6:06that
  190. 6:07and then that's all i need to do now
  191. 6:09you're going to say well that was great
  192. 6:11except that how did you calculate those
  193. 6:14values
  194. 6:15for this okay so the target's address
  195. 6:17for string
  196. 6:18one is 20 in hex
  197. 6:21in the upper 20 bits and a 10 in the
  198. 6:24lower 12 bits
  199. 6:25sounds good that doesn't seem crazy so
  200. 6:28here's my instruction sequence louie
  201. 6:31here's 20 here and add i the lower 12.
  202. 6:35seems reasonable there except it doesn't
  203. 6:38work
  204. 6:40why doesn't it work immediates
  205. 6:43in risk five are always sign extended
  206. 6:46we knew that but maybe we forgot about
  207. 6:48it a
  208. 6:50if you remember a is 10 10 has a 1
  209. 6:53in the upper bit so the highest the 12th
  210. 6:56bit
  211. 6:56the 11th bit by index but the 12th bit
  212. 6:59by counting from 1
  213. 7:00that's a one well when i sign extend
  214. 7:03it's going to be all ones
  215. 7:05so it looks like this i signed it to be
  216. 7:06all like this all
  217. 7:08ones see that all ones
  218. 7:12when i add this by the way what's all
  219. 7:13ones if i were to say like think about
  220. 7:14number two's complement what's all ones
  221. 7:17negative one so if i just look at the
  222. 7:19upper 20 bits
  223. 7:21and just add this value to this value
  224. 7:24just forget again i had only though
  225. 7:26those guys i don't this this is all
  226. 7:27zeros right so that's not going to
  227. 7:28affect the upper 20 bits okay a10
  228. 7:30doesn't mean the lower 20
  229. 7:31lower 12 bits so i'm fine with that but
  230. 7:33if i if i just consider those two
  231. 7:34numbers even though
  232. 7:35they're the upper 20 if i just consider
  233. 7:37them by themselves all f's
  234. 7:38which is really minus one and 20 what do
  235. 7:41you get
  236. 7:43you get one less than 20 which is one f
  237. 7:46so i'm off i'm off by exactly that one
  238. 7:49this is
  239. 7:50actually because this counts for a
  240. 7:51negative one i'm actually off by that
  241. 7:54one
  242. 7:54if that makes any sense so to fix
  243. 7:58it i take my 20 i add 1 to it
  244. 8:02to compensate for the fact that when i
  245. 8:03sign extended i'm going to lose the one
  246. 8:05and then when i add the a10 on the lower
  247. 8:0812 sign extend it remember you can only
  248. 8:10add 32 bits to 32 bits in the systems
  249. 8:12it's not like
  250. 8:12oh i only add the 12. now you gotta add
  251. 8:1432 to 32 to get 32. that's what our alus
  252. 8:16do
  253. 8:17our arithmetic logic units they only
  254. 8:18know how to add 32 to 32 to get 32.
  255. 8:20so then i'll get the right answer 28 10.
  256. 8:24so i've got to have this like
  257. 8:25compensating factor of this extra
  258. 8:28uh it's actually 2 to the 12. i got this
  259. 8:30extra 2 to the 12. that's up there
  260. 8:32to do that so what is this
  261. 8:35so by the way how do i figure out what
  262. 8:37this value is what do i figure out what
  263. 8:38this
  264. 8:38total value was this is going to be the
  265. 8:41immediate right the immediate that i put
  266. 8:42in there is going to be the a10
  267. 8:44well what is if i sign extended to all
  268. 8:46fs what does that become
  269. 8:47well if i have a number and it looks
  270. 8:49like it's negative right i have this
  271. 8:50number what
  272. 8:51is it let's figure out what it is all fs
  273. 8:53means the upper guy is a one if it's all
  274. 8:55upper guys and one
  275. 8:56two's complement is it means it's a
  276. 8:57negative number how do i figure what
  277. 8:58that value is
  278. 8:59invert the bits and add one and that's
  279. 9:01what the negative number was okay
  280. 9:03so let's take a look at this let's
  281. 9:04complement all fs and a10 well if i
  282. 9:07flip all those it becomes a zero flip a
  283. 9:10a
  284. 9:10is 10 10 remember 10 10 okay
  285. 9:1410 10 for a well the opposite of that is
  286. 9:17101. so there's a 5 there okay
  287. 9:21so that 101 becomes this 5.
  288. 9:24this one is one off of well flip that
  289. 9:27it's all ones except for the lowest guy
  290. 9:29that's an e
  291. 9:30so this guy is an e if it's a zero flip
  292. 9:33those bits it's an
  293. 9:34f 5ef is my value let's add that one now
  294. 9:37again
  295. 9:385ef remember to figure out what the
  296. 9:41two's complement is
  297. 9:42i flip it and add one so 5ef
  298. 9:45plus 1 is
  299. 9:495 5f0 0 is 15 20
  300. 9:52okay so that means this number
  301. 9:56was negative 15 20. okay
  302. 9:59so minus 15 20.
  303. 10:03so here we go instruction sequence is
  304. 10:06minus 15 20. add i and by the way
  305. 10:10let's do this here we go loop
  306. 10:14this calculates 28 10.
  307. 10:17so therefore i'll go back here so
  308. 10:19therefore
  309. 10:21what i have is this number
  310. 10:25is minus 15 20 which is what i want
  311. 10:28i want that number and i want this one
  312. 10:32to be 21. remember so the plus 1 goes up
  313. 10:36there i need to add 2 to the 12. that's
  314. 10:3821.
  315. 10:39the lower side is minus 15 20. and that
  316. 10:41calculates the address of string 1. so
  317. 10:43remember
  318. 10:44louis 21 in hex at i
  319. 10:47minus 15 20. and that's why whoops let
  320. 10:51me show you here
  321. 10:52that's why look look this is
  322. 10:55louis 21 at i
  323. 10:591520 huh okay
  324. 11:02pretty cool right in conclusion
  325. 11:07what does a compiler do it takes a
  326. 11:09high-level language
  327. 11:10here c converts it down to the assembler
  328. 11:13language
  329. 11:14which you can have different assembler
  330. 11:15languages if you changed a different isa
  331. 11:17it'd be a different assembly language
  332. 11:19risk five is different than mips okay so
  333. 11:21those are different assembly languages
  334. 11:23we're living in the risk five world now
  335. 11:25assembler removes the pseudo
  336. 11:27instructions sets up two tables
  337. 11:29a symbol table of all the symbols it
  338. 11:30knows about in that file and a
  339. 11:32relocation table
  340. 11:33of all the things that need to be fixed
  341. 11:35in the linker stage
  342. 11:36the linker goes to the symbols takes all
  343. 11:39of them in aggregate figures out are
  344. 11:40there any duplicate symbols error
  345. 11:42or any missing symbols error okay if i'm
  346. 11:45calling somebody but i have no symbols
  347. 11:46there error also once i have them
  348. 11:49once that's all covered i have to now
  349. 11:51place them all together concatenate all
  350. 11:52the text first
  351. 11:54all the text first all the data next and
  352. 11:57fixing those links i've got it now
  353. 11:59working it out out
  354. 12:00and sets whatever and makes use of
  355. 12:02whatever debugging that persists that
  356. 12:03continue there okay so whatever other
  357. 12:05header information in the edit out file
  358. 12:07done the loader takes that as we said
  359. 12:10before
  360. 12:11sets up the stack takes it and takes a
  361. 12:13disk disk file
  362. 12:14makes it make some memory for the system
  363. 12:16copies the text and data into that
  364. 12:18memory this is i'm summarizing this
  365. 12:19whole lecture in two in one minute
  366. 12:21copies all the all that text and data
  367. 12:23from edit out
  368. 12:24into that memory sets the stack pointer
  369. 12:26with any arguments on the command line
  370. 12:28sets up a0 and a1 to be the value of arc
  371. 12:31c and arc v
  372. 12:33and sets the stack pointer clears the
  373. 12:35registers and says
  374. 12:37go and watch his back happily as a as a
  375. 12:40clam as this thing continues to run and
  376. 12:42when it's all done
  377. 12:42whatever the return value was it passes
  378. 12:45it back through through the os
  379. 12:46to the top level and we're all done
  380. 12:50amazing that's the goal that's the high
  381. 12:52level picture
  382. 12:53and you now understand how compiling
  383. 12:56assembling linking and loading works in
  384. 12:58a risk five system
  385. 12:59congratulations give yourselves a hand
  386. 13:01high five
  387. 13:03well done good stuff we'll see you folks
  388. 13:05at the next module
  389. 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.