arrays, structs, and memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf ·...

33
Arrays, Structs, and Memory 10/18/16

Upload: others

Post on 29-Aug-2019

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Arrays,Structs,andMemory

10/18/16

Page 2: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Recall:IndexedAddressingMode

• Generalform:offset(%base, %index, scale)

• Translation:Accessthememoryataddress…base + (index * scale) + offset

• Example:-0x8(%ebp, %ecx, 0x4)

Page 3: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

TranslatethisarrayaccesstoIA32

int *x;x = malloc(10*sizeof(int));

...

x[i] = -12;

Atthispoint,supposethatthevariablex isstoredat%ebp+8.Andi isin%edx.Useindexedaddressingtoassignintothearray.

Page 4: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Two-dimensionalArrays

int twodims[3][4];

twodims[1][3] = 5;

• Technicallyanarrayofarraysofints.

• “Givemethreesetsoffourintegers.”

• Howaretheseorganizedinmemory?

Page 5: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Two-dimensionalArraysint twodims[3][4];

for(i=0; i<3; i++) {

for(j=0; j<4; j++) {

twodims[i][j] = i+j;

}

}0 1 2 3

1 2 3 4

2 3 4 5

twodims[0]

twodims[1]

twodims[2]

[0][0] [0][1] [0][2] [0][3]

[1][0] [1][1] [1][2] [1][3]

[2][0] [2][1] [2][2] [2][3]

Page 6: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Two-dimensionalArrays:Matrixint twodims[3][4];

for(i=0; i<3; i++) {

for(j=0; j<4; j++) {

twodims[i][j] = i+j;

}

}0 1 2 3twodims[0]

1 2 3 4twodims[1]

2 3 4 5twodims[2]

Page 7: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

MemoryLayout

int twodims[3][4];

• Matrix:3rows,4columns0 1 2 31 2 3 42 3 4 5

twodims[1][3]:

baseaddr +rowoffset+coloffset

twodims + 1*ROWSIZE*4 + 3*4

0xf260 + 16 + 12 = 0xf27c

0xf260 0 twodim[0][0]

0xf264 1 twodim[0][1]

0xf268 2 twodim[0][2]

0xf26c 3 twodim[0][3]

0xf270 1 twodim[1][0]

0xf274 2 twodim[1][1]

0xf278 3 twodim[1][2]

0xf27c 4 twodim[1][3]

0xf280 2 twodim[2][0]

0xf284 3 twodim[2][1]

0xf288 4 twodim[2][2]

0xf28c 5 twodim[2][3]

Page 8: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

MemoryLayout

int twodims[3][4];

• Matrix:3rows,4columns0xf260 0 twodim[0][0]

0xf264 1 twodim[0][1]

0xf268 2 twodim[0][2]

0xf26c 3 twodim[0][3]

0xf270 1 twodim[1][0]

0xf274 2 twodim[1][1]

0xf278 3 twodim[1][2]

0xf27c 4 twodim[1][3]

0xf280 2 twodim[2][0]

0xf284 3 twodim[2][1]

0xf288 4 twodim[2][2]

0xf28c 5 twodim[2][3]

RowMajorOrder:allRow0buckets,followedbyallRow1buckets

0 1 2 31 2 3 42 3 4 5

Page 9: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Ifwedeclaredint matrix[5][3];,andthebaseofmatrixis0x3420,whatistheaddressofmatrix[3][2]?

A. 0x3438B. 0x3440C. 0x3444D. 0x344CE. Noneofthese

Page 10: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

2DArraysAnotherWaychar *arr[3]; // array of 3 char *’s

for(i=0; i<3; i++) {

arr[i] = malloc(sizeof(char)*5);

for(j=0; j<5; j++) {

arr[i][j] = i+j;

}

}

10

arr[0]

arr[1]

arr[2]

0 1 2 3 4

1 2 3 4 5

2 3 4 5 6

stack

Heap:eachmalloc’ed arrayof5charsiscontiguous,butthreeseparatelymalloc’ed arrays,notnecessarilyà eachhasseparatebaseaddress

Page 11: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

11

char *arr;

arr = malloc(sizeof(char)*ROWS*COLS);

for(i=0; i< ROWS; i++) {

for(j=0; j< COLS; j++) {

arr[i*COLS+j] = i+j;

}

}

arr 0 1 2 3 41 2 3 4 5

2 3 4 5 6

stack

Heap:allROW*COLSbucketsarecontiguous(allocatedbyasinglemalloc)allbucketscanbeaccessfromsinglebaseaddress(addr)

2DArraysYetAnotherWay

Page 12: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Structs• Laidoutcontiguouslybyfield

• Inorderoffielddeclaration.• Mayrequiresomepadding,foralignment.

• Struct fieldsaccessibleasabase+displacement• Compilerknows(constant)displacementofeachfield

struct student{ int age; float gpa; int id;

};

struct student s;

… Memory

0x1234 s.age

0x1238 s.gpa

0x123c s.id

Page 13: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

DataAlignment:• Where(whichaddress)canafieldbelocated?

• char(1byte):canbeallocatedatanyaddress:0x1230,0x1231,0x1232,0x1233,0x1234,…

• short(2bytes):mustbealignedon2-byteaddresses:0x1230,0x1232,0x1234,0x1236,0x1238,…

• int (4bytes):mustbealignedon4-byteaddresses:0x1230,0x1234,0x1238,0x123c,0x1240,…

Page 14: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Whydowewanttoaligndataonmultiplesofthedatasize?A. Itmakesthehardwarefaster.

B. Itmakesthehardwaresimpler.

C. Itmakesmoreefficientuseofmemoryspace.

D. ItmakesimplementingtheOSeasier.

E. Someotherreason.

Page 15: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

DataAlignment:Why?• Simplifyhardware• e.g.,onlyreadints frommultiplesof4• Don’tneedtobuildwiringtoaccess4-bytechunksatany

arbitrarylocationinhardware

• Inefficienttoload/storesinglevalueacrossalignmentboundary(1vs.2loads)

• SimplifyOS:• Preventsdatafromspanningvirtualpages• Atomicityissueswithload/storeacrossboundary

Page 16: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Structs• Laidoutcontiguouslybyfield

• Inorderoffielddeclaration.• Mayrequiresomepadding,foralignment.

• Struct fieldsaccessibleasabase+displacement• Compilerknows(constant)displacementofeachfield

struct student{ int age; float gpa; int id;

};

struct student s;

… Memory

0x1234 s.age

0x1238 s.gpa

0x123c s.id

Page 17: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Howmuchspacedoweneedtostoreoneofthesestructures?

struct student{ char name[11];short age;int id;

};

A.17bytesB.18bytesC.20bytesD.22bytesE.24bytes

Page 18: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Structs

struct student{ char name[11];short age;int id;

};

• Sizeofdata:17bytes• Sizeofstruct:20bytes

Memory …

0x1234 s.name[0]

0x1235 s.name[1]

… … …

0x123d s.name[9]

0x123e s.name[10]

0x123f

0x1240 s.age

0x1231

0x1232

0x1233

0x1234 s.ssn

0x1235

0x1236

0x1237

0x1238 …

padding

padding

Usesizeof()whenallocatingstructs withmalloc()!

Page 19: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

AlternativeLayout

struct student{int id;short age;char name[11];

};

Samefields,declaredinadifferentorder.

Page 20: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

AlternativeLayout

struct student{int id;short age;char name[11];

};

• Sizeofdata:17bytes• Sizeofstruct:17bytes!

Memory …

0x1234 s.ssn

0x1235

0x1236

0x1237

0x1238 s.age

0x1239

0x1240 s.name[0]

0x1231 s.name[1]

0x1232 s.name[2]

… … …

0x1234 s.name[9]

0x1235 s.name[10]

0x1236 …

Ingeneral,thisisn’tabigdealonaday-to-daybasis.Don’tgooutandrearrangeallyourstruct declarations.

Page 21: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Cool,sowecangetridofthispaddingbybeingsmartaboutdeclarations?• Answer:Maybe.

• Rearranginghelps,butoftenpaddingafterthestruct can’tbeeliminated.struct T1 { struct T2 {

char c1; int x;char c2; char c1;int x; char c2;

}; };

T2: x c1 c2 2bytesT1: c1 c2 2bytes x

Page 22: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

“External”Padding• ArrayofStructs

Fieldvaluesineachbucketmustbeproperlyaligned:struct T2 arr[3];

Bucketsmustbeona4-bytealignedaddress

0

x c1 c2 2bytes

1

x c1 c2 2bytes

2

x c1 c2 2bytesarr:

x x+8 x+12

Page 23: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Whichinstructionswouldyouusetoaccesstheagefieldofstudents[8]?

struct student {

int id;

short age;

char name[11];

};

struct student students[20];

students[8].age = 21;

Assumethebaseofstudentsisstoredinregister%edx.

Page 24: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

StackPadding

• Memoryalignmentapplieselsewheretoo.

void func1(){ void func2(){

int x; vs. double y;

char ch[5]; int x;

short s; short s;

double y; char ch[5];

... ...

} }

Page 25: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

WhatWe’veLearnedCS31:FirstHalf

Page 26: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

TheHardwareLevel

• BasicHardwareUnits:• Processor• Memory• I/Odevices

• Connectedbybuses.

memory

CPU I/Odevices

bus

Page 27: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

FoundationalConcepts• VonNeumannarchitecture• Programsaredata.• Programsandotherdataarestoredinmainmemory.

• Binarydatarepresentation• Dataisencodedinbinary.• Two’scomplement• ASCII• etc.

• Instructionsareencodedinbinary.• Opcode• Sourceanddestinationaddresses

Page 28: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

ArchitectureandDigitalCircuits• Circuitsarebuiltfromlogicgates.• Basicgates:AND,OR,NOT,…

• Threetypesofcircuits:• Arithmetic/Logic• Storage• Control

• TheCPUusesallthreetypesofcircuits.• Clockcycledrivesthesystem.• Oneinstructionperclockcycle.

• ISAdefineswhichoperationsareavailable.

ALU

Registers

Control

Page 29: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

AssemblyLanguage• AssemblyinstructionscorrespondcloselytoCPUoperations.• CompilerconvertsCcodetoassemblyinstructions.• Typesofinstructions:• Arithmetic/logic:ADD,OR,…• ControlFlow:JMP,CALL• DataMovement:MOV,(andfakedatamvmt:LEAL)• Stack&Functions:PUSH,POP,CALL,LEAVE,RET

• Manywaystocompilethesameprogram.• Conventionsgovernchoicesthatneedtobeconsistent.

• Locationoffunctionarguments,returnaddress,etc.

Page 30: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

CProgrammingConcepts

• Arrays,structs,andmemorylayout.

• Pointersandaddresses.

• Functioncallsandstackmemory.

• Dynamicmemoryontheheap.

Page 31: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

Someofthe(many)thingswe’veleftout...

• EElevel:wiresandtransistors.• Optimizingcircuits:timeandarea.• Example:aripplecarryadderhasalongcriticalpath;canweshortenit?

• Architecturesupportforcomplexinstructions.• OftenanassemblyinstructionrequiresmultipleCPUoperations.

• Compilerdesign.• ThecompilerautomatesCàIA32translation.Howdoesthiswork?Howcanitbemadeefficient?

Page 32: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

MidtermInfo

• ArriveearlyonThursday.Wewillstartrightat1:15.• Bringapencil.• Pleasedon’tuseapen.

• Closednotes,butyoumaybringthefollowing:• IA32cheatsheet• IA32stackdiagram

• Q&A-stylereviewsessioninlabtomorrow.• Iwillnotprepareslidesforthis.• Youneedtopreparequestionstomakethisuseful.

Page 33: Arrays, Structs, and Memory - cs.swarthmore.edubryce/cs31/f16/slides/w07a_array_struct.pdf · Assembly Language • Assembly instructions correspond closely to CPU operations. •

MidtermTips

• Don’tleavequestionsblank:apartialanswerisbetterthannone.• Ifyoudon’tunderstandaquestion,askforclarificationduringexam.• Ifyou’renotsurehowtodoproblem,moveonandcomebacklater.• Useaquestion’spointvalueasroughguideforhowmuchtimetospendonit.• Reviewyouranswersbeforeturningintheexam.• Showyourworkforpartialcredit.