Call Girls Dubai &ubble O525547819 Call Girls In Dubai Blastcum
Hennch2nts 160526114335-160526184116-160527121537
1. Ch.2 Hennessy 1
Ch.2 (Henn): Instructions – Language of the Computer
Supplement(s): HennessyChapter 2 Supplement, Hennessy Chapter 2 – Formulae, Examples and Exercises
Objectives:
Introduction
Operations and operands of the Computer Hardware,
Signed and Unsigned Numbers
Representing Instructions in the Computer
Logical Operations, Instructions for Making Decisions
Supporting Procedures in Computer Hardware
MIPS Addressing for 32-Bit Immediates and Addresses
Parallelism and Instructions: Synchronization
Translating and Starting a Program
A C Sort Example to Put It All Together
Arrays versus Pointers
Real Stuff: ARM Instructions
Real Stuff: x86 Instructions
Fallacies and Pitfalls
Concluding Remarks
To commanda computer’s hardware, you must speakits language. The words of a computer’s language are calledinstructions, andits
vocabularyis calledan instructionset. In this chapter, you will see the instruction set of a real computer, both in the form written bypeople
and inthe form read bythe computer. We introduce instructions ina top-downfashion. Startingfrom a notationthat looks like a restricted
programminglanguage, we refine it step-by-stepuntil you see the real language ofa real computer. Chapter 3 continues our downward
descent, unveilingthe hardware for arithmetic andthe representation offloating-point numbers.
You might thinkthat the languagesof computers wouldbe as diverse as those ofpeople, but inrealitycomputer languages are quite similar,
more like regional dialects thanlike independent languages. Hence, once you learnone, it is easyto pickupothers. This similarityoccurs
because all computers are constructedfromhardware technologies based onsimilar underlying principles andbecause there are a few basic
operations that allcomputers must provide. Moreover, computer designers have a common goal:to finda language that makesit easyto
buildthe hardware andthe compiler while maximizing performance andminimizing cost andpower.
It is easyto see byformal-logicalmethods that there exist certain[instruction sets]that are in abstract adequate to control andcause the
executionof anysequence of operations . . . . The reallydecisive considerations from the present point of view, inselecting an[instruction
set], are more ofa practicalnature:simplicityof the equipment demandedbythe [instruction set], and the clarityof its a pplicationto the
actuallyimportant problems together withthe speedof its handling of those problems.— Burks,Goldstine,andvon Neumann,1947
The “simplicityof the equipment” is as valuable a considerationfor today’s computers as it was for those ofthe 1950s. The goalof this
chapter is to teach aninstructionset that follows thisadvice, showing both howit is represented in hardware andthe relationshipbetween
high-level programming languages andthis more primitive one. Our examplesare inthe Cprogramming language; Section2.15 on the CD
shows how these wouldchange for an object-oriented language like Java.
By learninghowto represent instructions, you will alsodiscover the secret ofcomputing:the stored-program concept (The ideathat
instructions and data of manytypes can be storedin memoryas numbers, leading to the storedprogram skills bywritingprograms inthe
language of the computer andrunning them onthe simulator that comeswith this book. You will alsosee the impact of programming
languages andcompiler optimizationonperformance. We conclude with a lookat the historical evolutionof instructionsets andanoverview
of other computer dialects.
The chosen instructionset comes fromMIPS Technologies, whichis anelegant example of the instructionsets designedsince the 1980s.
Later, we will take a quick lookat two other popular instructionsets. ARMis quite similar to MIPS. The other example is the popular Intel
x86.
Operations of the Computer Hardware
The MIPS assemblylanguage notation add a, b, c instructs a computer to add the twovariables b and c andto put their sumin a. This
notationis rigidinthat each MIPS arithmetic instructionperforms onlyone operationandmust always have exactlythree variables. For
example, suppose we want to place the sumof four variables b, c, d, and e intovariable a. The followingsequence ofinstructions adds the
four variables (Thus, it takesthree instructions to sumthe four variables):
2. Ch.2 Hennessy 2
The words to the right ofthe sharpsymbol (#) on eachline above a re comments for the humanreader, andthe computer ignoresthem.
Note that unlike other programming languages, eachline ofthis language can contain at most one instruction. Another difference fromCis
that comments always terminate at the endof a line.
The natural number of operands for anoperationlike additionis three:the two numbers being addedtogether anda place to p ut the sum.
Requiring everyinstructionto have exactlythree operands, conforms to the philosophyof keeping the hardware simple: hardware for a
variable number of operands is more complicatedthanhardware for a fixednumber. This situationillustrates the first of four underlying
principles of hardware design: -> DesignPrinciple 1: Simplicity favors regularity. We can nowshow, in the twoexamples that follow, the
relationshipof programs writtenin higher-level programming languages to programs inthismore primitive notation.
Compiling Two C Assignment Statements into MIPS: This is done bythe compiler, andthe below are the CandMIPS codes:
3. Ch.2 Hennessy 3
Compiling a Complex C Assignment into MIPS: f = (g + h) – (i + j);
OPERANDS OF THE COMPUTER HARDWARE
Unlike programs inhigh-level languages, the operands ofarithmetic instructions are restricted;theymust be from a limitednumber of
speciallocations built directlyinhardware calledregisters. Registers are primitives usedin hardware design. The size of a register in the MIPS
architecture is 32 bits;groups of32 bits occur sofrequentlythat theyare giventhe name word in the MIPSarchitecture. A word is the
natural unit of access in a computer, usuallya groupof 32 bits;corresponds to the size ofa register in the MIPS architecture.
One major difference betweenthe variablesof a programming language and registers is the limited number of registers, typically32 on
current computers, like MIPS. In thissection, we have addedthe restrictionthat the three operands ofMIPS arithmetic instructions must
each be chosen from one of the 32 32-bit registers.
The reason for the limit of32 registers maybe foundinthe secondof our four underlying designprinciples ofhardware technology: Design
Principle 2 – Smalleris faster. A very large number of registers mayincrease the clockcycle time simplybecause it takes electronic signals
longer whentheymust travel farther. In this case, the designer must balance the craving ofprograms for more registers with the
designer’s desire to keep the clock cycle fast. Another reason for not usingmore than32 is the number ofbits it wouldtake inthe
instruction format, as Section2.5 demonstrates.
Chapter 4 shows the central role that registers playinhardware construction;as we shall see inthischapter, effective use ofregisters is
critical to programperformance. Although we couldsimplywrite instructions usingnumbers for registers, from 0 to 31, the MIPS convention
is to use two-character names following a dollar signto represent a register. Section2.8 willexplainthe reasons behindthese names. For
now, we will use $s0, $s1, . . . for registers that correspondto variablesinCandJava programs and$t0, $t1, . . . for temp oraryregisters
neededto compile the programintoMIPS instructions.
Compiling a CAssignment Using Registers: It is the compiler’s job to associate programvariableswith registers. Take, for instance, the
assignment statement fromour earlier example: f = (g + h) – (i + j);The variables f, g, h, i, andj are assignedto the registers $s0, $s1, $s2,
$s3, and $s4, respectively. The compiledMIPS program is verysimilar to the prior example, except we replace the variableswiththe register
names mentioned above plus two temporaryregisters, $t0 and$t1, which correspondto the temporaryvariables above.
Memory Operands: Programming languages have simple variables that containsingle data elements, as inthese examples, but theyalso
have more complex data structures—arrays andstructures. These complex data structures cancontainmanymore data elements than there
are registers in a computer. The processor can keeponlya smallamount of data inregisters, but computer memorycontains billions of data
elements. Hence, data structures (arrays andstructures) are kept in memory.
As explained above, arithmetic operations occur onlyon
registers inMIPS instructions;thus, MIPS must include instructions
that transfer data betweenmemoryandregisters. Such instructions
are calleddata transfer instructions. To accessa word in memory,
the instruction must supplythe memoryaddress. Memoryis just a
large, single-dimensionalarray, with the addressacting as the index
to that array, startingat 0. (MIPS usesbyte-addressing, not word-
addressing). The data transfer instruction that copiesdata from
memoryto a register is traditionallycalledload. The format of the
loadinstruction is the name of the operation followedbythe
register to be loaded, thena constant and register usedto access
memory. The sumof the constant portionof the instructionand
the contents ofthe secondregister forms the memoryaddress. The
actual MIPSname for this instruction is lw, standingfor load word.
E.g.:
lw $t0,8($s3) # Temporary reg $t0 gets A[8]
In addition to associatingvariableswithregisters, the
compiler allocates data structures like arrays andstructures to
4. Ch.2 Hennessy 4
locations in memory. The compiler canthenplace the proper starting address into the data transfer instructions.
The address ofa wordmatches the address of one of the 4 bytes withinthe word, andaddresses ofsequential words differ by4.InMIPS,
words must start at addresses that are multiples of 4. This requirement is called analignment restriction(alignment restriction is a
requirement that data be alignedinmemoryon natural boundaries), andmanyarchitectures have it. Chapter 4 suggests whyalignment
leads to faster data transfers.)
Computers divide intothose that use the addressof the leftmost or “big end” byte as the word addressversus those that use the rightmost
or “little end” byte. MIPS is inthe big-endian camp. (Appendix B, shows the two options to number bytes in a word.) Byte addressing also
affects the arrayindex. To get the proper byte address inthe code above, the offset to be added to the base register $s3 must be 4 × 8, or
32, so that the load addresswill select A[8] andnot A[8/4]. (See the relatedpitfall onpage 175 of Section 2.18.)
The instruction complementaryto load is traditionallycalledstore; it copiesdata froma register to memory. The format of a store is similar
to that of a load:the name ofthe operation, followed bythe register to be stored, thenoffset to select the arrayelement, and finallythe
base register. Once again, the MIPS addressis specified inpart bya constant and inpart bythe contents of a register. The actual MIPS name
is sw, standing for store word.
Manyprograms have more variablesthan computers
have registers. Consequently, the compiler tries to keepthe most
frequentlyusedvariablesinregisters andplaces the rest in
memory, using loads andstores to move variablesbetween
registers andmemory. The process of putting less commonlyused
variables (or those neededlater) intomemoryis called spilling
registers.
Data accesses are faster ifdata is inregisters insteadof
memory. Moreover, data is more useful whenina register. A MIPS
arithmetic instructioncan read tworegisters, operate on them, and
write the result. A MIPS data transfer instructiononlyreads one
operandor writesone operand, without operating onit. Thus,
registers take less time to access andhave higher throughput than
memory, makingdata in registers bothfaster to accessandsimpler
to use. Accessing registers alsouseslessenergythan accessing
memory. To achieve highest performance and conserve energy,
compilers must use registers efficiently.
Constant or Immediate Operands: Manytimes a programwill use a constant inan operation—for example, incrementinganindex to point
to the next element of anarray. Infact, more than half ofthe MIPSarithmetic instructions have a constant as anoperandwhenrunning th e
SPEC CPU2006 benchmarks. Using onlythe instructions we have seensofar, we would have to loada constant from memoryto use one.
(The constants would have beenplacedin memorywhen the programwas loaded.) For example, to addthe constant 4 to register $s3, we
could use the code
lw $t0, AddrConstant4($s1) # $t0 = constant 4
add $s3,$s3,$t0 # $s3 = $s3 + $t0 ($t0 == 4) assumingthat $s1 + AddrConstant4 is the memoryaddressof the constant 4.
An alternative that avoids the load instructionis to offer versions of the arithmetic instructions in whichone operand is a constant. This quick
add instructionwithone constant operand is calledaddimmediate or addi. To add 4 to register $s3, we just write addi $s3,$s3,4 # $s3 =
$s3 + 4
Immediate instructions illustrate the thirdhardware design principle, DesignPrinciple 3: Make the common case fast.
Registerzero: The constant zerohas another role, whichis to simplifythe instructionset byoffering useful variations. For example, the move
operationis just an add instructionwhere one operandis zero. Hence, MIPSdedicates a register $zero to be hardwired to the value zero. (As
you might expect, it is register number 0.)
Although the MIPSregisters inthisbook are 32 bits wide, there is a 64-bit version of the MIPSinstruction set with 32 64-bit registers. To keep
them straight, theyare officiallycalledMIPS-32 and MIPS-64. Inthis chapter, we use a subset of MIPS-32.
Since MIPSsupports negative constants, there is noneedfor subtract immediate in MIPS.
5. Ch.2 Hennessy 5
ASSOCIATED CONTENT
To increase portability, Java was originallyenvisionedas relying ona software interpreter. The instruction set of thisinterpreter is calledJava
bytecodes (see Section2.15 on the CD), whichis quite different fromthe MIPS instruction set. To get performance close to the equivalent C
program, Java systems todaytypicallycompile Java bytecodes into the native instructionsets like MIPS. Because thiscompilationis normally
done muchlater thanfor Cprograms, suchJava compilers are oftencalled Just I nTime (JIT) compilers. Section2.12 shows howJITs are used
later than Ccompilers inthe start-up process, andSection 2.13 shows the performance consequences ofcompiling versus interpreting Java
programs.
The register inthe data transfer instructions was originallyinventedto hold anindex ofanarraywith the offset used for the starting address
of an array. Thus, the base register is also calledthe index register. Today’s memoriesare muchlarger andthe software model of data
allocationis more sophisticated, sothe base addressof the arrayis normallypassed ina register since it won’t fit inthe offset, as we shall
see.