Assembly Language: OverviewSecond Half of the CourseGoals of Today’s LectureThree Levels of LanguagesHigh-Level LanguageAssembly LanguageMachine LanguageWhy Learn Assembly Language?Why Learn Intel IA-32 Assembly?Computer ArchitectureA Typical ComputerVon Neumann ArchitectureSlide 13Control Unit: Instruction PointerControl Unit: Instruction DecoderRegistersKeeping it Simple: All 32-bit WordsC Code vs. Assembly CodeKinds of InstructionsVariables in RegistersImmediate and Register AddressingSlide 22Slide 23Slide 24Changing Program FlowConditional and Unconditional JumpsJump and Labels: While LoopSlide 28Jump and Labels: If-Then-ElseSlide 30Making the Code More Efficient…Complete ExampleReading IA-32 Assembly LanguageConclusions1Assembly Language: OverviewProfessor Jennifer Rexfordhttp://www.cs.princeton.edu/~jrex2Second Half of the Course•Toward the hardwareComputer architectureAssembly languageMachine language•Toward the operating systemVirtual memoryDynamic memory managementProcesses and pipesSignals and system calls3Goals of Today’s Lecture•Help you learn…Basics of computer architectureRelationship between C and assembly languageIA-32 assembly language through an example•Why?Write faster code in high-level languagesUnderstand how the underlying hardware worksKnow how to write assembly code when needed4Three Levels of Languages5High-Level Language•Make programming easier by describing operations in a semi-natural language•Increase the portability of the code•One line may involve many low-level operations•Examples: C, C++, Java, Pascal, …count = 0;while (n > 1) { count++; if (n & 1) n = n*3 + 1; else n = n/2;}6Assembly Language•Tied to the specifics of the underlying machine•Commands and names to make the code readable and writeable by humans•Hand-coded assembly code may be more efficient•E.g., IA-32 from Intelmovl %edx, %eaxandl $1, %eaxje elsejmp endifelse:endif:sarl $1, %edxmovl %edx, %eaxaddl %eax, %edxaddl %eax, %edxaddl $1, %edxaddl $1, %ecxloop:cmpl $1, %edxjle endloopjmp loopendloop:movl $0, %ecx7Machine Language•Also tied to the underlying machine•What the computer sees and deals with•Every instruction is a sequence of one or more numbers•All stored in memory on the computer, and read and executed•Unreadable by humans0000 0000 0000 0000 0000 0000 0000 00000000 0000 0000 0000 0000 0000 0000 00009222 9120 1121 A120 1121 A121 7211 00000000 0001 0002 0003 0004 0005 0006 00070008 0009 000A 000B 000C 000D 000E 000F0000 0000 0000 FE10 FACE CAFE ACED CEDE1234 5678 9ABC DEF0 0000 0000 F00D 00000000 0000 EEEE 1111 EEEE 1111 0000 0000B1B2 F1F5 0000 0000 0000 0000 0000 00008Why Learn Assembly Language?•Write faster code (even in high-level language)By understanding which high-level constructs are better… in terms of how efficient they are at the machine level•Understand how things work underneathLearn the basic organization of the underlying machineLearn how the computer actually runs a programDesign better computers in the future•Some software is still written in assembly languageCode that really needs to run quicklyCode for embedded systems, network processors, etc.9Why Learn Intel IA-32 Assembly?•Program natively on our computing platformRather than using an emulator to mimic another machine•Learn instruction set for the most popular platformMost likely to work with Intel platforms in the future•But, this comes at some cost in complexityIA-32 has a large and varied set of instructionsMore instructions than are really useful in practice•Fortunately, you won’t need to use everything10Computer Architecture11A Typical Computer CPUChipsetMemoryI/O busCPU. . .NetworkROM12Von Neumann Architecture•Central Processing UnitControl unit–Fetch, decode, and execute Arithmetic and logic unit–Execution of low-level operationsGeneral-purpose registers–High-speed temporary storageData bus–Provide access to memoryRandom AccessMemory (RAM)ControlUnitALUCPURegistersData bus13Von Neumann Architecture•MemoryStore executable machine-language instructions (text section)Store data (rodata, data, bss, heap, and stack sections)Random AccessMemory (RAM)ControlUnitALUCPURegistersData busTEXTRODATADATABSSHEAPSTACK14Control Unit: Instruction Pointer•Stores the location of the next instructionAddress to use when reading machine-language instructions from memory (i.e., in the text section)•Changing the instruction pointer (EIP)Increment to go to the next instructionOr, load a new value to “jump” to a new locationEIP15Control Unit: Instruction Decoder•Determines what operations need to take placeTranslate the machine-language instruction •Control what operations are done on what dataE.g., control what data are fed to the ALUE.g., enable the ALU to do multiplication or additionE.g., read from a particular address in memoryALUsrc1 src2dstoperation flag/carryALU16Registers•Small amount of storage on the CPUCan be accessed more quickly than main memory•Instructions move data in and out of registersLoading registers from main memoryStoring registers to main memory•Instructions manipulate the register contentsRegisters essentially act as temporary variablesFor efficient manipulation of the data•Registers are the top of the memory hierarchyAhead of main memory, disk, tape, …17Keeping it Simple: All 32-bit Words•Simplifying assumption: all data in four-byte unitsMemory is 32 bits wideRegisters are 32 bits wide•In practice, can manipulate different sizes of dataEAXEBX18C Code vs. Assembly Code19Kinds of Instructions•Reading and writing datacount = 0n•Arithmetic and logic operationsIncrement: count++Multiply: n * 3Divide: n/2Logical AND: n & 1•Checking results of comparisons Is (n > 1) true or false?Is (n & 1) non-zero or zero?•Changing the flow of controlTo the end of the while loop (if “n > 1”)Back to the beginning of the loopTo the else clause (if “n & 1” is 0)count = 0;while (n > 1) { count++; if (n & 1) n = n*3 + 1; else n = n/2;}20Variables in RegistersRegistersn %edxcount %ecxReferring to a register: percent sign (“%”)count = 0;while (n > 1) { count++; if (n & 1) n = n*3 + 1; else n = n/2;}21Immediate and Register Addressingcount=0;while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2;}movl $0, %ecxaddl
View Full Document