Computer Organization 33
Computer Organization 33
Computer Organization
GlennReinman 4731GBoelter Hall [email protected]
SomenotesadoptedfromBryantandOHallaron
Course Components
Lectures
Higherlevelconcepts
Discussions
Appliedconcepts,importanttoolsandskillsforlabs, clarificationoflectures,examcoverage
Labs
Theheartofthecourse Provideindepthunderstandingofanaspectofsystems Programmingandmeasurement
More Info
Web
ClasswebpagehostedbyCourseWeb Copiesoflectures,assignments,exams,solutions Forum
OfficeHours Textbook
RandalE.BryantandDavidR.OHallaron.Computer Systems:AProgrammersPerspective,2nd Edition, PrenticeHall2010.
Grading
Exams(45%)
Twoinclassexams(15%each) Final(15%) Allexamsareopenbook/opennotes.
Labs(50%)
5labs(10%each) Youmustworkaloneonalllabs
Homework(5%)
5assignments(1%each) Electronicsubmissiononly
Tentative Calendar
Week 1 2 3 4 5 6 7 8 9 10 HW#5Due HW#4Due HW#1Due HW#2Due HW#3Due M T Intro+BitsandBytes(1,2) MachineLevelRep(3) MachineLevelRep(3) MachineLevelRep(3) FloatingPoint(2) Concurrency(12+handouts) Concurrency(12+handouts) Concurrency(12+handouts) VirtualMemory(9) MIPS(handouts) W Integers(2) MachineLevelRep(3) MachineLevelRep(3) Exam#1 CodeOptimization(5) Memory(6) Exam#2 I/O(10) Linking+Exceptions(7,8) Review CUDALabDue OpenMP LabDue BufferLabDue BombLabDue DataLabDue R F
HomeworkandLabsDueviaCourseWeb by Midnight
Cheating
Whatischeating?
Sharingcode:eitherbycopying,retyping,lookingat,or supplyingacopyofafile.
WhatisNOTcheating?
Helpingothersusesystemsortools. Helpingotherswithhighleveldesignissues. Helpingothersdebugtheircode.
Penaltyforcheating:
AtthediscretionoftheAssociateDean
Lab Facilities
SEASAdministeredLinuxMachine
lnxsrv.seas.ucla.edu
Remoteaccessonly
Usessh tologinwithyourSEASaccount
PleasedirectanyaccountissuestotheSEAShelpdeskas theyaretheonlyoneswithrootaccessonthismachine
Alternatives(NotRecommended)
Youmayuseotheralternativestodevelopyourcode BUT:WewilltestontheSEASmachines
Yourcodemustworkcorrectlyonthesemachinesforcredit
Course Theme
Abstractionisgood,butdontforgetreality! Abstractionshavelimits
ThingsaremorecomplexinhardwarethantheylookinC/Java!! Bugsarehardtotrack/understandiflookingonlyfromahigh levelpointofview
Usefuloutcomes
Becomemoreeffectiveprogrammers
Abletofindandeliminatebugsefficiently Abletotuneprogramperformance
PrepareforlatersystemsclassesinCS
Compilers,OperatingSystems,Networks,ComputerArchitecture,Parallel Programming
Linker (ld)
SomenotesadoptedfromBryantandOHallaron
Evencarriesthroughinscientificnotation
1.5213X104
ImplementingElectronically
Hardtostore
ENIAC(Firstelectroniccomputer)used10vacuumtubes/digit
Hardtotransmit
Needhighprecisiontoencode10signallevelsonsinglewire
Messytoimplementdigitallogicfunctions
Addition,multiplication,etc.
Binary Representations
Base2NumberRepresentation
Represent1521310 as111011011011012 Thats1x20+0x21 +1x22 +1x23 +0x24 +
ElectronicImplementation
Easytostorewithbistable elements Reliablytransmittedonnoisyandinaccuratewires
0 3.3V 2.8V 0.5V 0.0V 1 0
Compiler+RunTimeSystemControlAllocation
Wheredifferentprogramobjectsshouldbestored Inanycase,allallocationwithinsinglevirtualaddressspace
Machine Words
MachineHasWordSize
Nominalsizeofintegervalueddata
Includingaddresses
32bitSystems(4bytes)
Limitsaddressesto4GB Becomingtoosmallformemoryintensiveapplications
64bitSystems(8bytes)
Potentiallyaddress 1.8X1019 bytes x8664machinessupport48bitaddresses:256Terabytes
Machinessupportmultipledataformats
Fractionsormultiplesofwordsize Alwaysintegralnumberofbytes
Addr = 0008 ??
Addr = 0008 ??
Addr = 0012 ??
Data Representations
SizesofCObjects(inBytes)
CDataType Typical32bit char 1 short 2 int 4 long 4 longlong 8 float 4 double 8 longdouble 8 char* 4 Oranyotherpointer
#include <stdio.h> int main() { printf ("%lu %lu %lu\n", sizeof(int), sizeof(long), sizeof(char*)); }
IA32 1 2 4 4 8 4 8 10/12 4
x8664 1 2 4 8 8 4 8 10/16 8
Byte Ordering
Howshouldbyteswithinmultibytewordbe orderedinmemory? Conventions
OlderSuns,PowerPCsareBigEndianmachines
Leastsignificantbytehashighestaddress
x86sareLittleEndianmachines
Leastsignificantbytehaslowestaddress
SPARCV9,MIPS,AlphaareBiEndianmachines
CanappeartobeconfigurableasBigorLittleEndian
Internalsmaystillbeonewayortheotherthough
01 Little Endian
23
45
67
67
45
23
01
ExampleFragment
Address 8048365: 8048366: 804836c: Instruction Code 5b 81 c3 ab 12 00 00 83 bb 28 00 00 00 00 Assembly Rendition pop %ebx add $0x12ab,%ebx cmpl $0x0,0x28(%ebx)
DecipheringNumbers
Value: 0x12ab Padto4bytes: 0x000012ab Splitintobytes: 00 00 12 ab Reverse: ab 12 00 00
Result (Linux):
int a = 15213; 0x11ffffcb8 0x11ffffcb9 0x11ffffcba 0x11ffffcbb 0x6d 0x3b 0x00 0x00
Representing Integers
int A = 15213; int B = -15213; long int C = 15213;
Linux/x86-64 A 6D 3B 00 00 Linux/x86-64 B 93 C4 FF FF Sun A 00 00 3B 6D Sun B FF FF C4 93 Decimal: 15213 Binary: Hex: Linux C 6D 3B 00 00 0011 1011 0110 1101 3 B 6 D Sun C 00 00 3B 6D
x86-64 C 6D 3B 00 00 00 00 00 00
Representing Pointers
int B = -15213; int *P = &B;
Sun P EF FF FB 2C
IA32 P D4 F8 FF BF
x86-64 P A0 FC FF FF 01 00 00 00
Representing Floats
Float F = 15213.0;
IA32/x86-64 F 00 B4 6D 46 Sun F 46 6D B4 00
IEEE Single Precision Floating Point Representation Hex: Binary: 15213: 4 6 6 D B 4 0 0 0100 0110 0110 1101 1011 0100 0000 0000 1110 1101 1011 01
Not same as integer representation, but consistent across machines Can see some relation to integer representation, but not obvious
Representing Strings
StringsinC
Representedbyarrayofcharacters EachcharacterencodedinASCIIformat
Standard7bitencodingofcharacterset Otherencodingsexist,butuncommon Character0hascode0x30
Digitihascode0x30+i
Stringshouldbenullterminated
Finalcharacter=0
Compatibility
Byteorderingnotanissue
Dataaresinglebytequantities
Textfilesgenerallyplatformindependent
Exceptfordifferentconventionsoflineterminationcharacter(s)!
Instructionsencodedasbytes
Alphas,Suns,Macsuse4byteinstructions
ReducedInstructionSetComputer(RISC)
PCsusevariablelengthinstructions
ComplexInstructionSetComputer(CISC)
Differentinstructiontypesandencodingsfordifferentmachines Mostcodenotbinarycompatible
ProgramsareByteSequencesToo!
Representing Instructions
int sum(int x, int y) { return x+y; }
Alpha sum 00 00 30 42 01 80 FA 6B Sun sum 81 C3 E0 08 90 02 00 09 PC sum 55 89 E5 8B 45 0C 03 45 08 89 EC 5D C3
Boolean Algebra
DevelopedbyGeorgeBoolein19thCentury
Algebraicrepresentationoflogic
EncodeTrueas1andFalseas0
And
A&B=1whenbothA=1andB=1
Or
A|B=1wheneitherA=1orB=1
& 0 1 0 0 0 1 0 1
| 0 1 0 0 1 1 1 1
Not
~A=1whenA=0
ExclusiveOr(Xor)
A^B=1wheneitherA=1orB=1, butnotboth
~ 0 1 1 0
^ 0 1 0 0 1 1 1 0
Bit-Level Operations in C
Operations&,|,~,^AvailableinC
Applytoanyintegraldatatype
long,int,short,char,unsigned
Viewargumentsasbitvectors Argumentsappliedbitwise
Examples(Chardatatype)
~0x41>0xBE
~010000012 > > 101111102 111111112
~0x00>0xFF
~000000002
0x69&0x55>0x41
011010012 &010101012 >010000012
0x69|0x55>0x7D
011010012 |010101012 >011111012
Examples(chardatatype)
!0x41>0x00 !0x00>0x01 !!0x41>0x01 0x69&&0x55>0x01 0x69||0x55>0x01 p&&*p (avoidsnullpointeraccess)
Shift Operations
LeftShift: x<<y
Argument x 01100010 << 3 Log. >> 2 00010000 00011000
Shiftbitvectorxleftypositions
Throwawayextrabitsonleft Fillwith0sonright
RightShift:
x>>y
Shiftbitvectorxrightypositions
Throwawayextrabitsonright
Argument x 10100010 << 3 Log. >> 2 00010000 00101000
Logicalshift
Fillwith0sonleft
Arithmeticshift
Replicatemostsignificantbitonright Usefulwithtwoscomplementintegerrepresentation
UndefinedBehavior
Shiftamount<0orwordsize
{0,2,4,6}
Operations
& | ^ ~ Intersection 01000001 Union 01111101 Symmetricdifference 00111100 Complement 10101010 {0,6} {0,2,3,4,5,6} {2,3,4,5} {1,3,5,7}
replaceByte
/* *replaceByte(x,n,c) Replacebyteninxwithc *Bytesnumberedfrom0(LSB)to3(MSB) *Examples:replaceByte(0x12345678,1,0xab)=0x1234ab78 *Youcanassume0<=n<=3and0<=c<=255 *Legalops:!~&^|+<<>> *Maxops:10 *Rating:3 */ int replaceByte(int x,int n,int c){
replaceByte
/* *replaceByte(x,n,c) Replacebyteninxwithc *Bytesnumberedfrom0(LSB)to3(MSB) *Examples:replaceByte(0x12345678,1,0xab)=0x1234ab78 *Youcanassume0<=n<=3and0<=c<=255 *Legalops:!~&^|+<<>> *Maxops:10 *Rating:3 */ int replaceByte(int x,int n,int c){ int temp=255<<(n<<3); int temp2=c<<(n<<3); return(~temp&x)|temp2; }
Boolean Algebra
Commutativity A|B=B|A A&B=B&A Associativity (A|B)|C=A|(B|C) (A&B)&C=A&(B&C) Productdistributesoversum A&(B|C)=(A&B)|(A&C) Sumandproductidentities A|0=A A&1=A Zeroisproductannihilator A&0=0 Cancellationofnegation ~(~A)=A
Integer Ring
Boolean Algebra
Integer Ring
AistrueorAistrueandBistrue=Aistrue
A|B=~(~A&~B)
AorBaretrueifandonlyifAandBarenotbothfalse
ExclusiveOrusingInclusiveOr
A^B=(~A&B)|(A&~B)
ExactlyoneofAandBistrue
A^B=(A|B)&~(A&B)
EitherAistrue,orBistrue,butnotboth
Main Points
ItsAllAboutBits&Bytes
Numbers Programs Text
DifferentMachinesFollowDifferentConventions
Wordsize Byteordering Representations
BooleanAlgebraisMathematicalBasis
Basicformencodesfalseas0,trueas1 GeneralformlikebitleveloperationsinC
Goodforrepresenting&manipulatingsets