where we are going (today) - university of iowa

63
Where we are going (today) Bits store whatever you want, including integers. How do we manipulate sequences of bits? Q: How do we arrange bits in the memory of the computer? (why do we care? we want the computer to store many individual numbers) A: bytes and words Q: How do we name or refer to all those individual numbers in memory? A: addresses and pointers 10110000 10110000 00001110 01000010 11110001 10110000 00001110 01000010 11110001 0 1 2 3 partway done

Upload: others

Post on 11-Dec-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Wherewearegoing(today)Bitsstorewhateveryouwant,includingintegers.Howdowemanipulatesequencesofbits?

Q:Howdowearrangebitsinthememoryofthecomputer?(whydowecare?wewantthecomputertostoremanyindividualnumbers)A:bytes andwords

Q:Howdowenameorrefertoallthoseindividualnumbersinmemory?A:addresses andpointers

10110000

10110000 00001110 01000010 11110001

10110000 00001110 01000010 11110001

0 1 2 3

partwaydone

CS2630ComputerOrganization

Meeting3:memoryorganizationandaddressesBrandonMyers

UniversityofIowa

Instructionmemory

Executionengine

Datamemory

organizesitsbitsasbytes andwords

Organizebytesintomachinewords

1A

0000000100020003000400050006000700080009000A000B000C000D000E000F

(noteaddressinhex)

32-bitwords 64-bitwords

PeerInstruction

1A

0000000100020003000400050006000700080009000A000B000C000D000E000F

(noteaddressinhex)

32-bitwords 64-bitwords

MIPShas32-bitaddresses

WhatisthelargestnumberofbytesthememoryofaMIPScomputercanhave?

Wherewearegoing(today)Bitsstorewhateveryouwant,includingintegers.Howdowemanipulatesequencesofbits?

Q:Howdowearrangebitsinthememoryofthecomputer?(whydowecare?wewantthecomputertostoremanyindividualnumbers)A:bytes andwords

Q:Howdowenameorrefertoallthoseindividualnumbersinmemory?A:addresses andpointers

10110000

10110000 00001110 01000010 11110001

10110000 00001110 01000010 11110001

0 1 2 3

done(fornow)

next

Lookingatmemoryaswords000000040008000C001000140018001C002000240028

Storingvalues

• storethevalue integer1510 ataddress 0x0008 00 00 00 0F

000000040008000C001000140018001C002000240028

Storingvalues

• storethevalue integer1510 ataddress 0x0014• storethevalue integer2010ataddress 0x0000

00 00 00 14

00 00 00 0F

000000040008000C001000140018001C002000240028

Addressescanbevalues,too!

• storethevalue integer1510 ataddress 0x0014• storethevalue integer2010 ataddress 0x0000• storethevalue address0x0014 ataddress 0x001C

00 00 00 14

00 00 00 0F

00 00 00 14

000000040008000C001000140018001C002000240028

Addressescanbevalues,too!• storethevalue integer1510 ataddress 0x0014• storethevalue integer2010 ataddress 0x0000• storethevalue address0x0014 ataddress 0x001C• wesaythewordataddress0x001Cisapointertotheintegerataddress0x0014

00 00 00 14

00 00 00 0F

00 00 00 14

000000040008000C001000140018001C002000240028

Addressescanbevalues,too!• storethevalue integer1510 ataddress 0x0014• storethevalue integer2010 ataddress 0x0000• storethevalue address0x0014 ataddress 0x001C• wesaythewordataddress0x001Cisapointertotheintegerataddress0x0014

• storethevalue address0x001C ataddress 0x0028• wesaythewordataddress0x0028 isapointer toapointer toaninteger

00 00 00 14

00 00 00 0F

00 00 00 14

00 00 00 1C

000000040008000C001000140018001C002000240028

int[] arr = new int[3];

Howarrayslookinmemory000000040008000C001000140018001C002000240028

int[] arr = new int[3];arr[0] = 13;

Howarrayslookinmemory

00 00 00 0D

000000040008000C001000140018001C002000240028

int[] arr = new int[3];arr[0] = 13;arr[1] = 10;

Howarrayslookinmemory

00 00 00 0D00 00 00 0A

000000040008000C001000140018001C002000240028

int[] arr = new int[3];arr[0] = 13;arr[1] = 10;arr[2] = 16;

Howarrayslookinmemory

00 00 00 0D00 00 00 0A00 00 00 10

000000040008000C001000140018001C002000240028

Peerinstruction• Supposeweallocatethisarray:int[] arr = new int[7];andJavadecidedtoputthefirstbyteofthearray(firstbyteofarr[0])ataddress0x04

Whatistheaddressofarr[5]?a. 0x04b. 0x14c. 0x09d. 0x54e. 0x05f. 0x18

address=baseaddress+index*element_size

Sizeofdatatypes(inbytes)Javadatatype sizein32-bit

architecturesizein64-bitarchitecture

boolean 1 1byte 1 1short 2 2int 4 4long 8 8float(later!) 4 4reference (storesamemoryaddress)

4 8

slideinspiredbyUWCSE351

Administrivia:• Allofficehours/DebugYourBrainscheduled:seesyllabus• Waitlistisat23;checkingtoseeifyoucanenrollinthealternativeECE3350asbackup(stillhas~20spots)• coursepolicysurvey• sofar:2slipdaysperassignmentisclearwinner• solet’sstartusingthatforHW1(althoughwesuggestsavingthemforlaterhomeworks)

Aside:namingpowersoftwo• Oneofourfavoriteformulas:howmanyuniquethingscanyourepresentwithNbits?2Nthings• NamingconventionsN =? name Close (butnotequal!)to

powerof10

10 Kibi (Ki) 103 orKilo (K)20 Mebi (Mi) 106orMega (M)30 Gibi (Gi) 109orGiga (G)40 Tebi (Ti) 1012orTera (T)50 Pebi (Pi) 1015orPeta (P)60 Exbi (Ei) 1018orExa (E)

Namethesenumbersofbytes:

211 bytes=_____

232 bytes=_____

259 bytes=_____

Wherewearegoing(today)Bitsstorewhateveryouwant,includingintegers.Howdowemanipulatesequencesofbits?

Q:Howdowearrangebitsinthememoryofthecomputer?(whydowecare?wewantthecomputertostoremanyindividualnumbers)A:bytes andwords

Q:Howdowenameorrefertoallthoseindividualnumbersinmemory?A:addresses andpointers

10110000

10110000 00001110 01000010 11110001

10110000 00001110 01000010 11110001

0 1 2 3

DONE(fornow)

DONE(fornow)

NEXT

Instructionmemory

Executionengine

Datamemory

candobitwiseoperations(andotherstuff)

Op1.Changingthenumberofbits• Oftenweneedtochangethenumberofbitsweareusingtostoreanumber(why?)

• 1210 using4bitsis11002• Ifwestoreitin8bits:000011002

• -510 using4bitsis:1011• Ifwestoreitin8bits:____________2

Wecallthisoperationsignextension: copytheleftmostbitin4-bitnumbertothenew4leftmostbitsinthe8-bitnumber

Op2.BitwiseoperationsLogicalAND(&)

00111010____0010

LogicalOR(|)

00111010____1011

XOR(^)

00111010____1001

negate(~)

0110____1001

Representingsetswithbits• 4-bitvectorrepresentssubsetsof{3,2,1,0}• 1meansintheset,0meansnotintheset

• Aistheset{1,0},encodedas0011• Bistheset{2,0},encodedas0101

• Setoperationsusingbitwiseoperators• A&BIntersection0001(1iff bothare1)• A|BUnion0111(1iff atleastone1)• A^Bsymmetricdifference0110(1iff exactlyone1)• ~Bcomplement1010(1iff 0)

slideinspiredbyUWCSE351

Op3.Shift• shift:movebitsleftorright

• Leftshift:6<<2

• Rightshift:6>>1

0 0 1 1 0

1 1 0 0 0

0 0 1 1 0

0 0 0 1 1

(diagramsshowthatIamstoringtheintegersusing5bits)

PeerinstructionWhatistheintegerresultfromevaluatingthisexpression?(assumeintegersare32bits)

(7<<2)&15

Let’splayacardgame• Comeupwithabinaryencodingfora52-carddeck

SlideinspiredbyCSE351Spring2016

Wewantthefollowingoperationstobeeasytoimplement• Comparetwocards,whichishighervalue?• Comparetwocards,aretheythesamesuit?

Operationsonapairofcards

Summaryandwhat’snext

Instructionmemory

Executionengine

Datamemory

TODAYorganizesitsbitsasbytes andwords

NEXTStoresprograms!

TODAYcandobitwiseoperations(andotherstuff)

Whattodonext• HW1/Quiz1dueWednesday• ReadingsuponMIPS• Introduceyourselfatofficehours/debugyourbrainonTues,Wed,Th• HW2willbeonthenexttopicofMIPSprogrammingandisoutimmediatelyafterHW1• InstallMARSontheresourcespageofthewebsiteifyouhaveextratime,you’lluseitinHW2

Wherewearegoing

Instructionsetarchitecture(e.g.,MIPS)

Compiler

Memorysystem I/OsystemProcessor

Datapath &Control

Digitallogic

translatingsourcecode(CorJava)ProgramstoassemblylanguageAndlinkingyourcodetoLibrarycode

HowthesoftwaretalksTothehardware

HowaprocessorrunsMIPSPrograms!

Howswitches(1or0)canbeusedtobuildInterestingfunctions:fromintegerarithmetictoprogrammablecomputers

Wherewearegoing

Instructionmemory

Executionengine

Datamemory

Learnwhatgetsstoredinhere

Organizebytesintomachinewords

1A

0000000100020003000400050006000700080009000A000B000C000D000E000F

(noteaddressinhex)

32-bitwords 64-bitwords

Peerinstruction:

Wehaveahypotheticalword-addressedmachinewith32-bitaddresses.Assumearrayxstartsataddress0x50.

int[] x = new int[4];

Whatistheaddressofx[3]?

a) 0x50b) 0x53c) 0x5Cd) 0x60e) 0x54

ReviewofbitwiseoperationsLogicalAND(&)

00111010____0010

LogicalOR(|)

00111010____1011

XOR(^)

00111010____1001

negate(~)

0110____1001

Shift• shift:movebitsleftorright

• Leftshift:6<<2

• Rightshift:6>>1

0 0 1 1 0

1 1 0 0 0

0 0 1 1 0

0 0 0 1 1

(diagramsshowthatIamstoringtheintegersusing5bits)

Wherewearegoing

Instructionsetarchitecture(e.g.,MIPS)

Compiler

Memorysystem I/OsystemProcessor

Datapath &Control

Digitallogic

translatingsourcecode(CorJava)ProgramstoassemblylanguageAndlinkingyourcodetoLibrarycode

HowthesoftwaretalksTothehardware

HowaprocessorrunsMIPSPrograms!

Howswitches(1or0)canbeusedtobuildInterestingfunctions:fromintegerarithmetictoprogrammablecomputers

Representationsofaprogram

Compiler

int x = arr[1];arr[2] = x + 10;

lw $t0, 4($r0)addi $t0, $t0, 10sw $t0, 8($r0)

Assembler

100011100000100000000000000001000010000100001000000000000000101010101110000010000000000000001000

Highlevellanguageprogram(humanreadable)

assemblyprogramastext(CS2630studentreadable)

assemblyprogramasbinary(machinereadable)

Languages• Highlevellanguage(HLL)programsaremachine-independent• languageisconvenienttouse• languageishaspowerfulfeatures

• Assemblylanguageprogramsarespecifictoanarchitecture• the“nativelanguage”oftheprocessor• languageisbare-bones:lacksmostofthefeaturesofHLLs

Java,C,Python,Lisp,C++,MATLAB,…

MIPS,ARM,x86,SPARC,Power,

Alpha,…

BriefhistoryofcompatibilitypreIBM360

postIBM360

Processormodel1slowprocessor,small

memory

Processormodel2mediocreprocessor,mediummemory

Processormodel3fastprocessor,large

memory

my_program.asmversion1

my_program.asmversion2

my_program.asmversion3

my_program.asm

Processor1slowprocessor,small

memory

Processor2mediocreprocessor,mediummemory

Processor3fastprocessor,large

memory

PeerinstructionIwrotemyprograminC(ahighlevellanguage)andcompiledittorunonaMIPSmachine.WhatshouldIdotogetthatprogramtorunonanx86machine?

a) rewritemyCprogramtobecompatiblewithx86b) rewritemyprograminx86assemblylanguagec) recompilemyCprogramforx86d) reassemblemyprogramusingthex86assembler

Languages• Highlevellanguage:usuallyhasvariables,objects,andarrays

• Assemblylanguage:usuallyjusthasregisters andmemory

Memory

Registers

https://library.law.uiowa.edu/history-library

https://library.law.uiowa.edu/history-library

000000040008000C001000140018001C002000240028

memory registers

Themainidea• 4categoriesofinstructions• performanoperationontworegistersandstoreresultinaregister• performanoperationononeregisterandaconstantandstoretheresultinaregister• moveavaluebetweenaregisterandmemory• determinewhichinstructiontoexecutenext

0x0000

r0 <- M[address]

M[address] <- r0

r1 <- r0 + constant

r2 <- r1 + r0Registertransferlanguage• 4categoriesofinstructions• performanoperationontworegistersandstoreresultinaregister• performanoperationononeregisterandaconstantandstoretheresultinaregister• moveavaluebetweenaregisterandmemory• determinewhichinstructiontoexecutenext

(later!)

Exampleassemblyprogram

0-14501

address0x00x40x80xC

r0

r1

r2

MEMORYREGISTERS

a = x + y - z

axyz

highlevel Peerinstruction:Writeanassemblyprogramusingasequenceofinstructions(useregistertransferlanguage)

IntroducingMIPS

MIPSregisters(32ofthem)RegisterNumber ConventionalName Usage$0 $zero hard-wiredto0

$1 $at reservedforassembler

$2- $3 $v0,$v1 returnvaluesfromfunctions

$4- $7 $a0- $a3 arguments

$8- $15 $t0- $t7 temporary registers

$16- $23 $s0- $s7 savedregisters

$24- $25 $t8- $t9 temporary registers

$26- $27 $k0- $k1 reservedforOS

$28 $gp globalpointer$29 $sp stackPointer$30 $fp framePointer$31 $ra returnAddress

MIPSinstructionformat:R-type

dst src src

dst src src

dst src src

dst Imm src

dst src Imm

IisforimmediatewhichinMIPSmeansaconstant

dst Imm src

“loadword”

“addimmediate”

“storeword”

MIPSinstructionformat:I-type

loadandstore

dst Imm src

dst Imm src

address=base+offset=src +Imm=0 + 4 = 4

address=base+offset=src +Imm=0 + 0 = 0

remember,register$0(or$zero)ishard-wiredto 0

• R-type• sll $t0,$t1,2#t0<- t1<<12(shiftleftlogical)• srl $t0,$t1,3#t0<- t1>>4(shiftrightlogical)• sra $t0,$t1,3#t0<- t1>>>4(shiftrightarithmetic)• and$t0,$t1,$t2#t00<- t1&t2(AND)• or$t0,$t1,2#t0<- t1|t2(OR)

• I-type• andi $t0,$t1,22#t0<- t1&22• ori $t0,$t1,0xFF#t0<- t1|0xFF

Logicaloperations

yes,sll andsrl areR-type;we’llsoonseewhy(hint:whatisthelargestshiftvalueneeded?)

PeerinstructionTranslatethefollowingcodeintoMIPS.

//kisanint[].Assumethefirstbyteofxisalready//storedataddress0x10k[0]=k[1]+1;

Whatarewemissing?

Makingdecisionsif (z == 0) then a = x + y else a = z

bne “branch(on)notequal”j“(unconditiona)jump”

articleAKA“Go-tostatementconsideredharmful”

Branch/jumpinstructions

src src label

src src label

“branchonnotequal”

“branchonequal”

“jump”

“jumpregister”label

src

PeerinstructionWhatvalueisstoredin$t0whentheprogramfinishes?

(numericresponse)

⁃https://youtu.be/VTS9O0CoVng?t

=34m12s

Storedprogramconcept

Instructionmemory

Executionengine

Datamemory

programrepresentedasbitsandstoredhere