Introduction To 8086 Assembly
Introduction To 8086 Assembly
Introduction To 8086 Assembly
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
Named Constants
Symbolic names associated with storage locations represent addresses
Named constants are symbols created to represent specic values
determined by an expression
Named constants can be numeric or string
Some named constants can be redened
No storage is allocated for these values
Equates
Constant values are known as equates
Sample equate section:
Count EQU 10
Element EQU 5
Size = Count * Element
MyString EQU "Maze of twisty passages"
Size = 0
11/22/2015 10:29 AM
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
2 of 13
11/22/2015 10:29 AM
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
DW 40Ch,10b,-13,0
-4
17
1
?
DW
DB
DD
040Ch,10b,-13,0
'This is an array'
-109236, FFFFFFFFh, -1, 100b
40 DUP(?)
10h DUP(0)
3 DUP("ABC")
4 DUP(3 DUP (0,1), 2 DUP('$'))
Word Storage
Word, doubleword, and quadword data are stored in reverse byte order (in
memory)
Directive
DW 256
3 of 13
Bytes in Storage
00 01
11/22/2015 10:29 AM
DD 1234567h
DQ 10
X DW 35DAh
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
67 45 23 01
0A 00 00 00 00 00 00 00
DA 35
11/22/2015 10:29 AM
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
SUM_OF_DIGITS
$1000
DONE?
.TEST
Examples of illegal names
TWO WORDS contains a blank
2abc begins with a digit
A45.28 . not rst character
YOU&ME contains an illegal character
The Mnemonic Field
For an instruction, the operation eld contains a symbolic operation code
(opcode)
The assembler translates a symbolic opcode into a machine language
opcode
Examples are: ADD, MOV, SUB
In an assembler directive, the operation eld contains a directive
(pseudo-op)
Pseudo-ops are not translated into machine code; they tell the assembler to
do something
The Operand Field
For an instruction, the operand eld species the data that are to be acted
on by the instruction. May have zero, one, or two operands
NOP
INC AX
ADD WORD1,2
11/22/2015 10:29 AM
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
ENDP
END MAIN
;End of program
; entry point for linker use
11/22/2015 10:29 AM
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
7 of 13
11/22/2015 10:29 AM
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
Examples
mov ax, word1
"Move word1 to ax"
Contents of register ax are replaced by the contents of the memory
location word1
xchg ah, bl
Swaps the contents of ah and bl
Illegal: mov word1, word2
can't have both operands be memory locations
Sample MOV Instructions
b
w
db
dw
mov
mov
mov
mov
mov
mov
4Fh
2048
bl,dh
ax,w
ch,b
al,255
w,-100
b,0
db
dw
mov
mov
mov
mov
11/22/2015 10:29 AM
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
reg, reg
reg, mem
mem, reg
MOV and XCHG cannot perform memory to memory moves
This provides an ecient means to swap the operands
No temporary storage is needed
Sorting often requires this type of operation
This works only with the general registers
Arithmetic Instructions
ADD dest, source
SUB dest, source
INC dest
DEC dest
NEG dest
Operands must be of the same size
source can be a general register, memory location, or constant
dest can be a register or memory location
except operands cannot both be memory
ADD and INC
ADD is used to add the contents of
two registers
a register and a memory location
a register and a constant
INC is used to add 1 to the contents of a register or memory location
Examples
add ax, word1
"Add word1 to ax"
Contents of register ax and memory location word1 are added, and
the sum is stored in ax
inc ah
Adds one to the contents of ah
Illegal: add word1, word2
can't have both operands be memory locations
SUB, DEC, and NEG
SUB is used to subtract the contents of
one register from another register
a register from a memory location, or vice versa
a constant from a register
DEC is used to subtract 1 from the contents of a register or memory location
9 of 13
11/22/2015 10:29 AM
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
ax,B
ax,A
ax,A
A,ax
10 of 13
11/22/2015 10:29 AM
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
.DATA
.CODE
.STACK size
Memory Models
.Model memory_model
tiny: code+data <= 64K (.com program)
small: code<=64K, data<=64K, one of each
medium: data<=64K, one data segment
compact: code<=64K, one code segment
large: multiple code and data segments
huge: allows individual arrays to exceed 64K
at: no segments, 32-bit addresses, protected mode only (80386 and
higher)
Program Skeleton
.MODEL small
.STACK 100h
.DATA
;declarations
.CODE
MAIN PROC
;main proc code
;return to DOS
ENDP MAIN
;other procs (if any) go here
end MAIN
Select a memory model
Dene the stack size
Declare variables
Write code
organize into procedures
Mark the end of the source le
dene the entry point
Input and Output Using 8086 Assembly Language
Most input and output is not done directly via the I/O ports, because
port addresses vary among computer models
it's much easier to program I/O with the service routines provided by
the manufacturer
There are BIOS routines (which we'll look at later) and DOS routines for
handling I/O (using interrupt number 21h)
Interrupts
11 of 13
11/22/2015 10:29 AM
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
DX,theString
mov
Input a Character
Interrupt 21h, function 01h
Filtered input with echo
12 of 13
11/22/2015 10:29 AM
http://www.shsu.edu/~csc_tjm/spring2005/cs272/intro_to...
This function returns the next character in the keyboard buer (waiting
if necessary)
The character is echoed to the screen
AL will contain the ASCII code of the non-control character
AL=0 if a control character was entered
An Example Program
%TITLE "Case Conversion"
.8086
.MODEL small
.STACK 256
.DATA
MSG1
DB 'Enter a lower case letter: $'
MSG2
DB 0Dh,0Ah,'In upper case it is: '
CHAR
DB ?,'$'
exCode
DB 0
.CODE
MAIN
PROC
;initialize ds
mov
ax,@data
; Initialize DS to address
mov
ds,ax
; of data segment
;print user prompt
mov
ah,9
; display string fcn
lea
dx,MSG1
; get first message
int
21h
; display it
;input a character and convert to upper case
mov
ah,1
; read char fcn
int
21h
; input char into AL
sub
al,20h
; convert to upper case
mov
CHAR,al
; and store it
;display on the next line
mov
dx,offset MSG2 ; get second message
mov
ah,9
; display string function
int
21h
; display message and upper case
;return to DOS
Exit:
mov
ah,4Ch
; DOS function: Exit program
mov
al,exCode
; Return exit code value
int
21h
; Call DOS. Terminate program
MAIN ENDP
END
MAIN
; End of program / entry point
13 of 13
11/22/2015 10:29 AM