Basic Instruction Set and Assembler Programming (MS-DOS)
While the 8088 (and 80x86 family) provide a large number of machine level
instructions and while MS-DOS (and its Windows derivatives) supply an equally
larger number of service routines, it is possible to get a feeling for low-level
programming with a relatively few instructions and interrupt services. The
material found here is not intended to make you into an IBM PC Assembler
programmer; that would require at least a full course and some practical
experience. However, you should get a basic feel for the structure and methods
of low-level programming on the IBM PC.
BASIC ASSEMBLER INSTRUCTIONS
As with the previously discussed instructions, in the following, with respect
to flags set, only the Carry, Overflow, Sign and Zero flags will be considered.
See the earlier material for meaning of "segreg", "regmem", etc.
Although provided in the earlier material, MOV, ADD, and SUB will also
be included here for the sake of completeness.
MOV [destination],[source]
copies the contents of the [source] into the [destination]
MOV segreg,regmem
MOV regmem,segreg
MOV regmem,reg
MOV regmem,constant
MOV reg,regmem
Flags: (none)
ADD [destination],[source]
adds contents of the [source] to contents of the [destination] and
replaces the contents of the [destination] with the sum
ADD AL or AX,constant
ADD regmem,reg
ADD regmem,constant
ADD reg,regmem
Flags: Carry,Overflow,Sign,Zero
SUB [destination],[source]
subtracts contents of the [source] from the contents of the
[destination] and replaces the contents of the [destination] with the
difference
SUB AL or AX, constant
SUB regmem,reg
SUB regmem,constant
SUB reg,regmem
Flags: Carry,Overflow,Sign,Zero
JMP [address]
unconditional jump; IP (and possibly CS) replaced to give
address specified in operand as next instruction
JMP label
JMP regmem (not used in this course)
Flags: (none)
CMP [value1],[value2]
compares operand values; same as SUB except the
difference is not used as a replacement; only the flags change
CMP AL or AX,constant
CMP regmem,reg
CMP regmem,constant
CMP reg,regmem
Flags: Carry,Overflow,Sign,Zero
JZ [address]
jump if Zero flag set (jump zero)
JNZ [address]
jump if Zero flag Not set (jump not zero)
JA [address]
jump if neither Carry nor Zero flag set; used for unsigned values (jump above)
JB [address]
jump if Carry flag is set; used for unsigned values (jump below)
JL [address]
jump if Sign flag is set; used for signed values (jump less than zero)
JG [address]
jump if neither Sign nor Zero flag is set; used for signed values (jump greater than zero)
INC [destination]
increment destination contents by 1
INC regmem
Flags: Overflow,Sign,Zero
DEC [destination]
decrement destination contents by 1
DEC regmem
Flags: Overflow,Sign,Zero
SHL [operand],[count]
Shift the bit pattern of the "operand" to the left by the amount
specified by "count" and fill low-order bit positions with 0. The only
legal values for "count" are 1 or CL; if CL is used the pattern is shifted
to the left by the number of positions contained in the CL register.
SHL regmem,1
SHL regmem,CL
Flags: Carry,Sign,Zero
SHR [operand],[count]
Shift the bit pattern of the "operand" to the right by the amount
specified by "count" and fill high-order bit positions with 0. The only
legal values for "count" are 1 or CL; if CL is used the pattern is shifted
to the left by the number of positions contained in the CL register.
SHL regmem,1
SHL regmem,CL
Flags: Carry,Sign,Zero
AND [destination],[source]
And the "source" bits with the "destination" bits and save the results
in "destination".
AND AL or AX,constant
AND regmem,regmem
AND regmem,constant
Flags: Sign,Zero
OR [destination],[source]
Or the "source" bits with the "destination" bits and save the results
in "destination".
OR AL or AX,constant
OR regmem,regmem
OR regmem,constant
Flags: Sign,Zero
XOR [destination],[source]
Exclusive Or the "source" bits with the "destination" bits and save the results
in "destination".
XOR AL or AX,constant
XOR regmem,regmem
XOR regmem,constant
Flags: Sign,Zero
NOT [operand]
Not the "operand" bits bits and save the results in "operand".
NOT regmem
Flags: (None!)
CALL [address]
for a "far proc", save CS and IP on stack then performs
a JMP to [address]; for a "near proc", saves IP on stack
then performs a JMP to [address]
CALL label
CALL regmem (not used in this course)
Flags: (none)
RET
return from a "called proc"; for a "far proc", pops IP
and CS off the stack; for a "near proc", pops IP off the
stack; next instruction become the one serially after the
CALL to this procedure
RET
Flags: (none)
PUSH [operand]
stores operand value on top of stack; note: constants
and 8-bit registers are not legal [operand]s
PUSH reg16
PUSH segreg
PUSH mem (not used in this course)
Flags: (none)
POP [operand]
restores value from top of the stack to the operand;
note PUSH comment applies here too
POP reg16
POP segreg
POP mem (not used in this course)
Flags: (none)
INT 21h
Call to DOS service function; specific function
dependant upon pre-set code in AH as previously discussed;
other software interrupts are not used in this course
LOOP [address]
decrement CX and JMP to [address] if contents of CX is
not zero
LOOP label
Flags: (none)
REP MOVSB
repeat copying of bytes from DS:SI to ES:DI, incrementing
SI and DI by 1 after each move and decrementing CX by 1
until contents of CX becomes zero
REP MOVSB
Flags: (none)
PSEUDO-INSTRUCTIONS
The following mnemonic codes are not true instructions; some do not even
use up memory locations. They are directives to establish data areas or to
provide other directives to the Assembler program.
DB
define byte
TEN DB 10 ;reserve space for a byte and initialize it to 10 (dec.)
SPACE DB 12 DUP (?) ;reserve space for 12 (dec.) uninitialized bytes
NAME DB 'Joe' ;reserve 3 bytes initialized to the ASCII codes for 'Joe'
DW
define word
DAYS DW (?) ;reserve 16-bits uninitialized
TOTAL DW 0 ;reserve 16-bits initialized to zero
END
indicated physical end of Assembler program and specifies
a label for the instruction where execution should begin
END START
SEGMENT
indicates the physical beginning of a program segment
(code, data, stack, etc.) within the source program
DATAREA SEGMENT
ENDS
indicates the physical end of a program segment with in
the source program
DATAREA ENDS
(note: label matches label of corresponding SEGMENT statement;
source segments can not be coded as overlapping or containing other segments)
PROC
indicates the physical beginning of a subroutine or
"procedure" and specifies if the procedure is to be
treated as "near" or "far" with respect to CALL and RET
instructions
DISPHEX PROC NEAR
ENDP
indicates the physical end of a subroutine or "procedure"
DISPHEX ENDP
(see note on ENDS)
ASSUME
specifies segment(s) associated with specifiec segment
register(s) for subsequent translation of labels into
segment register:offset address form
ASSUME CS:PGMCODE,DS:DATAREA
ORG
resets the assembler program's instruction offset counter
to the value specified as an operand
ORG 100h ;next instruction will be assembled to be located 256 (or
100h) bytes from the beginning of the segment
DEBUG Programming Restrictions
Labels and label references are not allowed; this is probably the most
significant restriction, especially since address calculations for 8088/80x86
instructions is very difficult.
The only "Pseudo-Instructions" permitted are DB and DW
All numeric values may only be entered in hexadecimal
Generally speaking, only .COM-style programs can be created with DEBUG
Very Simple DEBUG Programming Example
The following could be created as a simple text file, Script.txt:
A
MOV DX,110
MOV AH,09
INT 21
MOV AX,4C00
INT 21
A110
DB "You are wonderful!$"
RBX
0000
RCX
0023
NFeelGood.COM
W
Q
This type of file is called a "DEBUG script file"; notice that it contains
characters which exactly match what you could type while running in DEBUG
interactively (including blank lines to terminate Assemble operations).
Setting the BX and CX registers is required to specify the number of bytes
to be written to the file (with the W command)
To run this "script file" and create the executable program FeelGood.COM
enter the following command at the MS-DOS prompt:
DEBUG <Script.txt
Subsequent entry of the "command" FeelGood at the MS-DOS prompt would result
in display of the message: You are wonderful!