COS2014 Basic Concepts

Click here to load reader

download COS2014 Basic Concepts

of 59

description

COS2014 Basic Concepts. Department of Computer Science Faculty of Science RU. Assembly Language for Intel-Based Computers, 5 th Edition. Kip Irvine. Why study assembly language?. Learn about computers Computer architecture Operating systems Data representation Hardware devices - PowerPoint PPT Presentation

Transcript of COS2014 Basic Concepts

  • COS2014Basic ConceptsDepartment of Computer ScienceFaculty of ScienceRU.

  • Assembly Language for Intel-Based Computers, 5th Edition Kip Irvine

  • Why study assembly language?Learn about computersComputer architectureOperating systemsData representationHardware devicesLearn about assembly languagesLearn about compilingLearn how to write embedded programsLearn the assemble language for Intel 80x86

  • Welcome to Assembly Language: DefinitionsAssembly language: machine-specific language with a one-to-one correspondence with the machine language for that computerMachine language: The language a particular processor understands Assembler: converts programs from assembly language to machine language

  • Welcome to Assembly Language: Example Assembly Language:

    High-Level Language:

    Machine language:

    x = a + b;MOV AX, a ADD AX, b MOV x, AXA1 0002 06 0004 A3 0000

  • Welcome to Assembly Language: Problems with assembly languageProvides no structureIs not portableApplications can be very longHard to read and understandLots of detail required

  • Assembly LanguageMachine language:Machine instructions: direct instructions to the processor, e.g. to be encoded to control the datapathA numeric language understood by the processor

    Assembly language:Statements (instructions) in short mnemonics and symbolic reference, e.g. ADD, MOV, CALL, var1, i, j, that have a 1-to-1 relationship with machine instructionsUnderstood by human

  • When you are writing a C programhow does a computer look likeCPU, memory, I/O,Operations on variables,

  • *A Model of Computer for C+iffor&&CPUMemoryi, j, k;xfloat, yfloat;A[0], A[1], i = i + j;xfloat = 1.0;if (A[0]==0)ProgramCounter

  • This model is quite different from what the hardware in a computer does when you run your programWhy a C program code can run on your computer?Obviously, someone does some translation for you

  • *MOV AX, aADD AX, bMUL cMOV x, AX

    x = (a+b) * bWe have known C compilerAssembly programC program

  • We can see that Assembly Lang. is closer to real computer hardware!From the angle of Assembly Lang., how does a computer look like?

  • *A Model of Computer for ASMCPUMemoryMOV AX, aADD AX, bMOV x, AX010100110010101a110010110001010b000000000010010xAXBXJX...+ -PC

  • Assembly program/machine code still have some distance to the real computer hardware.e.g. Multi-core CPUhyperthreadingWe need one more level of translation

  • Computer Model of a Lower Layer(from Computer Architecture textbook)

  • *A Layered View of Computeri = i + j;xfloat = 1.0;if (A[0]==0)MOV AX, aADD AX, bMOV x, AXxxxxxxxxxxx

  • MOV AX, aADD AX, bMUL cMOV x, AX

    From Assembly to Binary00000000101000010000000000011000 00000000100011100001100000100001 10001100011000100000000000000000 10001100111100100000000000000100 10101100111100100000000000000000 10101100011000100000000000000100 00000011111000000000000000001000 AssemblerAssemblyMachine code

  • High Level Language ProgramAssembly Language ProgramMachine Language ProgramControl SignalCompilerAssemblerMachine Interpretationtemp = v[k];v[k] = v[k+1];v[k+1] = temp;MOVAX,aADDAX,bMOVx, AX0000 1001 1100 0110 1010 1111 0101 10001010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 ALUOP[0:3]
  • *Whats Next?Virtual machine concept (Sec. 1-2)Data representation (Sec. 1-3)

  • *Virtual Machine ConceptPurpose of this section:Understand the role of assembly language in a computer system

    Side product:The principle of layered abstraction for combating complexities, e.g. OSI 7-layer protocol

  • *Virtual Machine ConceptA layered abstraction of computers proposed by A. TanenbaumEach layer provides an abstract computer, or virtual machine, to its upper layerVirtual machine:A hypothetical computer that can be constructed of either HW or SWWhat is a computer?

  • *Simplest Model of ComputersCompute engineMemoryInput dataOutput dataProgramInstructionsc.f., y = f(x)Layered abstraction: A computer consists of layers of such virtual machine abstractions

  • *Why Layered Abstraction?Big idea: layered abstraction to combat complexitiesA strategy of divide-and-conquerDecompose a complex system into layers with well-defined interfacesEach layer is easier to manage and handleOnly need to focus on a particular layer, e.g. to make it the bestAlso, it makes interaction clearParticularly if one layer is realized in hardware and the other in software

  • *Layered Abstraction of ComputerEach layer as a hypothetical computer, or virtual machine, that runs a programming languageCan be programmed with the programming language to process inputs and outputs

    Program written in Li can be mapped to that Li-1 by:Interpretation: Li-1 program interprets and executes Li instructions one by oneTranslation: Li program is completely translated into Li-1 program, and runs on Li-1 machine

  • *Layered Abstraction of Computeri = i + j;xfloat = 1.0;if (A[0]==0)MOV AX, aADD AX, bMOV x, AXxxxxxxxxxxxLiLi-1

  • *Languages of Different LayersEnglish: Display the sum of A times B plus C.C++: cout
  • *High-Level LanguageLevel 5Application-oriented languages, e.g., C, C++, Java, PerlWritten with certain programming model in mindVariables in storageOperators for operationsPrograms compiled into assembly language (Level 4) or interpreted by interpreters

    What kind of computer does C see?

  • *Assembly LanguageLevel 4Instruction mnemonics that have a one-to-one correspondence to machine languageBased on a view of machine: register organization, addressing, operand types and locations, functional units, Calls functions written at the OS level (Level 3)Programs are translated into machine language (Level 2)

    What kind of computer does it see?

  • *Operating SystemLevel 3Provides services to Level 4 programs as if it were a computer Programs translated and run at the instruction set architecture level (Level 2)

  • *Instruction Set ArchitectureLevel 2Known as conventional machine languageAttributes of a computer as seen by assembly programmer, i.e. conceptual structure and functional behaviorOrganization of programmable storageData types and data structuresInstruction set and formatsAddressing modes and data accessingExecuted by Level 1 program (microarchitecture)

  • *MicroarchitectureLevel 1Can be described by register transfer language (RTL)Interprets conventional machine instructions (Level 2)Executed by digital hardware (Level 0)ZNRegisterMemoryPCIRControllerALUclockControl Signals

  • *Digital LogicLevel 0CPU, constructed from digital logic gatesSystem busMemory

  • *Whats Next?Virtual machine concept (Sec. 1-2)Data representation (Sec. 1-3)

  • *Data RepresentationPurpose of this sectionAssembly program often needs to process data, and manage data storage and memory locations need to know data representation and storageBinary numbers: translating between binary and decimalBinary additionInteger storage sizesHexadecimal integers: translating between decimal and hex.; hex. subtractionSigned integers: binary subtractionCharacter storage

  • Binary NumbersDigits are 1 and 01 = true0 = falseMSB most significant bitLSB least significant bit

    Bit numbering:

  • Binary NumbersEach digit (bit) is either 1 or 0Each bit represents a power of 2:Every binary number is a sum of powers of 2

  • *Integer Storage SizesWhat is the largest unsigned integer that may be stored in 20 bits?Standard sizes:Why unsigned numbers?

  • *Signed IntegersThe highest bit indicates the sign 1 = negative, 0 = positiveIf the highest digit of a hexadecimal integer is > 7, the value is negative. Examples: 8A, C5, A2, 9D

  • *Forming Two's ComplementNegative numbers are stored in two's complement notationComplement (reverse) each bitAdd 1Note that 00000001 + 11111111 = 00000000Why?

  • Binary AdditionStarting with the LSB, add each pair of digits, include the carry if present.

  • Hexadecimal IntegersAll values in memory are stored in binary. Because long binary numbers are hard to read, we use hexadecimal representation.

  • Translating Binary to HexadecimalEach hexadecimal digit corresponds to 4 binary bits.Example: Translate the binary integer 000101101010011110010100 to hexadecimal:

  • Converting Hexadecimal to DecimalMultiply each digit by its corresponding power of 16:dec = (D3 163) + (D2 162) + (D1 161) + (D0 160)

    Hex 1234 equals (1 163) + (2 162) + (3 161) + (4 160), or decimal 4,660.

    Hex 3BA4 equals (3 163) + (11 * 162) + (10 161) + (4 160), or decimal 15,268.

  • Data representation:Number systems (bases)Number systems usedBinary: The internal representation inside the computer. Externally, they may be represented in binary, decimal, or hexadecimal.Decimal: The system people use.ASCII representations of numbers used for I/O:ASCII binaryASCII octalASCII decimalASCII hexadecimal

  • Data representation: Hex Addition & MultiplicationHex addition and multiplication tables are largeWe can still do simple calculations by hand B852h 23Ah + 5A65h * 100h (Your turn) ABCh 2B3h + E F 0 h * 102h

  • Data representation: Converting to decimal12345 = 1 * 104 + 2 * 103 + 3 * 102 + 4 * 101 + 5*100(Human) conversions: hex to decimal ABCDh = 10*163 + 11*162+ 12 *161 + 13 *160 = 10*4096 + 11*256 + 12*16 + 13 = 40960 + 2816 + 192 + 13 = 43981

  • Data representation: Converting to decimal(Human) conversions to decimal ABCDh = (((10*16+11)*16+12)*16+13 = 43981 (easier on calculator)

  • Data representation: Your Turn: Conversion problems 111010b = ________ 10 1234 base 5 or (1234)5= _________ 10

  • Data representation: Conversion from decimal(Human) conversions from decimal 274810 = ??? In hex2748 = 171 * 16 + 12

    171 = 10 * 16 + 1110 = 0 * 16 + 10 so value is ABCh2748 = 171 * 16 +12= (10*16 + 11) * 16 + 12= 10 * 162 + 11 * 161 + 12 * 160= ABChHow do we know this is the Hex representation?

  • Data representation: Your Turn: Conversion problemsWrite decimal 58 in binary.Write decimal 194 in base 5

  • Learn How To Do the Following:Form the two's complement of a hexadecimal integerConvert signed binary to decimalConvert signed decimal to binaryConvert signed decimal to hexadecimalConvert signed hexadecimal to decimal

  • Ranges of Signed IntegersThe highest bit is reserved for the sign. This limits the range:Practice: What is the largest positive value that may be stored in 20 bits?

  • Character StorageCharacter setsStandard ASCII(0 127)Extended ASCII (0 255)ANSI (0 255)Unicode (0 65,535)Null-terminated StringArray of characters followed by a null byteUsing the ASCII tableback inside cover of book

  • Numeric Data Representationpure binarycan be calculated directlyASCII binarystring of digits: "01010101"ASCII decimalstring of digits: "65"ASCII hexadecimalstring of digits: "9C"

  • Character RepresentationASCII (Table of ASCII Codes)American Standard Code for Information Interchange Standard encoding scheme used to represent characters in binary format on computers 7-bit encoding, so 128 characters can be represented 0 to 31 & 127 are "control characters" (cannot print)Ctrl-A or ^A is 1, ^B is 2, etc.Used for screen formatting & data communication32 to 126 are printable (see the last page in textbook)

  • ASCII Character CodesCHAR DECIMAL HEX BINARY

    '0'48d30h 0011 0000b'9'57d39h 0011 1001b

    'A'65d41h0100 0001b'Z'90d5Ah0101 1010b

    'a'97d61h 0110 0001b'z'122d7Ah 0111 1010b

  • Binary DataDecimal, hex & character representations are easier for humans to understand; howeverAll the data in the computer is binaryAn int is typically 32 binary digitsint y = 5; (y = 0x00000005;)In computer y = 00000000 00000000 00000000 00000101int z = -5; (y = 0xFFFFFFFB;) In computer, z = 11111111 11111111 11111111 11111011

  • Binary DataA char is typically 8 binary digitschar x = '5'; (or char x = 53, or char x = 0x35)In computer, x = 00110101char x = 5; (or char x = 0x05;)In computer, x = 00000101Note that the ASCII character 5 has a different binary value than the numeral 5Also note that 1 ASCII character = 2 hex numbers

  • Storage Size TerminologyByte 8 bits (basic storage size for all data)Word16 bits (2 bytes)Doubleword32 bits (4 bytes)Quadword64 bits (8 bytes)

    *****Input and output of a compiler****Input and output of an assembler*****************