Course Syllabus [1/2]
Course Syllabus [1/2]
• Instructor
• 逄愛君, acpang@csie.ntu.edu.tw
• Office Number: 417, Office Hour: 15:00~17:00 (Thursday)
• Textbook
• “Assembly Language for Intel-Based Computers,” Kip R. Irvine, Pearson Education, 4th Edition, 2002.
• Requirements
• Homework x 3 45%
• Mid-term exam 20%
• Final exam 20%
• Term project 15%
• TAs (Office Hour: 13:00~15:00, Wednesday)
• 林俊仁, jrlin@voip.csie.ntu.edu.tw, Office Number: 438
• 黃文彬, jason@voip.csie.ntu.edu.tw, Office Number: 438
• Website & BBS
Course Syllabus [2/2]
Course Syllabus [2/2]
• Basic Concepts
• IA-32 Processor Architecture
• Assembly Language Fundamentals
• Data Transfers, Addressing and Arithmetic
• Procedures
• Conditional Processing
• Integer Arithmetic
• Advanced Procedure (Stack, Recursion, …)
• Strings and Arrays
• Structures and Macros
• 32-Bit Window Programming
• High-Level Language Interface
• 16-Bit MS-DOS Programming (MS-DOS Function Calls)
Assembly Language for Intel
Assembly Language for Intel - - Based Based Computers, 4
Computers, 4
ththEdition Edition
Chapter 1: Basic Concepts
Kip R. Irvine
Chapter Overview Chapter Overview
• Welcome to Assembly Language
• Virtual Machine Concept
• Data Representation
• Boolean Operations
Welcome to Assembly Language Welcome to Assembly Language
• Assembly language is the oldest programming language.
• Of all languages, it bears the closest
resemblance to the native language of a computer.
• Direct access to a computer’s hardware
• To understand a great deal about your computer’s architecture and operating system
Some Good Questions to Ask [1/4]
Some Good Questions to Ask [1/4]
• What background should I have?
• Computer programming (C++, C#, JAVA, VB…)
• What is an assembler?
• A program that converts source-code programs from assembly language into machine language
• MASM (Microsoft Assembler), TASM (Borland Turbo Assembler)
• Linker (a companion program of Assembler) combines individual files created by an assembler into a single executable program.
• Debugger provides a way for a programmer to trace the execution of a program and examine the contents of memory.
Some Good Questions to Ask [2/4]
Some Good Questions to Ask [2/4]
• What hardware/software do I need?
• A computer with an Intel386, Intel486 or one of the Pentium processors (IA-32 processor family)
• OS: Microsoft Windows, MS-DOS, LINUX running a DOS emulator
• Editor, Assembler, Linker (Microsoft 16-bit linker: LINK.EXE, 32-bit linker: LINK32.EXE), Debugger (16-bit MS-DOS
programs: MASM CodeView, TASM Turbo Debugger. 32-bit Windows console programs: Microsoft Visual Studio –
msdev.exe)
• What types of programs will I create?
• 16-Bit Real-Address Mode: MS-DOS, DOS emulator
• 32-Bit Protected Mode: Microsoft Windows
• How does assembly language (AL) relate to machine
Some Good Questions to Ask [3/4]
Some Good Questions to Ask [3/4]
• What will I learn?
• Basic principles of computer architecture
• Basic Boolean logic
• How IA-32 processors manage memory, using real mode, protected mode and virtual mode
• How high-level language compilers (such as C++) translate statements into assembly language and native machine code
• Improvement of the machine-level debugging skills (e.g., errors due to memory allocation)
• How application programs communicate with the computer’s
operating system via interrupt handlers, system calls, and common memory areas
• How do C++ and Java relate to AL? E.g., X=(Y+4) *3
• Is AL portable?
• A language whose source program can be compiled and run on a wide variety of computer systems is said to be portable.
• AL makes no attempt to be portable.
• It is tied to a specific processor family.
mov eax, Y add eax, 4 mov ebx, 3 imul ebx mov X, eax
Some Good Questions to Ask [4/4]
Some Good Questions to Ask [4/4]
• Why learn AL?
• Embedded system programs
• Programs to be highly optimized for both space and runtime speed
• To gain an overall understanding of the interaction between the hardware, OS and application programs
• Device driver: programs that translate general
operating system commands into specific references to hardware details
• Are there any rules in AL?
• Yes, there are a few rules, mainly due to the physical limitations of the processor and its native instruction
Assembly Language Applications Assembly Language Applications
• It is rare to see large application programs written completely in assembly language because they would take too much time to write and maintain.
• AL is used to optimize certain sections of application programs for speed and to access computer
hardware.
• Some representative types of applications:
• Business application for single platform
• Hardware device driver
• Business application for multiple platforms
• Embedded systems & computer games
Comparing ASM to High
Comparing ASM to High - - Level Languages Level Languages
Virtual Machine Concept Virtual Machine Concept
• Virtual Machines
• Specific Machine Levels
Virtual Machines [1/2]
Virtual Machines [1/2]
• Virtual machine concept
• A most effective way to explain how a computer’s hardware and software are related
• In terms of programming languages
• Each computer has a native machine language (language L0) that runs directly on its hardware
• A more human-friendly language is usually constructed above machine language, called Language L1
• Programs written in L1 can run two different ways:
• Interpretation – L0 program interprets and executes L1 instructions one by one
• Translation – L1 program is completely translated into an L0 program, which then runs on the computer hardware
Virtual Machines [2/2]
Virtual Machines [2/2]
• In terms of a hypothetical computer
• VM1 can execute commands written in language L1.
• VM2 can execute commands written in language L2.
• The process can repeat until a virtual machine VMn can be designed that supports a powerful, easy-to-use
language.
• The Java programming language is based on the virtual machine concept.
• A program written in the Java language is translated by a Java compiler into Java byte code.
• Java byte code: a low-level language that is quickly executed at run time by Java virtual machine (JVM).
• The JVM has been implemented on many different computer systems, making Java programs relatively system-independent.
Specific Machine Levels Specific Machine Levels
High-Level Language
Assembly Language
Operating System
Instruction Set Architecture
Microarchitecture
Digital Logic Level 0 Level 1 Level 2 Level 3 Level 4 Level 5
Digital Logic Digital Logic
• Level 0
• CPU, constructed from digital logic gates
• System bus
• Memory
Microarchitecture Microarchitecture
• Level 1
• Interprets conventional machine instructions (Level 2)
• Executed by digital hardware (Level 0)
• A proprietary secret
• Computer Chip manufacturers do not generally make it possible for average users to write
microinstructions.
Instruction Set Architecture Instruction Set Architecture
• Level 2
• Also known as conventional machine language
• Executed by Level 1 program (microarchitecture, Level 1)
• Each machine-language instruction is executed by several microinstructions.
Operating System Operating System
• Level 3
• A Level 3 machine understands interactive commands by users to load and execute programs, display directories, …
• Provides services to Level 4 programs
• Programs translated and run at the instruction set architecture level (Level 2)
Assembly Language Assembly Language
• Level 4
• Instruction mnemonics such as ADD, SUB and MOV that are easily translated to the instruction set architecture level (Level 2)
• Interrupt calls are executed directly by the operating system (Level 3)
• Assembly language programs are usually translated (assembled) in their entirety into
machine language before they begin to execute.
High High - - Level Language Level Language
• Level 5
• Application-oriented languages (C++, C#, Virtual Basic, … )
• Programs compiled into assembly language (Level 4)
• Built-in assembly language
Data Representation Data Representation
• Binary Numbers
• Translating between binary and decimal
• Binary Addition
• Integer Storage Sizes
• Hexadecimal Integers
• Translating between decimal and hexadecimal
• Hexadecimal addition/subtraction
• Signed Integers
• Binary addition/subtraction
• Character Storage
Binary Numbers Binary Numbers
• Digits are 1 and 0
• 1 = true
• 0 = false
• MSB – most significant bit
• LSB – least significant bit
• Bit numbering:
0 15
1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 0
MSB LSB
Binary Numbers Binary Numbers
• Each digit (bit) is either 1 or 0
• Each bit represents a power of 2:
1 1 1 1 1 1 1 1
27 26 25 24 23 22 21 20
Every binary number is a sum of powers of 2
Translating Binary to Decimal Translating Binary to Decimal
Weighted positional notation represents a convenient way to calculate the decimal value of an unsigned binary
integer having n digits:
dec = (Dn-1 × 2n-1) + (Dn-2 × 2n-2) + ... + (D1 × 21) + (D0 × 20)
D = binary digit
binary 00001001 = decimal 9:
(1 × 23) + (1 × 20) = 9
Translating Unsigned Decimal to Binary Translating Unsigned Decimal to Binary
• Repeatedly divide the decimal integer by 2. Each remainder is a binary digit for the translated value:
37 = 100101
Binary Addition Binary Addition
• Starting with the LSB, add each pair of digits, include the carry if present.
0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 0
+
0 0 0 0 1 0 1 1
1
(4) (7)
(11)
carry:
0 1 2 3 4 bit position: 7 6 5
Integer Storage Sizes Integer Storage Sizes
byte 16 8
32 word
doubleword
64 quadword
Practice: What is the largest unsigned integer that may be stored in 20 bits?
Standard sizes:
Hexadecimal Integers Hexadecimal Integers
All values in memory are stored in binary. Because long binary numbers are hard to read, we use hexadecimal representation.
Translating Binary to Hexadecimal Translating Binary to Hexadecimal
• Each hexadecimal digit corresponds to 4 binary bits.
• Example: Translate the binary integer
000101101010011110010100 to hexadecimal:
Converting Hexadecimal to Decimal Converting Hexadecimal to Decimal
• Multiply 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.
Powers of 16 Powers of 16
Used when calculating hexadecimal values up to 8 digits long:
Converting Decimal to Hexadecimal Converting Decimal to Hexadecimal
decimal 422 = 1A6 hexadecimal
Hexadecimal Addition Hexadecimal Addition
• Divide the sum of two digits by the number base (16). The quotient becomes the carry value, and the remainder is the sum digit.
36 28 28 6A
42 45 58 4B
78 6D 80 B5
1 1
21 / 16 = 1, rem 5
Important skill: Programmers frequently add and subtract the addresses of variables and instructions.
Hexadecimal Subtraction Hexadecimal Subtraction
• When a borrow is required from the digit to the left, add 10h to the current digit's value:
C6 75
A2 47
24 2E
−1
10h + 5 = 15h
Practice: The address of var1 is 00400020. The address of the next
Signed Integers Signed Integers
• The highest bit indicates the sign. 1 = negative, 0 = positive
1 1 1 1 0 1 1 0
0 0 0 0 1 0 1 0
sign bit
Negative
Positive
If the highest digit of a hexadecimal integer is > 7, the value is negative. Examples: 8A, C5, A2, 9D
Forming the Two's Complement Forming the Two's Complement
• Negative numbers are stored in two's complement notation.
• Additive Inverse of any binary integer (when a number’s additive inverse is added to the number, their sum is zero).
• Steps:
• Complement (reverse) each bit
• Add 1
Binary Subtraction Binary Subtraction
• When subtracting A – B, convert B to its two's complement
• Add A to (–B)
1 1 0 0 1 1 0 0
– 0 0 1 1 + 1 1 0 1
1 0 0 1
Learn How To Do the Following:
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
Ranges of Signed Integers 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?
Character Storage Character Storage
• Character sets
• Standard ASCII (0 – 127): 7-bit integer
• “ABC123” Æ 41h, 42h, 43h, 31h, 32h, 33h
• Extended ASCII (0 – 255)
• Graphics symbols and Greek characters
• Null-terminated String
• Array of characters followed by a null byte
• Using the ASCII table
• back inside cover of book
Numeric Data Representation Numeric Data Representation
• Pure binary
• Be a number stored in memory in its raw format
• Can be calculated directly
• Stored in multiples of 8 bits
• ASCII digit string
• A string of ASCII characters
“101”
ASCII octal
“41”
ASCII hexadecimal
“65”
ASCII decimal
“01000001”
ASCII binary
Value Format
Boolean Operations Boolean Operations
• NOT
• AND
• OR
• Operator Precedence
• Truth Tables
Boolean Algebra Boolean Algebra
• Based on symbolic logic, designed by George Boole
• Boolean expressions created from:
• NOT, AND, OR
NOT NOT
• Inverts (reverses) a boolean value
• Truth table for Boolean NOT operator:
NOT
Digital gate diagram for NOT:
AND AND
• Truth table for Boolean AND operator:
AND
Digital gate diagram for AND:
OR OR
• Truth table for Boolean OR operator:
OR
Digital gate diagram for OR:
Operator Precedence Operator Precedence
• Examples showing the order of operations:
Truth Tables [1/3]
Truth Tables [1/3]
• A Boolean function has one or more Boolean inputs, and returns a single Boolean output.
• A truth table shows all the inputs and outputs of a Boolean function
Example: ¬X ∨ Y
Truth Tables [2/3]
Truth Tables [2/3]
• Example: X ∧ ¬Y
Truth Tables [3/3]
Truth Tables [3/3]
• Example: (Y ∧ S) ∨ (X ∧ ¬S)
mux X
Y
S
Z
Two-input multiplexer