0% found this document useful (0 votes)
127 views41 pages

Computer Organization 33

This document provides information about the CS 33 course on computer organization at UCLA including course components, grading, policies on cheating, lab facilities, and a tentative calendar. The course covers topics like bits and bytes, machine-level representation of data, floating point representation, concurrency, and virtual memory through lectures, discussions, and labs. Students will be graded based on exams, labs, and homework assignments.

Uploaded by

Gloria Chan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views41 pages

Computer Organization 33

This document provides information about the CS 33 course on computer organization at UCLA including course components, grading, policies on cheating, lab facilities, and a tentative calendar. The course covers topics like bits and bytes, machine-level representation of data, floating point representation, concurrency, and virtual memory through lectures, discussions, and labs. Students will be graded based on exams, labs, and homework assignments.

Uploaded by

Gloria Chan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

CS33:

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

The Compilation System


#include<stdio.h> int main() { printf(hello,world\n); }
printf.o

hello.c Source program (text)

Prehello.i processor (cpp)


Modified source program (text)

Compiler hello.s (cc1)


Assembly program (text)

Assembler hello.o (as)


Relocatable object programs (binary)

Linker (ld)

hello Executable object program (binary)

Bits and Bytes


Chapter2ofB&O

SomenotesadoptedfromBryantandOHallaron

Why Dont Computers Use Base 10?


Base10NumberRepresentation
Thatswhyfingersareknownasdigits Naturalrepresentationforfinancialtransactions
Floatingpointnumbercannotexactlyrepresent$1.20

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

Byte-Oriented Memory Organization


ProgramsRefertoVirtualAddresses
Conceptuallyverylargearrayofbytes Actuallyimplementedwithhierarchyofdifferentmemorytypes Systemprovidesaddressspaceprivatetoparticularprocess
Programbeingexecuted Programcanclobberitsowndata,butnotthatofothers

Compiler+RunTimeSystemControlAllocation
Wheredifferentprogramobjectsshouldbestored Inanycase,allallocationwithinsinglevirtualaddressspace

Encoding Byte Values


Byte=8bits
Binary 000000002 to Decimal: 010 to Hexadecimal 0016 to 111111112 25510 FF16
0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Base16numberrepresentation Usecharacters0to9andAtoF WriteFA1D37B16 inCas0xFA1D37B


Or0xfa1d37b

Machine Words
MachineHasWordSize
Nominalsizeofintegervalueddata
Includingaddresses

32bitSystems(4bytes)
Limitsaddressesto4GB Becomingtoosmallformemoryintensiveapplications

64bitSystems(8bytes)
Potentiallyaddress 1.8X1019 bytes x8664machinessupport48bitaddresses:256Terabytes

Machinessupportmultipledataformats
Fractionsormultiplesofwordsize Alwaysintegralnumberofbytes

Word-Oriented Memory Organization


32-bit 64-bit Words Words Bytes Addr. 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015

Wordsarechunksofbits AddressesSpecifyByte Locations


Addressoffirstbytein word Addressesofsuccessive wordsdifferby4(32bit) or8(64bit)

Addr = 0000 ?? Addr = 0000 ?? Addr = 0004 ??

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

Byte Ordering Example


BigEndian(naturallanguage,M68k,IBMPower) Leastsignificantbytehashighestaddress decreasesignificancewithincreasingaddress LittleEndian(arithmeticlanguage,x86) Leastsignificantbytehaslowestaddress increasesignificancewithincreasingaddress Example Variablex has4byterepresentation0x01234567 Addressgivenby&x is0x100
Big Endian
0x100 0x101 0x102 0x103

01 Little Endian

23

45

67

0x100 0x101 0x102 0x103

67

45

23

01

Reading Byte-Reversed Listings


Disassembly
Textrepresentationofbinarymachinecode Generatedbyprogramthatreadsthemachinecode

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

Examining Data Representations


CodetoPrintByteRepresentationofData
Castingpointertounsignedchar*createsbytearray
typedef unsigned char* pointer; void show_bytes(pointer start, int len) { int i; for (i = 0; i < len; i++) printf("0x%p\t0x%.2x\n", start+i, start[i]); printf("\n"); } Printf directives: %p: Print pointer %x: Print Hexadecimal

show_bytes Execution Example


int a = 15213; printf("int a = 15213;\n"); show_bytes((pointer) &a, sizeof(int));

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

Twos complement representation (Covered next lecture)

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

Different compilers & machines assign different locations to objects

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

char S[6] = "15213";


IA32/x86-64 S 31 35 32 31 33 00 Sun S 31 35 32 31 33 00

Stringshouldbenullterminated
Finalcharacter=0

Compatibility
Byteorderingnotanissue
Dataaresinglebytequantities

Textfilesgenerallyplatformindependent
Exceptfordifferentconventionsoflineterminationcharacter(s)!

Machine-Level Code Representation


EncodeProgramasSequenceofInstructions
Eachsimpleoperation
Arithmeticoperation Readorwritememory Conditionalbranch

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

Forthisexample,Alpha& Sunusetwo4byte instructions


Usedifferingnumbersof instructionsinothercases

PCuses7instructions withlengths1,2,and3 bytes

Different machines use totally different instructions and encodings

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

General Boolean Algebras


OperateonBitVectors
Operationsappliedbitwise
01101001 & 01010101 01000001 01000001 01101001 | 01010101 01111101 01111101 01101001 ^ 01010101 00111100 00111100 ~ 01010101 10101010 10101010

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

Contrast: Logic Operations in C


ContrasttoLogicalOperators
&&,||,!
View0asFalse AnythingnonzeroasTrue Alwaysreturn0or1 Earlytermination

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

Arith. >> 2 00011000

Shiftbitvectorxrightypositions
Throwawayextrabitsonright
Argument x 10100010 << 3 Log. >> 2 00010000 00101000

Logicalshift
Fillwith0sonleft

Arithmeticshift

Replicatemostsignificantbitonright Usefulwithtwoscomplementintegerrepresentation

Arith. >> 2 11101000

UndefinedBehavior
Shiftamount<0orwordsize

Cool Stuff with Xor


BitwiseXorisformof addition Withextraproperty thateveryvalueisits ownadditiveinverse
A^A=0
*x Begin 1 2 3 End A A^B A^B (A^B)^A = B B *y B B (A^B)^B = A A A void funny(int *x, int *y) { *x = *x ^ *y; /* #1 */ *y = *x ^ *y; /* #2 */ *x = *x ^ *y; /* #3 */ }

Representing & Manipulating Sets


Representation
Widthwbitvectorrepresentssubsetsof{0,,w1} aj =1ifj A
01101001 76543210 01010101 76543210 {0,3,5,6}

{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

A+B=B+A A*B=B*A (A+B)+C=A+(B+C) (A*B)*C=A*(B*C) A*(B+C)=A*B+B*C A+0=A A*1=A A*0=0 ( A)=A

Boolean Algebra

Integer Ring

Boolean:Sumdistributesoverproduct A|(B&C)=(A|B)&(A|C) A+(B*C) (A+B)*(B+C) Boolean:Idempotency A|A=A A+A A


AistrueorAistrue=Aistrue

A&A=A Boolean:Absorption A|(A&B)=A A&(A|B)=A Boolean:LawsofComplements A|~A=1


AistrueorAisfalse

A*A A A+(A*B) A A*(A+B) A A+A 1

AistrueorAistrueandBistrue=Aistrue

Ring:Everyelementhasadditiveinverse A|~A 0 A+A=0

Relations Between Operations


DeMorgansLaws
Express&intermsof|,andviceversa
A&B=~(~A|~B)
AandBaretrueifandonlyifneitherAnorBisfalse

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

You might also like