Click here to load reader

COS2014 Basic Concepts Department of Computer Science Faculty of Science RU

Embed Size (px)

Citation preview

  • Slide 1
  • COS2014 Basic Concepts Department of Computer Science Faculty of Science RU.
  • Slide 2
  • Assembly Language for Intel- Based Computers, 5 th Edition Kip Irvine
  • Slide 3
  • Why study assembly language? Learn about computers Computer architecture Operating systems Data representation Hardware devices Learn about assembly languages Learn about compiling Learn how to write embedded programs Learn the assemble language for Intel 80x86
  • Slide 4
  • Welcome to Assembly Language: Definitions Assembly language: machine-specific language with a one-to-one correspondence with the machine language for that computer Machine language: The language a particular processor understands Assembler: converts programs from assembly language to machine language
  • Slide 5
  • Welcome to Assembly Language: Example Assembly Language: High-Level Language: Machine language: x = a + b; MOV AX, a ADD AX, b MOV x, AX A1 0002 06 0004 A3 0000
  • Slide 6
  • Welcome to Assembly Language: Problems with assembly language Provides no structure Is not portable Applications can be very long Hard to read and understand Lots of detail required
  • Slide 7
  • Assembly Language Machine language: Machine instructions: direct instructions to the processor, e.g. to be encoded to control the datapath A 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 instructions Understood by human
  • Slide 8
  • When you are writing a C program how does a computer look like CPU, memory, I/O, Operations on variables,
  • Slide 9
  • 8 A Model of Computer for C + if for && CPU Memory i, j, k; xfloat, yfloat; A[0], A[1], i = i + j; xfloat = 1.0; if (A[0]==0) Program Counter Typed storage
  • Slide 10
  • This model is quite different from what the hardware in a computer does when you run your program Why a C program code can run on your computer? Obviously, someone does some translation for you
  • Slide 11
  • 10 MOV AX, a ADD AX, b MUL c MOV x, AX x = (a+b) * b We have known C compiler Assembly program C program
  • Slide 12
  • We can see that Assembly Lang. is closer to real computer hardware! From the angle of Assembly Lang., how does a computer look like?
  • Slide 13
  • 12 A Model of Computer for ASM CPU Memory MOV AX, a ADD AX, b MOV x, AX 010100110010101 a 110010110001010 b 000000000010010 x AX BX JX... + - PC
  • Slide 14
  • Assembly program/machine code still have some distance to the real computer hardware. e.g. Multi-core CPU hyperthreading We need one more level of translation
  • Slide 15
  • Computer Model of a Lower Layer (from Computer Architecture textbook)
  • Slide 16
  • 15 A Layered View of Computer + if for && CPU Memory i, j, k; xfloat, yfloat; A[0], A[1], i = i + j; if (A[0]==0) Program Counter CPU Memory ADD AX, b MOV x, AX 010100110010101 a 110010110001010 b 000000000010010 x AX BX JX... + - PC i = i + j; xfloat = 1.0; if (A[0]==0) MOV AX, a ADD AX, b MOV x, AX xxxxxx xxxxx
  • Slide 17
  • MOV AX, a ADD AX, b MUL c MOV x, AX From Assembly to Binary 00000000101000010000000000011000 00000000100011100001100000100001 10001100011000100000000000000000 10001100111100100000000000000100 10101100111100100000000000000000 10101100011000100000000000000100 00000011111000000000000000001000 Assembler Assembly Machine code
  • Slide 18
  • High Level Language Program Assembly Language Program Machine Language Program Control Signal Compiler Assembler Machine Interpretation temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; MOVAX,a ADDAX,b MOVx, AX 0000 1001 1100 0110 1010 1111 0101 1000 1010 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]
  • 37 Signed Integers The highest bit indicates the sign 1 = negative, 0 = positive If the highest digit of a hexadecimal integer is > 7, the value is negative. Examples: 8A, C5, A2, 9D
  • Slide 39
  • 38 Forming Two's Complement Negative numbers are stored in two's complement notation Complement (reverse) each bit Add 1 Note that 00000001 + 11111111 = 00000000 Why?
  • Slide 40
  • Binary Addition Starting with the LSB, add each pair of digits, include the carry if present.
  • Slide 41
  • Hexadecimal Integers All values in memory are stored in binary. Because long binary numbers are hard to read, we use hexadecimal representation.
  • Slide 42
  • Translating Binary to Hexadecimal Each hexadecimal digit corresponds to 4 binary bits. Example: Translate the binary integer 000101101010011110010100 to hexadecimal:
  • Slide 43
  • Converting Hexadecimal to Decimal Multiply each digit by its corresponding power of 16: dec = (D 3 16 3 ) + (D 2 16 2 ) + (D 1 16 1 ) + (D 0 16 0 ) Hex 1234 equals (1 16 3 ) + (2 16 2 ) + (3 16 1 ) + (4 16 0 ), or decimal 4,660. Hex 3BA4 equals (3 16 3 ) + (11 * 16 2 ) + (10 16 1 ) + (4 16 0 ), or decimal 15,268.
  • Slide 44
  • Data representation: Number systems (bases) Number systems used Binary: 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 binary ASCII octal ASCII decimal ASCII hexadecimal
  • Slide 45
  • Data representation: Hex Addition & Multiplication Hex addition and multiplication tables are large We can still do simple calculations by hand B852h 23Ah + 5A65h * 100h (Your turn) ABCh 2B3h + E F 0 h * 102h
  • Slide 46
  • Data representation: Converting to decimal 12345 = 1 * 10 4 + 2 * 10 3 + 3 * 10 2 + 4 * 10 1 + 5*10 0 (Human) conversions: hex to decimal ABCDh = 10*16 3 + 11*16 2 + 12 *16 1 + 13 *16 0 = 10*4096 + 11*256 + 12*16 + 13 = 40960 + 2816 + 192 + 13 = 43981
  • Slide 47
  • Data representation: Converting to decimal (Human) conversions to decimal ABCDh = (((10*16+11)*16+12)*16+13 = 43981 (easier on calculator)
  • Slide 48
  • Data representation: Your Turn: Conversion problems 111010b = ________ 10 1234 base 5 or (1234) 5 = _________ 10
  • Slide 49
  • Data representation: Conversion from decimal (Human) conversions from decimal 2748 10 = ??? In hex 2748 = 171 * 16 + 12 171 = 10 * 16 + 11 10 = 0 * 16 + 10 so value is ABCh 2748 = 171 * 16 +12 = (10*16 + 11) * 16 + 12 = 10 * 16 2 + 11 * 16 1 + 12 * 16 0 = ABCh How do we know this is the Hex representation?
  • Slide 50
  • Data representation: Your Turn: Conversion problems Write decimal 58 in binary. Write decimal 194 in base 5
  • Slide 51
  • Learn How To Do the Following: Form the two's complement of a hexadecimal integer Convert signed binary to decimal Convert signed decimal to binary Convert signed decimal to hexadecimal Convert signed hexadecimal to decimal
  • Slide 52
  • Ranges of Signed Integers The 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?
  • Slide 53
  • Character Storage Character sets Standard ASCII(0 127) Extended ASCII (0 255) ANSI (0 255) Unicode (0 65,535) Null-terminated String Array of characters followed by a null byte Using the ASCII table back inside cover of book
  • Slide 54
  • Numeric Data Representation pure binary can be calculated directly ASCII binary string of digits: "01010101" ASCII decimal string of digits: "65" ASCII hexadecimal string of digits: "9C"
  • Slide 55
  • Character Representation ASCII (Table of ASCII Codes)Table 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 communication 32 to 126 are printable (see the last page in textbook)
  • Slide 56
  • ASCII Character Codes CHAR 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
  • Slide 57
  • Binary Data Decimal, hex & character representations are easier for humans to understand; however All the data in the computer is binary An int is typically 32 binary digits int y = 5; (y = 0x00000005;) In computer y = 00000000 00000000 00000000 00000101 int z = -5; (y = 0xFFFFFFFB;) In computer, z = 11111111 11111111 11111111 11111011
  • Slide 58
  • Binary Data A char is typically 8 binary digits char x = '5'; (or char x = 53, or char x = 0x35) In computer, x = 00110101 char x = 5; (or char x = 0x05;) In computer, x = 00000101 Note that the ASCII character 5 has a different binary value than the numeral 5 Also note that 1 ASCII character = 2 hex numbers
  • Slide 59
  • Storage Size Terminology Byte 8 bits (basic storage size for all data) Word 16 bits (2 bytes) Doubleword 32 bits (4 bytes) Quadword 64 bits (8 bytes)