From: raymoon@ms1.dgsys.com (Raymond Moon)
Subject: x86 Assembly Language FAQ - General Part 1/3
Summary: This section is part one of three parts that contain x86 asm info common to all assemblers.
------------------------------

Subject: 1. Introduction and Intent

go General Section for Details
Return to the Table Of Contents
------------------------------

Subject: 2. Table of Contents

Part I

1.  Introduction and Intent
2.  Table of Contents
3.  Charters For comp.lang.asm.x86 and alt.lang.asm Newsgroups **
4.  What is Assembly Language **
5.  List of x86 OpCodes **
6.  What is HELPPC and Where It Is Available **
7.  How To Truncate a File
8.  How Can STDERR Be Redirected To a File
9.  How To Determine the CPU Type
10. IRQ Assignments
11. Ralf Brown's Interrupt List                                     REVISED
12. Using VGA Mode 13h for Fast Graphics
13. Protected Mode                                                  REVISED
14. Shareware ASM Libraries

Part II

15. Accessing 4 Gegs of Memory in Real Mode
16. What Is Available at developer.intel.com
17. Interrupts and Exceptions
18. ASM Books Available
19. ASM Code Available on Internet
20. How To Commit a File
21. Using Extended Memory Manager
22. EXE2BIN Replacement
23. ASM Tutorials Available on the Internet
24. Shareware Assemblers                                            REVISED
25. Undocumented OpCodes

Part III

26. WWW Assembly HomePages
27. Common Reason Why Memory Allocation Fails
28. Volume Serial Numbers
29. .obj File Format
30. Rebooting from Software
31. Other FAQs
32. Pseudo Random Number Generator in Assembly Language
33. Command Line Arguments
34. Free 32-bit and DJGPP
35. TERSE Programming Language
36. Assembly Language IDEs                                          REVISED
37. Disassemblers
38. How to Optimize for the Pentium
39. Assembly Language Programming Style Guidelines
40. Other Assembly-Related Newsgroups
41. ZD-86 Debugger
42. Acknowledgments

[General][MASM][TASM][A86/D86]
------------------------------

Subject: 7. How To Truncate A File

There is not any single DOS Int 21h function that performs this operation. 
A file can be truncated using two functions.  The procedure is:

1.  Use Int 21h function 42h, Move File Pointer, to move the file pointer
    to the position where you want the file to be truncated.
2.  Use Int 21h function 40h, Write File or Device, to write zero bytes to 
    the file.

Execution of the last DOS function will update the directory to the new
file length.

Contributor: Raymond Moon, raymoon@moonware.dgsys.com
Last changed: 28 Dec 94

Return to the Table Of Contents
------------------------------

Subject: 8. How Can STDERR Be Redirected To A File

I understand that 4DOS has this capability at its command line.  If you are
looking in the assembly language FAQ for this information, an assembly
language answer probably is desired.  Here it is.

You will need to write a short program that performs the STDERR redirection
before loading and executing the desired program.  This loader program
relies upon the fact that a child program inherits all open files of the
parent program unless the parent program opens a file with the inheritance
flag set to no.

Because the full code for such a program is too large for this FAQ, I will
give the salient specifications for such a program.

1.  The loader program accepts three command line arguments:
    a.  The full path and filename of the file into which STDERR is to be
        written.
    b.  The full path and filename of the program to be executed.
    c.  The command line for the program to be executed (should be
        delimited by double quotes to allow multiple arguments).  This
        argument is optional.
2.  Release all memory above the program using Int 21 function 4ah so that
    there will be room enough to load and execute the designated program.
3.  Open the file from step 1.a above into which STDERR is to be written.
4.  Duplicate STDERR filehandle, which is 2, using Int 21h function 45h.
5.  Using Int 21h function 46h, force STDERR filehandle, again 2, to have
    the filehandle of the opened file from step 2.
6.  Use Int 21h function 4b00h to load and execute the program from step
    1.a.  Use the default environment and the command line from step 1.c
    above.
7.  Upon return from the function 4b00h, close the file opened in step 2.
8.  To restore STDERR, use Int 21h function 46h to force STDERR, again 2, 
    to point to the filehandle saved from step 3 above.

This same technique can be applied to any of the standard devices.

I have written a full featured demonstration program.  I believe that asm
programmers will find the source code useful even if they do not want to
redirect stderr to a file.  The URL to the file is:

    ftp://ftp.simtel.net/pub/simtelnet/msdos/asmutl/stderrf.zip

Contributor: Raymond Moon, raymoon@moonware.dgsys.com
Last changed: 3 Jun 95

Return to the Table Of Contents
------------------------------

Subject: 9. How To Determine The CPU Type

9.1  CPUID PROGRAM

The type of processor and math coprocessor can be determined using two
functions that have been provided by Intel.  The source code to these
functions can be obtained from Intel by:

    ftp://ftp.intel.com/pub/IAL/tools_utils_demos/cpuid3.zip

Three source files are included in this .zip file.
    cpuid3a.asm - This source code file contains two assembly language
        functions.  One determines the type of cpu from 8088/8086 to
        Pentium.  The second detects and identifies, if present, the type
        of math coprocessor.
    cpuid3b.c - a c program that calls the above two functions and displays
        the results.
    cpuid3c.asm - this is an assembly program equivalent to cpuid3b.c.

9.2  AP-485 INTEL PROCESSOR IDENTIFICATION WITH THE CPUID INSTRUCTION

This Application Note explains how to use the CPUID instruction in software
applications, BIOS implementations, and various processor tools.  By taking
advantage of the CPUID instruction, software developers can create software
applications and tools that can execute compatibly across the widest range
of Intel processor generations and models, past, present, and future.

    http://developer.intel.com/design/pro/applnots/241618.HTM

9.3  Robert Collins' CPUID.ASM

Robert Collins has written two columns for Dr. Dobb's Journal on this
subject.  These articles with source code is available on his web site:

    Part 1: http://www.x86.org/ddj/Sep96/Sep96.html
    Part 2: http://www.x86.org/ddj/Nov96/Nov96.html


9.4  Grzegorz Mazur's x86 CPU Identification

Grzegorz has a series of hypertext articles that explain x86 CPU
identification algorithms developed by himself.  Covered are not only the
Intel chips but also V20, V30 (remember them), and Cyrix.  His page is
located:

    http://grafi.ii.pw.edu.pl/gbm/x86/index.html

Contributor: Raymond Moon, raymoon@moonware.dgsys.com
Last changed: 18 Mar 97

Return to the Table Of Contents
------------------------------

Subject: 10. IRQ Assignments

A list of IRQ assignments are available in David Jurgens' HELPPC database.  
See Subject #6 for details on how to obtain this program.

Contributor: Raymond Moon, raymoon@moonware.dgsys.com
Last changed: 28 Dec 94  

Return to the Table Of Contents
------------------------------

Subject: 11. Ralf Brown's Interrupt List REVISED

11.1  FILE AVAILABILITY

The latest version of Ralf Brown's Interrupt List is 5.9, dated 30 Jun 98.  
The files are available directly from his home page, from SimTel, or Garbo:

    http://www.cs.cmu.edu/afs/cs.cmu.edu/user/ralf/pub/WWW/files.html
    ftp://ftp.simtel.net/pub/simtelnet/msdos/info
    ftp://garbo.uwasa.fi/pc/programming

The files are:
inter59a.zip    Comprehensive listing of interrupt calls, 1of4
inter59b.zip    Comprehensive listing of interrupt calls, 2of4
inter59c.zip    Comprehensive listing of interrupt calls, 3of4
inter59d.zip    Comprehensive listing of interrupt calls, 4of4
inter59e.zip    Utility programs/source code for interrupt list
inter59f.zip    WinHelp conversion programs for interrupt list
inter59g.zip    Hypertext conversion programs for interrupt list

11.2  DESCRIPTION

The interrupt list is a comprehensive listing of functions available
through interrupt calls and FAR calls, both documented and (officially)
undocumented, plus maps of CMOS and BIOS memory, I/O ports, I2C-bus
devices, and System Management Mode save areas.  This release contains more
than 9200 entries and over 5000 tables

11.3  WHAT IS NEW


Nearly 400k of updates, including Intel 440EX/X and 82371MX chipsets, OPTi
"Vendetta" and 82C493/82C382 chipsets, Via VT82C496G and VT82C570M "Apollo
Master" chipset, PicoPower Vesuvius chipset, C&T 82C9001A and 64200/64310
video chips, Ensoniq ES1370/1371 sound chips, Cirrus CL-PD6710/22/29
network adapters, Award-BIOS password algorithm, more PCI vendor IDs, and a
substantially expanded OPCODES.LST (now including instruction timing tables
for most CPUs).  Also added a new viewer for use under MS Windows (in
inter59e).

11.4 OTHER INCLUDED GEMS

OVERVIEW.LST - A brief description of each of the 256 interrupts.
86BUGS.LST - A list of undocumented and buggy instructions with
    descriptions of the x86 Intel processor and compatible processors.  And
    you thought that the Intel FDIV was the first bug in a processor!
CMOS.LST - a CMOS memory map.
OPCODE.LST - A list of undocumented instructions and documented
    instructions of any last processor.
PORTS.LST - I/O port addressed for XT, AT and PS/2 computers.
GLOSSARY.LST - glossary of PC terms.
MEMORY.LST - The format for various memory locations, such as the BIOS Data
    Segment, Interrupt Vector Table, and much, much more.
INTERRUP.PRI - iAPX 86 Interrupt Primer

Contributor: Raymond Moon, raymoon@moonware.dgsys.com
Last changed: 5 Sep 98

Return to the Table Of Contents
------------------------------

Subject: 12. Using VGA Mode 13h for Fast Graphics

12.1  INTRODUCTION AND PREPARATION

Mode 13h is so widely used for graphics applications in DOS because it is
very easy to use.  The screen is constantly being redrawn by the video
card.  To affect what the card draws, it is necessary to write to the
screen buffer.  The screen buffer in mode 13h is always at segment:offset =
A000:0000.  Thus, to set up drawing directly to the video buffer, this is
what you'd most often first do:

  ;Change the video mode to 13h
    xor  ah, ah         ;VIDEO Function 00h: Change screen
    mov  al, 13h        ;Put the desired graphics mode into AL
    int  10h            ;Call VIDEO

  ;Prepare for writing to the video buffer
    mov  di, 0a000h     ;Put the video segment into DI
    mov  es, di         ; so it can easily be put into ES
    xor  di, di         ;Start writing at coordinates (0,0)

12.2  WRITING PIXELS TO THE SCREEN

Why is Mode 13h so popular?  To understand, you must know a few basic
facts.  In Mode 13h, the screen is 320 by 200, or 320 pixels across and 200
pixels down.  In each pixel, there's a possibility of 256 colors, which can
be fit into one byte.  Thus, 320*200*1 = 64000 bytes, about the size of one
segment.  Think of the screen as an array of colors.  The first row takes
up addresses A000:0000 to A000:013F (decimal 319), the second row takes up
addresses A000:0140 to A000:027F (decimal 639), and so on.  To plot a
pixel, assuming ES=A000:

  ;Plot a pixel in video mode 13h, where
  ;PixelAddress = (320 * Y) + X
    mov  ax, 320        ; Prepare for the multiplication  
    mul  [Y]            ; Assuming that Y is defined in the data segment
                        ;   earlier in the program
    mov  di, ax         ; Put in into the pointer to the offset of ES
    add  di, [X]        ; Assuming that X is defined in the data segment
                        ;   earlier in the program
    mov  al, [Color]    ; Assuming that Color is defined in the data
                        ;   segment earlier in the program
    stosb               ; Write it to the screen!

See how easy that was?  Something to remember is that it is zero-based.  
The upper-left corner is (0,0), and the lower-right is (319,199).  A
complete TASM Ideal mode procedure might look something like this (it
assumes that the video card is already set to mode 13h):

PROC WritePixel BASIC   ; Or whatever language you might want to link
                        ;  it to
    USES es, di         ; It's always a good idea to preserve ES and DI
    ARG  X:word, Y:word, Color:BYTE
    mov  di, 0a000h     ; Put the video segment into DI
    mov  es, di         ;   so it can easily be put into ES
    mov  ax, 320        ; Prepare for the multiplication
    mul  [Y]            ; Offset pointer by the Y value passed in
    mov  di, ax         ; Put in into pointer to the offset of ES
    add  di, [X]        ; Offset the pointer by the X value passed in
    mov  al, [Color]    ; Put color to be written to the screen in AL
    stosb               ; Write it to the screen!
    ret
ENDP WritePixel

To write a horizontal line, just put the length in CX, and replace the
STOSB with a REP STOSB.  Writing a vertical line is only a little more
tricky.  Observe the following TASM Ideal mode procedure:

PROC VerticalLine BASIC ; Or whatever language you might want to link
                        ;  it to
    USES es, di         ; It's always a good idea to preserve ES and
                        ;  DI
    ARG  X:word, Y:word, Color:BYTE, Length:word
    mov  di, 0a000h     ; Put the video segment into DI
    mov  es, di         ; so it can easily be put into ES
    mov  ax, 320        ; Prepare for the multiplication  
    mul  [Y]            ; Offset the pointer by the Y value passed in
    mov  di, ax         ; Put in into the pointer to the offset of ES
    add  di, [X]        ; Offset the pointer by the X value passed in
    mov  al, [Color]    ; Put the color to be written to the screen
                        ; in AL
    mov  cx, [Length]   ; Prepare for the loop
YLoop:
    stosb               ; Write it to the screen!
    add  di, 319        ; Move down one row (DI has already advanced
                        ;  once because of the STOSB, thus the 319)
    loop YLoop
    ret
ENDP VerticleLine

Observe how there is a tight loop that moves DI down one row each
iteration.

In short, the easiest way to write directly to the Mode 13h video buffer is
to think of the screen as just a 320 by 200 array of bytes, starting at
A000:0000.

Author: Michael Averbuch (mikeaver@prairienet.org)
Last Change: 29 Dec 94

Return to the Table Of Contents
------------------------------

Subject: 13. Protected Mode REVISED

13.1 PMODE Tutorials, FAQ, and other reference documentation


Protected Mode Basics by Robert Collins
    http://www.x86.org/articles/pmbasics/tspec_a1_doc.html
    Excellent starting tutorial with source code.

PMODE FAQ
    ftp://ftp.simtel.net/pub/simtelnet/msdos/asmutl/pmtut002.zip

Protected Mode Book List
    http://www.interactive.net/~viren/Janz/Books/pmode_books.htm

Jens Hohmuth PMODE Tutorial
    http://www.fh-zwickau.de/~hoh/pm_eng/text/index.htm
    HTML format

pmode-l FAQ
    http://www.lysator.liu.se/~redhog

13.2 Source code Archives

Walnut Creek PMODE Archives
    ftp://ftp.cdrom.com/pub/demos/code/hardware/pmode/index.html

X2FTP.OULU.FI
    ftp://x2ftp.oulu.fi/pub/msdos/programming/pmode
    Protected mode utilities and some source code

13.3 PMODE Websites

Cameron's 386+ Programming Page                     NOT CURRENTLY WORKING
    http://free.prohosting.com/~cameron/
    32 bit DOS extender/Utilities/pmode extender
    File formats and specifications/Game programming
    Knowledge Base with ASM tutorials, Denthor's VGA Trainer and Univ. of
        Guadalajara ASM tutorial

Peter's PMODE Home Page
    http://www.geocities.com/SiliconValley/Peaks/1231/
    PMODE tutorials and programming related files

Niko Komin's Assembler for PCs page
    http://www.inx.de/~nkomin/html/assembe.htm

Shareware, pmode, x86 mnemonics, ASM related links.
    http://www.alaska.net/~zumwalt
    Archives, Source Code, Technical Documentation, OS Chat Room and much more

PASS-32, Dieter's Assembler
    http://www.eikon.e-technik.tu-muenchen.de/~et_514/pass32.html
    Debugger and DOS extender also available

Dario Alpern's programs
    https://members.tripod.com/~alpertron/ENGLISH2.HTM
    PMODE examples

5.4 PMODE Mailing Lists

Protected Mode Mailing list:
    To subscribe:
    Send: mailto:pmode-l-request@fys.ruu.nl
    subject: none
    body: subscribe pmode-l email@yourisp.name (Note that is pmode-l (ell) not
        pmode-1 (one)

Use pmode-l@fys.ruu.nl to send email to others in the list.

To unsubscribe:
    Send: mailto:pmode-l-request@fys.ruu.nl
    subject: none
    body: unsubscribe pmode-l email@yourisp.name

Contributor: Raymond Moon, raymoon@moonware.dgsys.com
Last changed: 19 Sep 98

Return to the Table Of Contents
------------------------------

Subject: 14. Shareware ASM Libraries

14.1  ASMLIB PROGRAMMER'S TOOLKIT, VERSION 4.0

Douglas Herr's shareware assembly language library.  This library is
available from SimTel.

    ftp://ftp.simtel.net/pub/simtelnet/msdos/asmutl/asmlib40.zip

The zip file contains only the medium model of the library.  There are 405
assembly subroutines in a .lib file and documentation.  Source code is available
with registration and extra fee.  The library covers the following areas:
    string/integer data manipulation        screen mode subroutines
    text-mode multi-window subroutines      disk & file subroutines
    text-mode video subroutines             EMS and XMS subroutines
    floating-point subroutines              graphics
    keyboard input subroutines              mathematical solutions
    subroutines which determine PC status

asmlib40 also comes with an editor, E16, written entirely with asmlib.

Improvements since version 3.7 is auto-sizing of the near heap in the startup
code.  There have been some incremental improvements including 32k-color
graphics and virtual graphics screens.

14.2  THE ASSEMBLY WIZARD'S LIBRARY, VERSION 2.1

This is Chris Walker's shareware assembly language library.  This library used
to be Thomas Hanlin's.

    ftp://ftp.simtel.net/pub/simtelnet/msdos/asmutl/asmwiz21.zip

This library comes with documentation and one .lib file that supports small and
tiny memory models.  Source code is available with registration. The library
covers the following areas:

    Base Conversions        Mouse Services
    Exception Handling      Sound and Music

    Delays and Countdowns   String Services
    File Handling           Telecommunications
    Filename Manipulation   Time and Date
    Keyboard Services       Video Services
    Long Integer Math       Miscellaneous Services
    Memory Services

14.3  UCR Standard Library for Assembly Language Programmers

This library is written by Randall Hyde and others.  This library is available
from many sites but most of them are seriously out of date.  You can get the
latest version at: 

    http://webster.ucr.edu/Page_asm/RHUCRLib.html

Unlike the previous libraries, there are no registrations fees and the included
source code is released to the public domain.  The author does request that if
you use the library, you contribute at least one routine to the library.

    Standard Input Routines     Character Set Routines
    Standard Output Routines    Memory Management Routines
    Conversion Routines         String Handling Routines
    Utility Routines

14.4  ALIB Version 3.0

ALIB is Jeff Ownens' shareware assembly language library.  This library is
available from SimTel.

    ftp://ftp.simtel.net/pub/simtelnet/msdos/asmutl/alib30.zip

Like the UCR library described above, registration fees are not requested.  The
library consists of 179 assembly source files covering the following areas:

    compress    - data compression and expansion
    config      - program configuration, colors, paths, etc.
    compare     - compare strings
    convert     - hex/decimal/ascii conversions
    database    - simple database functions
    disk        - disk information, path changes, file searches
    display     - fast display functions, write to display memory
    error       - error handlers
    float       - simple floating point math package
    math        - dword math, crc, roots
    memory      - memory manager, extended, xms, ems, conventional
    menu        - menuing system
    message     - messages in windows on screen
    misc        - misc routines
    mouse/key   - mouse and keyboard functions
    parse       - extraction of parameters from command line
    random      - random number generators
    search      - search for character or string
    sort        - sort buffer or file
    sound       - sounds 
    string      - ascii string handling
    stdout      - characters, strings, spaces to stdout
    system      - system interrogation and setup
    time        - time and date conversions

14.5 FREELIB, Version 3.0

Freelib v3.0 is a library of 200 routines that may be useful for assembly
language programming.  Freelib includes routines that do many of the tasks that
make assembly language difficult - like buffered file I/O, formatted string
output, memory allocation, etc.  Also includes 16.16bit fixed point arithmetic,
text screen output (EGA 80x25 or VGA 90x34), and VGA graphics in both 16 and 256
colors.  All routines are highly optimized for size and speed, and average only
60 bytes each.  Full source code and documentation is included for all routines. 
Freelib is public domain software, free for non-commercial use.  The library is
available from SimTel:

    ftp://ftp.simtel.net/pub/simtelnet/msdos/asmutl/freeli30.zip

Contributor: Raymond Moon, raymoon@moonware.dgsys.com
Last changed: 20 Dec 96


Return to the Table Of Contents