DOC PREVIEW
Princeton COS 217 - Assembly Language

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Assembly LanguageGoals of Today’s LectureThree Levels of LanguagesHigh-Level LanguageSlide 5Machine LanguageWhy Learn Assembly Language?Why Learn Intel IA32 Assembly?Computer ArchitectureA Typical ComputerVon Neumann ArchitectureControl Unit: Instruction PointerControl Unit: Instruction DecoderRegistersKeeping it Simple: All 32-bit WordsC Code vs. Assembly CodeExample: Kinds of InstructionsExample: Variables in RegistersExample: Immediate and Register AddressingSlide 20Slide 21Slide 22Example: Changing Program FlowExample: Jump and LabelsSlide 25Example: Making More Efficient??Complete ExampleReading IA32 Assembly LanguageDesigning an Instruction SetHow Many Instructions to Have?CISC vs. RISCNext Time: More on Assembly Language1Assembly LanguageProfessor Jennifer RexfordCOS 2172Goals of Today’s Lecture•Assembly languageBetween high-level language and machine codeWhy learn assembly-language programming?•Computer architectureCentral Processing Unit (CPU)Fetch-decode-execute cycle•C code vs. assembly codeSmall IA32 example to illustrate the relationship to CIllustrates storing, manipulating, comparing, and jumping3Three Levels of Languages4High-Level Language•Make programming easier by describing operations in a 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;}5Assembly 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., IA32 from Intelmovl %edx, %eaxandl $1, %eaxje .elsejmp .endif.else:.endif:sarl $1, %edxmovl %edx, %eaxaddl %eax, %edxaddl %eax, %edxaddl $1, %edxaddl $1, %ecx.loop:cmpl $1, %edxjle .endloopjmp .loop.endloop:movl $0, %ecx6Machine Language•Also tied to the underlying machine•What the computer sees and deals with•Every command 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 00007Why 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 underneathLearn the basic organization of the underlying machineLearn how the computer actually runs a programDesign better computers in the future•Some software is still written in assembly languageCode that really needs to run quicklyCode for embedded systems, network processors, etc.8Why Learn Intel IA32 Assembly?•Program natively on our computing platformRather than using an emulator to mimic another machine•Learn instruction set for the most popular platformMost likely to work with Intel platforms in the future•But, this comes at some cost in complexityIA32 has a large and varied set of instructionsArguably more than are really useful in practice•Fortunately, you won’t need to use everything9Computer Architecture10A Typical Computer CPUChipsetMemoryI/O busCPU. . .NetworkROM11Von Neumann Architecture•Central Processing UnitControl unit–Fetch, decode, and execute Arithmetic and logic unit–Execution of low-level operationsGeneral-purpose registers–High-speed temporary storageData bus–Provide access to memory•MemoryStore instructionsStore dataRandom AccessMemory (RAM)ControlUnitALUCPURegistersData bus12Control Unit: Instruction Pointer•Stores the location of the next instructionAddress to use when reading from memory•Changing the instruction pointer (EIP)Increment by one to go to the next instructionOr, load a new value to “jump” to a new locationEIP13Control Unit: Instruction Decoder•Determines what operations need to take placeTranslate the machine-language instruction •Control what operations are done on what dataE.g., control what data are fed to the ALUE.g., enable the ALU to do multiplication or additionE.g., read from a particular address in memoryALU14Registers•Small amount of storage on the CPUCan be accessed more quickly than main memory•Instructions move in and out of registersLoading registers from main memoryStoring registers to main memory•Instructions manipulate the register contentsRegisters essentially act as temporary variablesFor efficient manipulation of the data•Registers are the top of the memory hierarchyAhead of cache, main memory, disk, tape, …15Keeping it Simple: All 32-bit Words•Simplifying assumption: all data in four-byte unitsMemory is 32 bits wideRegisters are 32 bits wide•In practice, can manipulate different sizes of dataEAXEBX16C Code vs. Assembly Code17Example: Kinds of Instructions•Storing values (in registers)count = 0n•Arithmetic and logic operationsIncrement: count++Multiply: n * 3Divide: n/2Logical AND: n & 1•Checking results of comparisons while (n > 1)if (n & 1)•JumpingTo the end of the while loop (if “n > 1”)Back to the beginning of the loopTo 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;}18Example: Variables 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;}19Example: Immediate and Register Addressingcount=0;while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2;}movl $0, %ecxaddl $1, %ecxReferring to a constant: dollar sign (“$”)Read directly from the instructionwritten to a register20Example: Immediate and Register Addressingcount=0;while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2;}movl %edx, %eaxandl $1, %eaxComputing intermediate value in register EAX21movl %edx, %eaxaddl %eax, %edxaddl %eax, %edxaddl $1, %edxExample: Immediate and Register Addressingcount=0;while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2;}Adding n twice is cheaper than multiplication!22sarl $1,


View Full Document

Princeton COS 217 - Assembly Language

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Assembly Language
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Assembly Language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Assembly Language 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?