You are on page 1of 3

ELECTRICAL AND ELECTRONIC ENGINEERING

SAMPLE PROGRAMS
Directives are statements that give directions to the assembler about how it should translate the assembly
language instructions into machine code.
Directives are used by the assembler to organize the program as well as other output files. Examples are
SEGMENT, DB (allocation of memory in bytes), DW (2 bytes allocation at a time), DD (2 words
allocation at a time), DQ (4 words), DT (10 bytes of memory space), ENDS, ASSUME, END, and ENDP
An assembly language instruction consists of four fields,

A tab makes the program listing neat and easy to debug.
Include comments in your program for documentation and passing information.
Brackets indicate that the field is optional. Brackets are not typed.
1. The label field allows the program to refer to a line of code by name.
2. In a line of assembly language program there can be mnemonic (instruction) and operand(s). Separate
operands by a comma (,).
;****************8086 Assembly Language Program********************
;Exercise 1: Display of strings of data

;This is the first 8086 Assembly language program
; Confirm the segment initialization and usage
.MODEL SMALL ;Specifies the memory model the program will use
;in the simplified segment definition. In this case
;64Kb for code and 64Kb of data memory is reserved.
.STACK ;Allocate stack. Need to be initialized
.CODE ;A directive to start code segment.
START: ;Generally a good name to use for entry point
;or MAIN: Also used by some programmers.
JMP BEGIN ; Unconditional jump to label BEGIN
welcome DB "Hello World! This is the message.$"
; Dollar sign used to terminate string

BEGIN: ; code starts here
mov dx, OFFSET welcome
; copies logical address (offset) of welcome to DX
;**************** Procedure of loading the segment to which data resides *******************
mov ax, SEG welcome
; Copies default segment address where the welcome string is stored to
;AX
mov ds, ax
;******************** End of procedure *************************************
mov ah, 9 ;move 9 into ah to call function 9 of interrupt
int 21h ;21h responds to displaying a string to the screen.
mov ax,4c00h ;move value 4c00 hex into AX.
Int 21h ;call interrupt 21h, subroutine 4c which returns
; controls to DOS, otherwise it would crash. 00h is an
;error level returned to DOS.
END START ;end here


; Exercise 2 Display of decimal numbers
.model small ; request for memory allocations
.stack 100h ;stack initialization
.data ;start of the data segment
Data1 dw 003EH ;variable declaration and initialization
Data2 dw 004FH
sum DW ?
.code ;start of the code segment

; *************Entry point to your program *****************
main:
;Assigning address to Pointers and data segment from default values

mov ax, @data ;load the start address of the data segment into AX
mov ds, ax ;DS now points to the local variables start address
mov ax, data1 ;loads the first operand into AL
mov bx, data2 ;loads the second operand into BL
add ax, bx ;increases the contents of AL with the contents of BL(addition)
mov sum, ax ;stores the result in location SUM

; ******* Loop for changing Hex to Dec. *******
mov bx,10 ;we use long division to convert HEX to DEC
mov cx,0 ;the counter

divide:
xor dx,dx ;clear the dx register
div bx ;perform the long division
push dx ;push the remainder into the stack
inc cx ;we count the number of divisions until the quotient is zero
cmp ax,0
jne divide ;loop

;****** Printing of consecutive numbers *******
print:
mov ah,02h ;output the character(remainder)
pop dx ;character to output
add dl,30h ;convert into ascii code for screen output
int 21h
LOOP print ;loop against the value of cx

;****** Exiting your program to OS

mov ah, 4ch
Int 21h ;return to the operating system(DOS)

; ***************Exit point*****************

END main


The following Memory Models can be used:
1. SMALL MODEL (.MODEL SMALL): The model uses maximum of 64K bytes for Code and
2. 64K bytes for Data (Code<=64K and Data <=64K). This model is the most widely used
3. memory model and is sufficient for all the programs to be used in this course.
4. MEDIUM MODEL, (.MODEL MEDIUM): The model uses maximum of 64K bytes for Data
5. and Code can exceed 64K bytes (Code>64K and Data <=64K).
6. COMPACT MODEL, (.MODEL COMPACT): The model uses maximum of 64K bytes for
7. Code and Data can exceed 64K bytes (Code<=64K and Data >64K).
8. LARGE MODEL, (.MODEL LARGE): Both Code and Data can exceed 64K bytes. However
9. no single data set (i.e. array) can exceed 64K bytes (Code>64K and Data >64K).
10. HUGE MODEL, (.MODEL HUGE): Both Code and Data can exceed 64K bytes. Additionally,
11. a single data set (i.e. array) can exceed 64K bytes (Code>64K and Data >64K).