Carnegie Mellon - cs.cmu.edu

45
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Transcript of Carnegie Mellon - cs.cmu.edu

Page 1: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

1BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Page 2: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

2BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Bits,Bytes,andIntegers– Part2

15-213:IntroductiontoComputerSystems3rd Lecture,Sept.5,2017

Today’sInstructor:RandyBryant

Page 3: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

3BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

AssignmentAnnouncements¢ Lab0availableviacoursewebpageandAutolab.

§ DueThurs.Sept.7,11:59pm§ Nogracedays§ Nolatesubmissions§ Justdoit!

¢ Lab1availableviaTPZandAutolab§ DueThurs,Sept.14,11:55pm§ Readinstructionscarefully:writeup,bits.c,tests.c

§ Quirkysoftwareinfrastructure§ Basedonlectures2,3,and4(CS:APPChapter2)§ Aftertoday’slectureyouwillknoweverythingfortheinteger

problems§ FloatingpointcoveredThursdaySept.7

Page 4: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

4BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

SummaryFromLastLecture¢ Representinginformationasbits¢ Bit-levelmanipulations¢ Integers

§ Representation:unsignedandsigned§ Conversion,casting§ Expanding,truncating§ Addition,negation,multiplication,shifting

¢ Representationsinmemory,pointers,strings¢ Summary

Page 5: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

5BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

EncodingIntegers

Two’sComplementExamples(w=5)

B2T (X ) = -xw-1 ×2w-1 + xi ×2

i

i=0

w-2

åB2U(X ) = xi ×2i

i=0

w-1

åUnsigned Two’sComplement

SignBit

10 = -16 8 4 2 1

0 1 0 1 0

-10 = -16 8 4 2 1

1 0 1 1 0

8+2 = 10

-16+4+2 = -10

Page 6: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

6BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Unsigned&SignedNumericValues¢ Equivalence

§ Sameencodingsfornonnegativevalues

¢ Uniqueness§ Everybitpatternrepresents

uniqueintegervalue§ Eachrepresentable integerhas

uniquebitencoding

¢ Expressioncontainingsignedandunsignedint:int iscasttounsigned

X B2T(X)B2U(X)0000 00001 10010 20011 30100 40101 50110 60111 7

–88–79–610–511–412–313–214–115

10001001101010111100110111101111

01234567

Page 7: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

7BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

SignExtensionandTruncation¢ SignExtension

¢ Truncation

Page 8: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

8BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Today:Bits,Bytes,andIntegers¢ Representinginformationasbits¢ Bit-levelmanipulations¢ Integers

§ Representation:unsignedandsigned§ Conversion,casting§ Expanding,truncating§ Addition,negation,multiplication,shifting

¢ Representationsinmemory,pointers,strings¢ Summary

Page 9: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

9BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

UnsignedAddition

¢ StandardAdditionFunction§ Ignorescarryoutput

¢ ImplementsModularArithmetics = UAddw(u ,v) = u +v mod2w

• • •• • •

uv+

• • •u + v• • •

TrueSum:w+1bits

Operands:w bits

DiscardCarry:w bits UAddw(u , v)

1110 1001+ 1101 01011 1011 11101011 1110

E9+ D51BEBE

0 0 00001 1 00012 2 00103 3 00114 4 01005 5 01016 6 01107 7 01118 8 10009 9 1001A 10 1010B 11 1011C 12 1100D 13 1101E 14 1110F 15 1111

223+ 213446190

unsigned char

Page 10: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

10BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

0 2 4 6 8 10 12 140

2

46

810

1214

0

4

8

12

16

20

24

28

32

Integer Addition

Visualizing(Mathematical)IntegerAddition

¢ IntegerAddition§ 4-bitintegersu,v§ ComputetruesumAdd4(u ,v)

§ Valuesincreaselinearlywithu andv

§ Formsplanarsurface

Add4(u ,v)

u

v

Page 11: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

11BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

0 2 4 6 8 10 12 140

2

46

810

1214

0

2

4

6

8

10

12

14

16

VisualizingUnsignedAddition

¢ WrapsAround§ Iftruesum≥2w

§ Atmostonce

0

2w

2w+1

UAdd4(u ,v)

u

v

TrueSum

ModularSum

Overflow

Overflow

Page 12: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

12BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Two’sComplementAddition

¢ TAdd andUAdd haveIdenticalBit-LevelBehavior§ Signedvs.unsignedadditioninC:

int s, t, u, v;s = (int) ((unsigned) u + (unsigned) v);t = u + v

§ Willgive s == t

• • •• • •

uv+

• • •u + v• • •

TrueSum:w+1bits

Operands:w bits

DiscardCarry:w bits TAddw(u , v)

1110 1001+ 1101 01011 1011 11101011 1110

E9+ D51BEBE

-23+ -43446-66

Page 13: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

13BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

TAddOverflow

¢ Functionality§ Truesumrequiresw+1

bits§ DropoffMSB§ Treatremainingbitsas

2’scomp.integer

–2w–1

–2w

0

2w–1–1

2w–1

TrueSum

TAdd Result

1 000…0

1 011…1

0 000…0

0 100…0

0 111…1

100…0

000…0

011…1

PosOver

NegOver

Page 14: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

14BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

-8 -6 -4 -2 0 2 4 6-8

-6-4

-20

24

6

-8

-6

-4

-2

0

2

4

6

8

Visualizing2’sComplementAddition

¢ Values§ 4-bittwo’scomp.§ Rangefrom-8to+7

¢ WrapsAround§ Ifsum³ 2w–1

§ Becomesnegative§ Atmostonce

§ Ifsum<–2w–1

§ Becomespositive§ Atmostonce

TAdd4(u ,v)

u

vPosOver

NegOver

Page 15: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

15BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

CharacterizingTAdd

¢ Functionality§ Truesumrequiresw+1 bits§ DropoffMSB§ Treatremainingbitsas2’s

comp.integer

TAddw (u,v) =u + v + 2w-1 u + v < TMinwu + v TMinw £ u + v £ TMaxwu + v - 2w-1 TMaxw < u + v

ì

í ï

î ï

(NegOver)

(PosOver)

u

v

<0 >0

<0

>0

NegativeOverflow

PositiveOverflow

TAdd(u ,v)

2w

2w

Page 16: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

16BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Multiplication¢ Goal:ComputingProductofw-bitnumbersx,y

§ Eithersignedorunsigned

¢ But,exactresultscanbebiggerthanw bits§ Unsigned:upto2w bits

§ Resultrange:0≤x *y ≤(2w – 1)2 =22w – 2w+1 +1§ Two’scomplementmin(negative):Upto2w-1bits

§ Resultrange:x *y ≥(–2w–1)*(2w–1–1)=–22w–2+2w–1

§ Two’scomplementmax(positive):Upto2w bits,butonlyfor(TMinw)2

§ Resultrange:x *y ≤(–2w–1)2 =22w–2

¢ So,maintainingexactresults…§ wouldneedtokeepexpandingwordsizewitheachproductcomputed§ isdoneinsoftware,ifneeded

§ e.g.,by“arbitraryprecision”arithmeticpackages

Page 17: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

17BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

UnsignedMultiplicationinC

¢ StandardMultiplicationFunction§ Ignoreshighorderw bits

¢ ImplementsModularArithmeticUMultw(u ,v)= u ·v mod2w

• • •• • •

uv*

• • •u · v• • •

TrueProduct:2*w bits

Operands:w bits

Discardw bits:w bitsUMultw(u , v)

• • •

1110 1001* 1101 01011100 0001 1101 1101

1101 1101

E9* D5C1DDDD

223* 21347499221

Page 18: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

18BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

SignedMultiplicationinC

¢ StandardMultiplicationFunction§ Ignoreshighorderw bits§ Someofwhicharedifferentforsigned

vs.unsignedmultiplication§ Lowerbitsarethesame

• • •• • •

uv*

• • •u · v• • •

TrueProduct:2*w bits

Operands:w bits

Discardw bits:w bitsTMultw(u , v)

• • •

-23* -43

989-35

1110 1001* 1101 01010000 0011 1101 1101

1101 1101

E9* D503DDDD

Page 19: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

19BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Power-of-2MultiplywithShift¢ Operation

§ u << k givesu * 2k

§ Bothsignedandunsigned

¢ Examples§ u << 3 == u * 8§ (u << 5) – (u << 3)== u * 24

§ Mostmachinesshiftandaddfasterthanmultiply§ Compilergeneratesthiscodeautomatically

• • •

0 0 1 0 0 0•••

u2k*

u · 2kTrueProduct:w+k bits

Operands:w bits

Discardk bits:w bits UMultw(u , 2k)

•••

k

• • • 0 0 0•••

TMultw(u , 2k)0 0 0••••••

Page 20: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

20BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

UnsignedPower-of-2DividewithShift¢ QuotientofUnsignedbyPowerof2

§ u >> k givesë u / 2kû§ Useslogicalshift

Division Computed Hex Binary x 15213 15213 3B 6D 00111011 01101101 x >> 1 7606.5 7606 1D B6 00011101 10110110 x >> 4 950.8125 950 03 B6 00000011 10110110 x >> 8 59.4257813 59 00 3B 00000000 00111011

0 0 1 0 0 0•••

u2k/

u / 2kDivision:

Operands:•••

k••• •••

•••0 0 0••• •••

ë u / 2k û •••Result:

.

BinaryPoint

0

0 0 0•••0

Page 21: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

21BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

SignedPower-of-2DividewithShift¢ QuotientofSignedbyPowerof2

§ x >> k givesë x / 2kû§ Usesarithmeticshift§ Roundswrongdirectionwhenu < 0

0 0 1 0 0 0•••x2k/

x / 2kDivision:

Operands:•••

k••• •••

•••0 ••• •••RoundDown(x / 2k) •••Result:

.

BinaryPoint

0 •••

Division Computed Hex Binary y -15213 -15213 C4 93 11000100 10010011 y >> 1 -7606.5 -7607 E2 49 11100010 01001001 y >> 4 -950.8125 -951 FC 49 11111100 01001001 y >> 8 -59.4257813 -60 FF C4 11111111 11000100

Page 22: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

22BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

CorrectPower-of-2Divide¢ QuotientofNegativeNumberbyPowerof2

§ Wanté x / 2kù (RoundToward0)§ Computeasë (x+2k-1)/ 2kû

§ InC:(x + (1<<k)-1) >> k§ Biasesdividendtoward0

Case1:Norounding

Divisor:

Dividend:

0 0 1 0 0 0•••

u

2k/é u / 2k ù

•••

k1 ••• 0 0 0•••

1 •••0 1 1••• .

BinaryPoint

1

0 0 0 1 1 1•••+2k –1 •••

1 1 1•••

1 ••• 1 1 1•••

Biasinghasnoeffect

Page 23: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

23BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

CorrectPower-of-2Divide(Cont.)

Divisor:

Dividend:

Case2:Rounding

0 0 1 0 0 0•••

x

2k/é x / 2k ù

•••

k1 ••• •••

1 •••0 1 1••• .

BinaryPoint

1

0 0 0 1 1 1•••+2k –1 •••

1 ••• •••

Biasingadds1tofinalresult

•••

Incrementedby1

Incrementedby1

Page 24: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

24BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Negation:Complement&Increment¢ Negatethroughcomplementandincrease

~x + 1 == -x

¢ Example§ Observation:~x + x == 1111…111 == -1

1 0 0 1 0 11 1x

0 1 1 0 1 00 0~x+

1 1 1 1 1 11 1-1

Decimal Hex Binary x 15213 3B 6D 00111011 01101101 ~x -15214 C4 92 11000100 10010010 ~x+1 -15213 C4 93 11000100 10010011 y -15213 C4 93 11000100 10010011

x=15213

Page 25: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

25BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Complement&IncrementExamples

Decimal Hex Binary x -32768 80 00 10000000 00000000 ~x 32767 7F FF 01111111 11111111 ~x+1 -32768 80 00 10000000 00000000

x=TMin

Decimal Hex Binary 0 0 00 00 00000000 00000000 ~0 -1 FF FF 11111111 11111111 ~0+1 0 00 00 00000000 00000000

x=0

Canonicalcounterexample

Page 26: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

26BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Today:Bits,Bytes,andIntegers¢ Representinginformationasbits¢ Bit-levelmanipulations¢ Integers

§ Representation:unsignedandsigned§ Conversion,casting§ Expanding,truncating§ Addition,negation,multiplication,shifting§ Summary

¢ Representationsinmemory,pointers,strings

Page 27: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

27BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Arithmetic:BasicRules¢ Addition:

§ Unsigned/signed:Normaladditionfollowedbytruncate,sameoperationonbitlevel

§ Unsigned:additionmod2w

§ Mathematicaladdition+possiblesubtractionof2w

§ Signed:modifiedadditionmod2w(resultinproperrange)§ Mathematicaladdition+possibleadditionorsubtractionof2w

¢ Multiplication:§ Unsigned/signed:Normalmultiplicationfollowedbytruncate,

sameoperationonbitlevel§ Unsigned:multiplicationmod2w

§ Signed:modifiedmultiplicationmod2w(resultinproperrange)

Page 28: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

28BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

WhyShouldIUseUnsigned?¢ Don’t usewithoutunderstandingimplications

§ Easytomakemistakesunsigned i;for (i = cnt-2; i >= 0; i--)

a[i] += a[i+1];

§ Canbeverysubtle#define DELTA sizeof(int)int i;for (i = CNT; i-DELTA >= 0; i-= DELTA)

. . .

Page 29: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

29BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

CountingDownwithUnsigned¢ Properwaytouseunsignedasloopindex

unsigned i;for (i = cnt-2; i < cnt; i--)

a[i] += a[i+1];

¢ SeeRobertSeacord,SecureCodinginCandC++§ CStandardguaranteesthatunsignedadditionwillbehavelikemodular

arithmetic§ 0– 1à UMax

¢ Evenbettersize_t i;for (i = cnt-2; i < cnt; i--)

a[i] += a[i+1];§ Datatypesize_t definedasunsignedvaluewithlength=wordsize§ Codewillworkevenif cnt =UMax§ Whatifcnt issignedand<0?

Page 30: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

30BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

WhyShouldIUseUnsigned?(cont.)¢ Do UseWhenPerformingModularArithmetic

§ Multiprecision arithmetic

¢ Do UseWhenUsingBitstoRepresentSets§ Logicalrightshift,nosignextension

¢ Do UseInSystemProgramming§ Bitmasks,devicecommands,…

Page 31: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

31BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Today:Bits,Bytes,andIntegers¢ Representinginformationasbits¢ Bit-levelmanipulations¢ Integers

§ Representation:unsignedandsigned§ Conversion,casting§ Expanding,truncating§ Addition,negation,multiplication,shifting§ Summary

¢ Representationsinmemory,pointers,strings

Page 32: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

32BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Byte-OrientedMemoryOrganization

¢ Programs referto databyaddress§ Conceptually,envisionitasaverylargearrayofbytes

§ Inreality,it’snot,butcanthinkofitthatway§ Anaddressislikeanindexintothatarray

§ and,apointervariablestoresanaddress

¢ Note:systemprovides privateaddressspacestoeach“process”§ Thinkofaprocessasaprogrambeingexecuted§ So,aprogramcanclobberitsowndata,butnotthatofothers

• • •

Page 33: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

33BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

MachineWords¢ Anygivencomputerhasa“WordSize”§ Nominalsizeofinteger-valueddata

§ andofaddresses

§ Untilrecently,mostmachinesused32bits(4bytes) aswordsize§ Limitsaddressesto4GB(232 bytes)

§ Increasingly,machineshave64-bitwordsize§ Potentially,couldhave18EB(exabytes)ofaddressablememory§ That’s18.4X1018

§ Machinesstillsupportmultipledataformats§ Fractionsormultiplesofwordsize§ Alwaysintegralnumberofbytes

Page 34: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

34BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Word-OrientedMemoryOrganization¢ AddressesSpecifyByte

Locations§ Addressoffirstbyteinword§ Addressesofsuccessivewordsdiffer

by4(32-bit)or8(64-bit)

000000010002000300040005000600070008000900100011

32-bitWords Bytes Addr.

0012001300140015

64-bitWords

Addr =??

Addr =??

Addr =??

Addr =??

Addr =??

Addr =??

0000

0004

0008

0012

0000

0008

Page 35: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

35BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

ExampleDataRepresentations

CDataType Typical32-bit Typical64-bit x86-64

char 1 1 1

short 2 2 2

int 4 4 4

long 4 8 8

float 4 4 4

double 8 8 8

pointer 4 8 8

Page 36: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

36BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

ByteOrdering¢ So,howarethebyteswithinamulti-byteword orderedin

memory?¢ Conventions§ BigEndian:Sun(OracleSPARC),PPCMac,Internet

§ Leastsignificantbytehashighestaddress§ LittleEndian:x86,ARMprocessorsrunningAndroid,iOS,andLinux

§ Leastsignificantbytehaslowestaddress

Page 37: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

37BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

ByteOrderingExample

¢ Example§ Variablex has4-byte valueof0x01234567§ Addressgivenby&x is0x100

0x100 0x101 0x102 0x103

01 23 45 67

0x100 0x101 0x102 0x103

67 45 23 01

Big Endian

Little Endian

01 23 45 67

67 45 23 01

Page 38: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

38BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

RepresentingIntegersDecimal: 15213

Binary: 0011 1011 0110 1101

Hex: 3 B 6 D

6D3B0000

IA32, x86-64

3B6D

0000

Sun

int A = 15213;

93C4FFFF

IA32, x86-64

C493

FFFF

Sun

Two’s complement representation

int B = -15213;

long int C = 15213;

00000000

6D3B0000

x86-64

3B6D

0000

Sun6D3B0000

IA32

Increasin

gad

dresses

Page 39: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

39BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

ExaminingDataRepresentations¢ CodetoPrintByteRepresentationofData§ Castingpointertounsignedchar* allowstreatmentasabytearray

Printf directives:%p: Printpointer%x: PrintHexadecimal

typedef unsigned char *pointer;

void show_bytes(pointer start, size_t len){size_t i;for (i = 0; i < len; i++)

printf(”%p\t0x%.2x\n",start+i, start[i]);printf("\n");

}

Page 40: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

40BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

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

Result (Linux x86-64):int a = 15213;0x7fffb7f71dbc 6d0x7fffb7f71dbd 3b0x7fffb7f71dbe 000x7fffb7f71dbf 00

Page 41: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

41BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

RepresentingPointers

Differentcompilers&machinesassigndifferentlocationstoobjects

Evengetdifferentresultseachtimerunprogram

int B = -15213;int *P = &B;

x86-64Sun IA32EF

FF

FB

2C

AC

28

F5

FF

3C

1B

FE

82

FD

7F

00

00

Page 42: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

42BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

char S[6] = "18213";

Representing Strings

¢ StringsinC§ Representedbyarrayofcharacters§ EachcharacterencodedinASCIIformat

§ Standard7-bitencodingofcharacterset§ Character“0”hascode0x30

– Digiti hascode0x30+i§ Stringshouldbenull-terminated

§ Finalcharacter=0

¢ Compatibility§ Byteorderingnotanissue

IA32 Sun31

38

32

31

33

00

31

38

32

31

33

00

Page 43: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

43BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Address Instruction Code Assembly Rendition8048365: 5b pop %ebx8048366: 81 c3 ab 12 00 00 add $0x12ab,%ebx804836c: 83 bb 28 00 00 00 00 cmpl $0x0,0x28(%ebx)

ReadingByte-ReversedListings¢ Disassembly§ Textrepresentationofbinarymachinecode§ Generatedbyprogramthatreadsthemachinecode

¢ ExampleFragment

¢ DecipheringNumbers§ Value: 0x12ab

§ Padto32bits: 0x000012ab

§ Splitintobytes: 00 00 12 ab

§ Reverse: ab 12 00 00

Page 44: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

44BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

IntegerCPuzzles

x < 0 Þ ((x*2) < 0)ux >= 0x & 7 == 7 Þ (x<<30) < 0ux > -1x > y Þ -x < -yx * x >= 0x > 0 && y > 0 Þ x + y > 0x >= 0 Þ -x <= 0x <= 0 Þ -x >= 0(x|-x)>>31 == -1ux >> 3 == ux/8x >> 3 == x/8x & (x-1) != 0

int x = foo();

int y = bar();

unsigned ux = x;

unsigned uy = y;

Initialization

Page 45: Carnegie Mellon - cs.cmu.edu

Carnegie Mellon

45BryantandO’Hallaron,ComputerSystems:AProgrammer’sPerspective,ThirdEdition

Summary¢ Representinginformationasbits¢ Bit-levelmanipulations¢ Integers

§ Representation:unsignedandsigned§ Conversion,casting§ Expanding,truncating§ Addition,negation,multiplication,shifting

¢ Representationsinmemory,pointers,strings¢ Summary