Introduction to 8086 Assembly Language
CS 272
Sam Houston State University
Dr. Tim McGuire


Structure of an assembly language program

The Header %TITLE "Sample Header"
.8086
.model small
.stack 256
Named Constants Equates Count EQU 10
Element EQU 5
Size = Count * Element
MyString EQU "Maze of twisty passages"
Size = 0
The Data Segment Reserving space for variables .data
numRows    DB 25
numColumns DB ?
videoBase  DW 0800h
aTOm DB "ABCDEFGHIJKLM" Program Data and Storage Defining Data DW 40Ch,10b,-13,0 DB 255,?,-128,'X' Naming Storage Locations ANum DB -4
     DW 17
ONE
UNO  DW 1
X    DD ?
Arrays X  DW  040Ch,10b,-13,0
Y  DB  'This is an array'
Z  DD  -109236, FFFFFFFFh, -1, 100b
DUP DB  40 DUP(?)
DW  10h DUP(0)
DB  3 DUP("ABC")
DB  4 DUP(3 DUP (0,1), 2 DUP('$'))
Word Storage Directive      Bytes in Storage
DW 256         00 01
DD 1234567h    67 45 23 01
DQ 10          0A 00 00 00 00 00 00 00
X DW 35DAh     DA 35 Low byte of X is at X, high byte of X is at X+1
The Program Body An example Label    Mnemonic   Operand     Comment
---------------------------------------------------------
         .data
exCode   DB         0          ;A byte variable
myWord   DW         ?          ;Uninitialized word var.
         .code
MAIN    PROC
         mov        ax,@data   ;Initialize DS to address
         mov        ds,ax      ; of data segment
         jmp        Exit       ;Jump to Exit label
         mov        cx,10      ;This line skipped!
Exit:    mov        ah,04Ch    ;DOS function: Exit prog
         mov        al, exCode ;Return exit code value
         int        21h        ;Call DOS. Terminate prog
MAIN     ENDP                  ;End Program
         END        MAIN       ; and specify entry point
The Label Field Legal and Illegal Labels The Mnemonic Field The Operand Field NOP             ;no operands -- does nothing
INC AX          ;one operand -- adds 1 to the contents of AX
ADD WORD1,2     ;two operands -- adds 2 to the contents
                ; of memory word WORD1
The Comment Field Good and Bad Comments MOV CX,0 ;move 0 to CX MOV CX,0 ;CX counts terms, initially 0 ;
; Initialize registers
;
    MOV AX,0
    MOV BX,0
The Closing MAIN  ENDP      ;End of program
      END MAIN  ; entry point for linker use
Assembling a Program edit myprog.asm tasm myprog produces myprog.obj tlink myprog produces myprog.exe Dealing with Errors Using the Debugger tasm /zi myprog
tlink /v myprog
td myprog
.COM and .EXE files Ending a Program Data Transfer Instructions Examples Sample MOV Instructions b    db  4Fh
w    dw  2048 mov bl,dh
mov ax,w
mov ch,b
mov al,255
mov w,-100
mov b,0
LoByte LABEL BYTE
aWord  DW    97F2h
Addresses with Displacements b    db  4Fh, 20h, 3Ch
w    dw  2048, -100, 0 mov bx, w+2
mov b+1, ah
mov ah, b+5
mov dx, w-3
eXCHanGe Arithmetic Instructions ADD dest, source
SUB dest, source
INC dest
DEC dest
NEG dest
ADD and INC Examples SUB, DEC, and NEG Examples Type Agreement of Operands Translation of HLL Instructions Program Segment Structure .DATA
.CODE
.STACK size
Memory Models 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
  • Define the stack size
  • Declare variables
  • Write code
  • Mark the end of the source file
  • Input and Output Using 8086 Assembly Language Interrupts Output to Monitor Output a String Print String Example %TITLE "First Program -- HELLO.ASM"
            .8086
            .MODEL   small
            .STACK   256
            .DATA
    msg     DB      "Hello, World!$"
            .CODE
    MAIN    PROC
            mov     ax,@data     ;Initialize DS to address
            mov     ds,ax        ; of data segment
            lea     dx,msg       ;get message
            mov     ah,09h       ;display string function
            int     21h          ;display message
    Exit:   mov     ah,4Ch       ;DOS function: Exit program
            mov     al,0         ;Return exit code value
            int     21h          ;Call DOS. Terminate program
    MAIN    ENDP                 ;End of program
            END     MAIN         ; entry point
    Input a Character 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