Computer Engineering II

Summer 2004 Laboratory Notes

The ECE 291 Documentation Project

Department of Electrical and Computer Engineering

University of Illinois at Urbana-Champaign

Edited by

Peter L. B. Johnson

This manual is intended to be a supplement to the class notes and text book for ECE 291, the second course in the introductory computer engineering sequence. This course, along with the use of this manual, attempts to achieve three major objectives. First, to give the student a fundamental understanding of the principles and techniques of machine-level programming. Second, to teach students to use computers for real-time data acquisition and control of input-output devices. Lastly, to give the student an understanding of how to design and implement complex programs on a PC. This manual contains both lab-specific information as well as general programming information to the end of achieving the stated goals.

Table of Contents
I. Getting Started
1 Introduction to the Course
1.1 Machine Problems
1.2 WWW Page
2 Using the PC
2.1 Microsoft Windows / DOS
2.2 Assembling and Linking Files
3 Text Editors
3.1 VIM
3.2 Emacs
3.3 Other Editors
II. Assembly Programming
4 Assembly Language
4.1 Conditional Branching and Flags
4.2 Variations on Loops
4.3 Modular Programming
4.4 Programming Style
4.5 Memory Addressing
4.6 String Instructions
5.1 Layout of a NASM Source Line
5.2 Pseudo-Instructions
5.3 Effective Addresses
5.4 Constants
5.5 Expressions
5.6 SEG and WRT
5.7 STRICT: Inhibiting Optimization
5.8 Critical Expressions
5.9 Local Labels
5.10 Standard Macros
5.11 Assembler Directives
6 Debugging Tools
6.1 Turbo Debugger (TD)
6.2 The Case of the Speckled Bug
7 Data Structures
7.1 Arrays
7.2 Queues
7.3 Linked Lists
7.4 Binary Trees
7.5 Hash Tables
8 C Programming
8.1 Introduction to the C Programming Language
8.2 Mixing Assembly and C
8.3 16-bit C Programming
9 Libraries
9.1 The LIB291 Library of Subroutines
9.2 Code Examples of binasc and ascbin
III. Interfacing With the World
10 I/O Devices
10.1 Keyboard
10.2 Mouse
10.3 8253 Timer Chip
10.4 Internal Speaker
11 Graphics
11.1 Text Mode
11.2 VGA Mode 13h Graphics
11.3 Interrupt 10h Video Reference
11.4 PCX Graphic File Format
12 Serial Communication
12.1 Serial Data Communication
12.2 Modems, Bauds, and Bits per Second
12.3 Interface Standards
12.4 Connections, Compatibility, and Null Modems
12.5 Parallel/Serial Conversion
12.6 Serial Data Communication using BIOS calls
12.7 Serial Data Communication using IN and OUT
12.8 Creating a Null-Modem
13 Parallel Communication
13.1 Printer Adapter Hardware
13.2 BIOS and DOS Function Calls
13.3 Machine Language Control of Printer Adapter
13.4 Use of the Printer Adapter for Data Input
IV. Protected Mode
14 Introduction to Protected Mode
14.1 How to Do this Tutorial
14.2 The Goal
14.3 Protected Mode and the Final Project
15 DJGPP Development Environment
15.1 About DJGPP
15.2 About DPMI
15.3 Setting Up DJGPP
16 Starting Protected Mode
16.1 Our First Protected Mode Program
16.2 Going Behind the Scenes
17 Differences Between Real Mode and Protected Mode
17.1 Source Differences
17.2 Memory Differences
17.3 Using Interrupts in Protected Mode
18 Introduction to PModeLib
18.1 What is PModeLib?
18.2 The proc and invoke Macros
18.3 Using PModeLib: A Framework
18.4 Using Interrupts with PModeLib Functions
18.5 Allocating Memory
18.6 File I/O
18.7 Protected Mode Interrupt Handling
18.8 High-Resolution Graphics
V. Advanced Topics
19 Sound
19.1 How to Play Long Sounds
VI. Appendices
A. PModeLib Reference
A.1 Global Variables
A.2 Global Constants and Limits
A.3 Initialization and Shutdown
A.4 Simulate Real-Mode Interrupt
A.5 Memory Handling
A.6 General File Handling
A.7 Graphics File Handling
A.8 Interrupt, IRQ, and Callback Wrappers
A.9 Text Mode Functions
A.10 High-Resolution VBE/AF Graphics Functions
A.11 NetBIOS Networking
A.12 IP "Sockets" Networking (TCP/IP, UDP/IP)
A.13 Sound Programming
A.14 DMA Functions
A.15 Miscelleanous Utility Functions
B. x86 Instruction Reference
B.1 Key to Operand Specifications
B.2 Key to Opcode Descriptions
B.3 Key to Instruction Flags
B.4 General Instructions
B.5 SIMD Instructions (MMX, SSE)
C. EFLAGS Cross-Reference
D. ASCII Code Tables
D.1 Character Set Quick Reference (Hex)
D.2 ASCII Codes in Hex, Octal, and Decimal
List of Tables
4-1. 16-bit Register Addressing Modes
4-2. Examples of Valid 32-bit Register Addresses
10-1. Meaning of Shift Status Byte at 0040:0017h
10-2. Meaning of Shift Status Byte at 0040:0018h
10-3. Scan Codes Assigned to Keys on the Standard or AT Keyboard
10-4. "Second Codes" for ASCII Code 00h Key Combinations
10-5. Interpretation of the Timer Control Byte
12-1. Most Commonly Used RS-232C Signals
12-2. RS-232C Electrical Specifications
12-3. BIOS Serial Port Status, Returned in AH
12-4. BIOS Modem Port Status, Returned in AL
12-5. Accessible registers in the IN8250 ACE
12-6. Serial Interrupt Enable Register (@ ComBase+1, with DLAB=0)
12-7. Serial Line Control Register (@ ComBase+3)
12-8. Modem Control Register (@ ComBase+4)
12-9. Serial Line Status Register (@ ComBase+5)
12-10. Modem Status Register (@ ComBase+6)
13-1. Signals on 25-pin Printer Connector (pin numbers)
13-2. I/O Port Addresses (in hexadecimal) for Printer Adapter Buffer Registers
13-3. Register AL Bit Assignments for Printer Status Signals
13-4. Register AL Bit Assignments for Printer Control Signals
B-1. SSE Condition Predicate Encoding
C-1. EFLAGS Cross-Reference
List of Figures
10-1. Built-in speaker hardware
12-1. Typical Connection between and another Device via the Telephone Network
12-2. 300 Baud Asynchronous Full-Duplex U.S. Frequency Assignments
12-3. Typical Null-Modem Cables
12-4. Simplified logic diagram of the Asynchronous Communications Adapter
13-1. Timing Diagram for the Centronics Protocol
13-2. Simplified Logic Diagram of the Printer Adapter
19-1. Interrupt at DMA Loop Point
19-2. Interrupt at DMA Loop Point and at Halfway Point
List of Examples
9-1. Use of dspmsg to type out the string tstmsg on the display
9-2. Use of dosxit
9-3. binasc code example
9-4. ascbin code example
10-1. Interrupt 9 (Keyboard) Handler
10-2. Using Interrupt 16h with AH=1
11-1. Displaying a PCX File
17-1. Getting the Time in Protected Mode
17-2. Protected Mode "dspmsg" Program
18-1. Using the invoke macro
18-2. PModeLib "dspmsg" Program
18-3. Allocating Memory
18-4. File I/O
18-5. Hooking the timer interrupt
18-6. Displaying an Image