csci 304 computer organiza6on
play

CSCI 304: Computer Organiza6on Spring 2017, TR 11:00-12:20pm - PowerPoint PPT Presentation

CSCI 304: Computer Organiza6on Spring 2017, TR 11:00-12:20pm Bin Ren Assistant Professor in CS 1 CSCI304 = Alice in Wonderland 2 Introduc6on


  1. CSCI ¡304: ¡Computer ¡Organiza6on ¡ Spring ¡2017, ¡TR ¡11:00-­‑12:20pm ¡ ¡ Bin ¡Ren ¡ Assistant ¡Professor ¡in ¡CS ¡ 1 ¡

  2. CSCI304 ¡= ¡Alice ¡in ¡Wonderland ¡ 2 ¡

  3. Introduc6on ¡ • Websites ¡ – Personal: ¡ ¡ • h@p://www.cs.wm.edu/~bren/ ¡ – Course: ¡ ¡ • h@p://www.cs.wm.edu/~bren/cs304sp17/coursehome.html ¡ • Discussion ¡Group ¡ – blackboard ¡system ¡ • Grade ¡PosNng ¡ 3 ¡

  4. Introduc6on ¡ • Syllabus ¡ – Course ¡DescripNon ¡ – Pre-­‑requisites ¡ – ObjecNves ¡ – Textbook ¡ – Grading ¡Policy ¡ ¡ – Academic ¡Honor ¡ • Pre-­‑Lab ¡ – cs ¡account ¡ – grading ¡inquiry ¡code ¡ 4 ¡

  5. Why ¡C? ¡ • Age ¡has ¡its ¡advantages ¡ – C ¡has ¡been ¡around ¡for ¡~40 ¡years ¡ • Easy ¡to ¡understand ¡ – C ¡is ¡a ¡great ¡language ¡for ¡expressing ¡common ¡ideas ¡in ¡ programming ¡in ¡a ¡way ¡that ¡most ¡people ¡are ¡comfortable ¡ with ¡(procedural ¡language) ¡ • Reasonably ¡close ¡to ¡the ¡machine ¡ – Low-­‑level ¡access ¡to ¡memory ¡ ¡ – Provide ¡language ¡constructs ¡that ¡map ¡efficiently ¡to ¡ machine ¡instrucNons ¡ – Requires ¡minimal ¡run-­‑Nme ¡support ¡ * ¡C ¡has ¡the ¡best ¡combina6on ¡of ¡speed, ¡low ¡memory ¡use, ¡ low-­‑level ¡access ¡to ¡the ¡hardware, ¡and ¡popularity ¡ ¡ FYI: ¡Comparing ¡Languages: ¡h@p://www.cprogramming.com/langs.html ¡ 5 ¡

  6. OK, ¡really… ¡why ¡C? ¡ Is ¡there ¡a ¡size ¡problem? ¡ ¡ • – Size ¡is ¡part ¡of ¡the ¡issue, ¡but ¡so ¡is ¡speed. ¡ ¡ Pros. ¡ – C ¡is ¡lightweight ¡and ¡fast. ¡ Powerful ¡ • – To ¡opNmize ¡ – Write ¡drivers ¡ – Get ¡a ¡job ¡in ¡micro ¡processing ¡technology ¡ – Write ¡my ¡own ¡OS ¡ ¡ I ¡hate ¡garbage ¡ • – No ¡garbage ¡collecNon ¡ – Fun ¡memory ¡leaks ¡to ¡debug ¡ Cons. ¡ Wonderfully, ¡yet ¡irritaNngly, ¡obedient ¡ ¡ • – you ¡type ¡something ¡incorrectly, ¡and ¡it ¡has ¡a ¡way ¡of ¡ compiling ¡fine ¡and ¡just ¡doing ¡something ¡you ¡don't ¡ expect ¡at ¡run-­‑Nme. ¡ 6 ¡

  7. Welcome ¡to ¡C ¡ • Going ¡from ¡Python ¡to ¡C ¡is ¡like ¡going ¡from ¡an ¡ automaNc ¡transmission ¡to ¡a ¡sNck ¡shid ¡ – Lower ¡level: ¡much ¡more ¡is ¡led ¡for ¡you ¡to ¡do ¡ – Unsafe: ¡you ¡can ¡set ¡your ¡computer ¡on ¡fire ¡ – C ¡standard ¡library: ¡is ¡much ¡smaller ¡ – Different ¡syntaxes, ¡and ¡structured ¡vs. ¡script ¡ – Not ¡object ¡oriented: ¡paradigm ¡shid ¡ 7 ¡

  8. Programming ¡in ¡C ¡ • C ¡is ¡procedural, ¡not ¡object-­‑oriented ¡ • C ¡is ¡fully ¡compiled ¡(to ¡machine ¡code) ¡ • C ¡allows ¡direct ¡manipulaNon ¡of ¡memory ¡via ¡pointers ¡ • C ¡does ¡not ¡have ¡garbage ¡collecNon ¡ • C ¡has ¡many ¡important, ¡yet ¡subtle, ¡details ¡ 8 ¡

  9. Your ¡first ¡C ¡program ¡ #include ¡<stdio.h> ¡ #include ¡<stdio.h> ¡ ¡ void ¡main(void) ¡ ¡ int ¡main(void) ¡{ ¡ ¡ { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡prinU("Hello, ¡world!\n"); ¡ ¡ ¡ ¡ ¡ ¡ ¡prinU(“Hello, ¡world!\n”); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡(0); ¡} ¡ } ¡ #include ¡<stdio.h> ¡ ¡ main() ¡{ ¡ ¡ #include ¡<stdio.h> ¡ Which ¡one ¡is ¡best? ¡ ¡ ¡ ¡ ¡ ¡ ¡prinU("Hello, ¡world!\n"); ¡ ¡ void ¡main(void) ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡0; ¡} ¡ ¡ ¡ ¡ ¡ ¡prinU(“Hello, ¡ ¡“); ¡ ¡ #include ¡<stdio.h> ¡ ¡ ¡ ¡ ¡ ¡ ¡prinU(“world!”); ¡ int ¡main(void) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡prinU(“\n”); ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡prinU("Hello, ¡world!\n"); ¡ ¡ ¡ ¡ ¡ ¡ ¡getchar(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡0; ¡} ¡ Reminder ¡ à ¡There ¡are ¡a ¡lot ¡of ¡different ¡ways ¡to ¡solve ¡the ¡same ¡problem. ¡ TO-­‑DO: ¡Experiment ¡with ¡leaving ¡out ¡parts ¡of ¡the ¡program, ¡to ¡see ¡what ¡error ¡ messages ¡you ¡get. ¡ 9 ¡

  10. Your ¡first ¡C ¡program ¡(cont) ¡ • What ¡is ¡going ¡on? ¡ – #include ¡<stdio.h> ¡-­‑ ¡Tells ¡the ¡compiler ¡to ¡include ¡this ¡ header ¡file ¡for ¡compilaNon. ¡To ¡access ¡the ¡standard ¡ funcNons ¡that ¡comes ¡with ¡your ¡compiler, ¡you ¡need ¡to ¡ include ¡a ¡header ¡with ¡the ¡#include ¡direcNve. ¡ – main() ¡-­‑ ¡This ¡is ¡a ¡funcNon, ¡in ¡parNcular ¡the ¡main ¡block. ¡ – { ¡} ¡-­‑ ¡These ¡curly ¡braces ¡are ¡equivalent ¡to ¡staNng ¡"block ¡ begin" ¡and ¡"block ¡end". ¡ ¡The ¡code ¡in ¡between ¡is ¡called ¡a ¡ “block” ¡ – prinq() ¡-­‑ ¡Ah... ¡the ¡actual ¡print ¡statement. ¡Thankfully ¡we ¡ have ¡the ¡header ¡file ¡stdio.h! ¡But ¡what ¡does ¡it ¡do? ¡How ¡is ¡it ¡ defined? ¡ ¡ – return ¡0 ¡-­‑ ¡What's ¡this? ¡Every ¡funcNon ¡returns ¡a ¡value… ¡ ¡ 10 ¡

  11. Standard ¡Header ¡Files ¡ • FuncNons, ¡types ¡and ¡macros ¡of ¡the ¡standard ¡library ¡are ¡ declared ¡in ¡standard ¡headers: ¡ <assert.h> ¡ ¡<float.h> ¡ ¡<math.h> ¡ ¡<stdarg.h> ¡ ¡<stdlib.h> ¡ <ctype.h> ¡ ¡<limits.h> ¡ ¡<setjmp.h> ¡ ¡<stddef.h> ¡ ¡<string.h> ¡ ¡ <errno.h> ¡ ¡<locale.h> ¡ ¡<signal.h> ¡ ¡<stdio.h> ¡ ¡<Nme.h> ¡ • A ¡header ¡can ¡be ¡accessed ¡by ¡ – #include ¡<header> ¡ – NoNce, ¡these ¡do ¡not ¡end ¡with ¡a ¡semi-­‑colon ¡ • Headers ¡can ¡be ¡included ¡in ¡any ¡order ¡and ¡any ¡number ¡of ¡Nmes ¡ • Must ¡be ¡included ¡outside ¡of ¡any ¡external ¡declaraNon ¡or ¡ definiNon; ¡and ¡before ¡any ¡use ¡of ¡anything ¡it ¡declares ¡ • Need ¡not ¡be ¡a ¡source ¡file ¡ 11 ¡

  12. C ¡compila6on ¡model… ¡hello.c ¡to ¡hello ¡ hello.c ¡ Type ¡in ¡program ¡using ¡an ¡editor ¡of ¡ your ¡choice ¡(file.c); ¡plain ¡text ¡ %gcc ¡ ¡-­‑o ¡ ¡hello ¡ ¡hello.c ¡ .c ¡+ ¡.h ¡= ¡.i ¡which ¡is ¡the ¡“ulNmate ¡source ¡ code”? ¡i.e. ¡# ¡includes ¡expanded ¡and ¡ #defines ¡replaced ¡(-­‑E) ¡ .i ¡ à ¡.s ¡ ¡which ¡is ¡assembler ¡source ¡code ¡(-­‑S) ¡ .s ¡ à ¡.o ¡which ¡is ¡an ¡object ¡file; ¡fragments ¡of ¡ machine ¡code ¡with ¡unresolved ¡symbols ¡i.e. ¡ some ¡addresses ¡not ¡yet ¡known ¡(-­‑c). ¡ ¡ ¡ .o ¡+ ¡library ¡links ¡ à ¡a.out ¡(default ¡name); ¡ resolves ¡symbols, ¡generates ¡an ¡ executable. ¡ hello ¡ %hello ¡ 12 ¡

  13. Some ¡words ¡about ¡coding ¡style ¡ • Always ¡explicitly ¡declare ¡the ¡return ¡type ¡on ¡the ¡funcNon! ¡ If ¡you ¡don’t, ¡it ¡defaults ¡to ¡a ¡type ¡integer ¡anyway. ¡ • Replace ¡return ¡0 ¡with ¡return ¡EXIT_SUCCESS ¡(in ¡ <stdlib.h>) ¡ • What ¡about ¡ documenta6on ? ¡Comments ¡in ¡the ¡C89 ¡ standard ¡are ¡noted ¡by: ¡/* ¡*/. ¡The ¡comment ¡begins ¡with ¡/ * ¡and ¡ends ¡with ¡*/. ¡ – Comments ¡cannot ¡be ¡nested! ¡ – // ¡is ¡a ¡single ¡line ¡comment ¡i.e. ¡from ¡the ¡locaNon ¡of ¡// ¡to ¡the ¡ end ¡of ¡the ¡line ¡is ¡considered ¡a ¡comment ¡ 13 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend