CS 16: Assembly Language Programming for the IBM PC and Compatibles

Similar documents
Computer Science 281 Binary and Hexadecimal Review

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Number Representation

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CPEN Digital Logic Design Binary Systems

2011, The McGraw-Hill Companies, Inc. Chapter 3

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Chapter 2 Logic Gates and Introduction to Computer Architecture

Section 1.4 Place Value Systems of Numeration in Other Bases

The string of digits in the binary number system represents the quantity

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Binary Numbers. Binary Octal Hexadecimal

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Binary Representation

Chapter 2. Binary Values and Number Systems

Lecture 2. Binary and Hexadecimal Numbers

High level code and machine code

Memory Systems. Static Random Access Memory (SRAM) Cell

Numbering Systems. InThisAppendix...

MICROPROCESSOR AND MICROCOMPUTER BASICS

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Base Conversion written by Cathy Saxton

NUMBER SYSTEMS. 1.1 Introduction

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

3. Convert a number from one number system to another

PROGRAMMABLE LOGIC CONTROLLERS Unit code: A/601/1625 QCF level: 4 Credit value: 15 TUTORIAL OUTCOME 2 Part 1

Today s topics. Digital Computers. More on binary. Binary Digits (Bits)

1. True or False? A voltage level in the range 0 to 2 volts is interpreted as a binary 1.

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

MACHINE ARCHITECTURE & LANGUAGE

Chapter 1: Digital Systems and Binary Numbers

Number and codes in digital systems

MACHINE INSTRUCTIONS AND PROGRAMS

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

How To Write Portable Programs In C

1 Classical Universal Computer 3

COMBINATIONAL CIRCUITS

The Answer to the 14 Most Frequently Asked Modbus Questions

Subject knowledge requirements for entry into computer science teacher training. Expert group s recommendations

Useful Number Systems

Digital Design. Assoc. Prof. Dr. Berna Örs Yalçın

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

CS201: Architecture and Assembly Language

DNA Data and Program Representation. Alexandre David

CPU Organization and Assembly Language

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

CSI 333 Lecture 1 Number Systems

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

Management Challenge. Managing Hardware Assets. Central Processing Unit. What is a Computer System?

ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Programming Languages

SECTION C [short essay] [Not to exceed 120 words, Answer any SIX questions. Each question carries FOUR marks] 6 x 4=24 marks

Positional Numbering System

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Chapter 5 Instructor's Manual

The programming language C. sws1 1

Machine Architecture and Number Systems. Major Computer Components. Schematic Diagram of a Computer. The CPU. The Bus. Main Memory.

=

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Let s put together a Manual Processor

Levels of Programming Languages. Gerald Penn CSC 324

CHAPTER 3 Boolean Algebra and Digital Logic

Chapter 4: Computer Codes

Binary Adders: Half Adders and Full Adders

Systems I: Computer Organization and Architecture

HOMEWORK # 2 SOLUTIO

Storing Measurement Data

(Refer Slide Time: 00:01:16 min)

Programmable Logic Controllers Definition. Programmable Logic Controllers History

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

Copyright 2012 Pearson Education, Inc. Chapter 1 INTRODUCTION TO COMPUTING AND ENGINEERING PROBLEM SOLVING

CHAPTER 4 MARIE: An Introduction to a Simple Computer

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

Lecture 8: Binary Multiplication & Division

Systems I: Computer Organization and Architecture

High-speed image processing algorithms using MMX hardware

Administrative Issues

Software: Systems and Application Software

NUMBER SYSTEMS. William Stallings

Lab Experience 17. Programming Language Translation

Digital Electronics Detailed Outline

Lecture 11: Number Systems

PROGRAMMABLE LOGIC CONTROLLERS Unit code: A/601/1625 QCF level: 4 Credit value: 15 OUTCOME 3 PART 1

Chapter 7D The Java Virtual Machine

Number Conversions Dr. Sarita Agarwal (Acharya Narendra Dev College,University of Delhi)

Binary, Hexadecimal, Octal, and BCD Numbers

Appendix A Installation of Visual C++ and MASM

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

X86-64 Architecture Guide

Transcription:

CS 16: Assembly Language Programming for the IBM PC and Compatibles

First, a little about you Your name Have you ever worked with/used/played with assembly language? If so, talk about it Why are you taking this course? What are your expectations/goals from me, as well as this course? Then, a little about me Background about myself Cover the syllabus, including my expectations of you and this course Finally, any additional questions before we get started

Get a quick overview of assembly language Understand the basic concept of a virtual machine See how we represent data Take a look at Boolean operations

Some good questions to ask Assembly language applications

What background should I have? You should have programmed in at least one structured high-level language, such as Java, C, Python, or C++ You should know how to use IF statements, arrays, and functions to solve programming problems What is an assembler? An assembler is a utility program that converts source code programs from assembly language into machine language

What hardware/software do I need? You need a computer that runs a 32-bit or 64-bit version of Microsoft Windows, along with one of the recent versions of Microsoft Visual Studio What types of programs will I create? 32-Bit Protected Mode 32-bit protected mode programs run under all 32-bit versions of Microsoft Windows They are usually easier to write and understand than real-mode programs 64-Bit Mode 64-bit programs run under all 64-bit versions of Microsoft Windows. 16-Bit Real-Address Mode 16-bit programs run under 32-bit versions of Windows and on embedded systems

What will I learn? Basic principles of computer architecture as applied to x86 processors Basic Boolean logic and how it applies to programming and computer hardware How x86 processors manage memory, using protected mode and virtual mode How high-level language compilers (such as C++) translate statements from their language into assembly language and native machine code How high-level languages implement arithmetic expressions, loops, and logical structures at the machine level

What else will I learn? Data representation, including signed and unsigned integers, real numbers, and character data How to debug programs at the machine level, as the need for this skill is vital when you work in languages such as C and C++, which generate native machine code How application programs communicate with the computer s operating system via interrupt handlers and system calls How to interface assembly language code to C++ programs How to create assembly language application programs

How does assembly language (AL) relate to machine language? Machine language is a numeric language specifically understood by a computer s processor (the CPU) All x86 processors understand a common machine language Assembly language consists of statements written with short mnemonics such as ADD, MOV, SUB, and CALL Assembly language has a one-to-one relationship with machine language: Each assembly language instruction corresponds to a single machine-language instruction.

How do C++ and Java relate to AL? High-level languages such as Python, C++, and Java have a one-tomany relationship with assembly language and machine language A single statement in C++, for example, expands into multiple assembly language or machine instructions Most people cannot read raw machine code, so in this book, we examine its closest relative, assembly language Is AL portable? Assembly language is not portable because it is designed for a specific processor family There are a number of different assembly languages widely used today, each based on a processor family

Beats me In all seriousness, there are applications for assembly language Embedded programs are short programs stored in a small amount of memory in single-purpose devices Real-time applications dealing with simulation and hardware monitoring require precise timing and responses Computer game consoles require their software to be highly optimized for small code size and fast execution

There are still more applications for assembly language Assembly language helps you to gain an overall understanding of the interaction between computer hardware, operating systems, and application programs Some high-level languages abstract their data representation to the point that it becomes awkward to perform low-level tasks such as bit manipulation Device drivers are programs that translate general operating system commands into specific references to hardware details

Some representative types of applications: Business application for single platform Hardware device driver Business application for multiple platforms Embedded systems & computer games

High-level language Formal structures, which allow: Easy organization Ability to maintain large sections of code ASM Minimal formal structure leads to programmers enforcing structure Consequence: difficult to maintain existing code

High-level language May not provide for direct hardware access Even if it exists, awkward coding techniques often used Consequence: difficult to maintain existing code ASM Hardware access is straightforward and simple Programs are short and welldocumented Consequence: easy to maintain existing code

High-level language Usually very portable Code can be recompiled on multiple operating systems with minimal changes Consequence: easy to maintain existing code ASM Must be recoded separately for each platform Often uses an assembler with different syntax Consequence: difficult to maintain existing code

High-level language Produces too much executable code May not run efficiently ASM Ideal because: Executable code is small Runs quickly

Virtual Machines Specific Machine Levels

Tanenbaum: Virtual machine concept Programming Language analogy: 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

English: Display the sum of A times B plus C. C++: cout << (A * B + C); Intel Machine Language: A1 00000000 F7 25 00000004 03 05 00000008 E8 00500000 Assembly Language: mov eax,a mul B add eax,c call WriteInt

High-Level Language Assembly Language Instruction Set Architecture (ISA) Digital Logic

Application-oriented languages C++ Java Pascal Visual Basic... Programs compile into assembly language

Instruction mnemonics that have a one-to-one correspondence to machine language Programs are translated into Instruction Set Architecture (ISA) Level-machine language

Also known as conventional machine language Executed by Digital Logic

CPU, constructed from digital logic gates System bus Memory Implemented using bipolar transistors

Binary Numbers Translating between binary and decimal Binary Addition Integer Storage Sizes Hexadecimal Integers Translating between decimal and hexadecimal Hexadecimal subtraction Signed Integers Binary subtraction Character Storage

Digits are 1 and 0 1 = true 0 = false MSB: most significant bit LSB: least significant bit Bit numbering

Each digit (bit) is either 1 or 0 Each bit represents a power of 2

2 n Decimal Value 0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 2 n Decimal Value 8 256 9 512 10 1,024 11 2,048 12 4,096 13 8,192 14 16,384 15 32,768 Every binary number is a sum of powers of 2

Weighted positional notation shows how to calculate the decimal value of each binary bit: dec = (D n-1 2 n-1 ) + (D n-2 2 n-2 ) +... + (D 1 2 1 ) + (D 0 2 0 ) D = binary digit Binary 00001001 = decimal 9: (1 2 3 ) + (1 2 0 ) = 9

Repeatedly divide the decimal integer by 2 Each remainder is a binary digit in the translated value: 37 = 100101 Division Quotient Remainder 37/2 18 1 18/2 9 0 9/2 4 1 4/2 2 0 2/2 1 0 1/2 0 1

Starting with the LSB, add each pair of digits, include the carry if present

Standard sizes Ranges of unsigned integers Storage Type Range (Low High) Powers of 2 Unsigned byte 0 to 255 0 to (2 8 1) Unsigned word 0 to 65535 0 to (2 16 1) Unsigned doubleword 0 to 4,294,967,295 0 to (2 32 1) Unsigned quadword 0 to 18,446,744,073,709,551,615 0 to (2 64 1) What is the largest unsigned integer that may be stored in 20 bits?

Binary Decimal Hexadecimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 Binary Decimal Hexadecimal 1000 8 8 1001 9 9 1010 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F

Each hexadecimal digit corresponds to 4 binary bits Example: Translate the binary integer 000101101010011110010100 to hexadecimal 1 6 A 7 9 4 0001 0110 1010 0111 1001 0100

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

Used when calculating hexadecimal values up to 8 digits long 16 n Decimal Value 0 1 1 16 2 256 3 4,096 4 65,536 5 1,048,576 6 16,777,216 7 268,435,456

decimal 422 = 1A6 hexadecimal Division Quotient Remainder 422/16 26 6 26/16 1 A 1/16 0 1

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 1 1 28 6A 42 45 58 4B 78 6D 80 B5 21 / 16 = 1, rem 5 Important skill: Programmers frequently add and subtract the addresses of variables and instructions.

When a borrow is required from the digit to the left, add 16 (decimal) to the current digit's value 16 + 5 = 21-1 C6 75 A2 47 24 2E Practice: The address of var1 is 00400020. The address of the next variable after var1 is 0040006A. How many bytes are used by var1?

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

Negative numbers are stored in two's complement notation Represents the additive inverse Starting Value 00000001 Step 1: Reverse the bits 11111110 Step 2: Add 1 to the value from Step 1 11111110 + 00000001 Sum: 2 s Complement Representation 11111111 Note that 00000001 + 11111111 = 00000000

When subtracting A B, convert B to its two's complement Add A to ( B) 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 + 1 1 1 1 1 1 0 1 0 0 0 0 1 0 0 1 Practice: Subtract 0101 from 1001

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 unsigned integers Storage Type Range (Low High) Powers of 2 Signed byte -128 to +127-2 7 to (2 7 1) Signed word -32,768 to +32,767-2 15 to (2 15 1) Signed doubleword -2,147,483,648 to +2,147,483,647-2 31 to (2 31 1) Signed quadword -9,223,372,036,854,775,808 to -9,223,372,036,854,775,807-2 63 to (2 63 1) What is the largest signed integer that may be stored in 20 bits?

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

Pure binary Can be calculated directly ASCII binary String of digits: "01010101" ASCII decimal String of digits: "65" ASCII hexadecimal String of digits: "9C"

NOT AND OR Operator Precedence Truth Tables

Based on symbolic logic, designed by George Boole Boolean expressions created from: NOT, AND, OR Expression X X Y X Y X Y Description NOT X X AND Y X OR Y (NOT X) OR Y (X Y) NOT (X AND Y) X Y X AND (NOT Y)

Inverts (reverses) a Boolean value Truth table for Boolean NOT operator Digital gate diagram for NOT: X X F T T F NOT

Truth table for Boolean AND operator X Y X Y F F F F T F T F F T T T Digital gate diagram for AND: AND

Truth table for Boolean OR operator X Y X Y F F F F T T T F T T T T Digital gate diagram for OR: OR

Examples showing the order of operations Expression X Y (X Y) X (Y Z) Order of Operations NOT, then OR OR, then NOT AND, then OR

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 X X Y X Y F T F T F T T T T F F F T F T T

Example: X Y X Y Y X Y F F T F F T F F T F T T T T F F

Example: (Y S) (X S) X Y S (Y S) S (X S) (Y S) (X S) F F F F T F F F T F F T F F T F F F T T T T T F F T T T F F T F F F F F T T T F F T T F T F F F F T T T T F F T X Y S mux Z Two-input multiplexer A multiplexer is a device that selects one of several analog or digital input signals and forwards the selected input into a single output line

Assembly language helps you learn how software is constructed at the lowest levels Assembly language has a one-to-one relationship with machine language Each layer in a computer's architecture is an abstraction of a machine Layers can be hardware or software Boolean expressions are essential to the design of computer hardware and software