You are on page 1of 9

-1.

model small
.stack 100h
.code
first proc
mov dl,65
mov ah,02
int 21h
mov dl,'b'
int 21h
mov ax,4c00h
int 21h
first endp
end first
=============================================
.model small
.stack 100h
.code
main proc
mov cx,26
mov dl, 65
mov ah,2
abc:
int 21h
inc dl
loop abc
mov ax,4c00h
int 21h
main endp
end main
=============================================
.model small
.stack 100h
.data
xyz db "KUDCS",0dh,0ah,"$"
.code
main proc
mov ax, @data
mov ds, ax
mov ah, 9
mov dx, offset xyz
int 21h
mov ax, 4c00h
int 21h
main endp
end main
=============================================
;; This program displays the following sequence:
;; AaBbCcDdEe........Zz
;;
.model small
.stack 100h
.code
main proc
mov cx,26 ; or mov cx,1ah
mov bl,41h ; Code for 'A'
mov bh,61h ; Code for 'a'
mov ah,2
lbl1:
mov dl,bl
int 21h
mov dl,bh
int 21h
inc bl
inc bh
loop lbl1
mov ax,4c00h
int 21h
main endp
end main
=============================================
;; This program shows number on screen but single digit number
;; This program is not for multiple digit numbers
.model small
.stack 100h
.data
arr1 dw 2h,3h,4h,5h,9h

.code
main proc
mov ax,@data
mov ds,ax
mov cx,5
mov si,0
zcopy:
mov bx,offset arr1
mov dh,0
mov dl,[bx+si]
add dl,30h
mov ah,02
int 21h
inc si
inc si
loop zcopy
mov ax,4c00h
int 21h
main endp
end main
=============================================
title program to show decimal no. on screen
.model small
.stack 100h
.code
main proc
mov cx,635
mov ax,cx
mov bx,10
mov dx,0
mov cx,0
loop1:
div bx
push dx
mov dx,0
inc cx
cmp ax,0
jnz loop1
loop2:
pop dx
add dx,30h
mov ah,2
int 21h
loop loop2
mov ax,4c00h
int 21h
main endp
end main
=============================================
;; Display the contents of a register in binary no. format
;;
OR
;; Display the bit patterns of the contents of a register
.model small
.stack 100h
.code
main proc
mov cx,16
; cx contains how many bits in a register to process
mov bx,21
mov ah,2
looplbl:
mov dl,31h
shl bx,1
jc disp
dec dl

; Suppose bx contains 21d or 15h or


; 00000000 00010101b
; Service to display a character
; Move code of one in dl register
; Shift left bx register, if carry
; flag is high it means the left most
; bit is one otherwise dl contains the
; ASCII code for zero which is 30h

disp:
int 21h
loop looplbl
mov ax,4c00h
int 21h
main endp
end main
=============================================

-2Title macro and procedure


.model small
.stack 100h
.data
msg db "KUDCS",'$'
.code
show macro string
mov dx,offset string
mov ah,9
int 21h
endm
newline proc
mov ah,2
mov dl,0ah
int 21h
mov dl,0dh
int 21h
ret
newline endp
main proc
mov ax,@data
mov ds,ax
mov cx,10
abc:
show msg
call newline
loop abc
mov ax,4c00h
int 21h
main endp
end main
=============================================
;; Use of include: just write code in a separate file and
;; write include to include it in another file
Title takes any integer and count it
.model small
.stack 100h
.data
num1 db 0
msg db "Enter any integer: ",'$'
crlf db 0ah,0dh,'$'
.code
include numbers.h
main proc
mov ax,@data
mov ds,ax
DispStr msg
call NumRead
dispstr crlf
mov dl,01h
mov dh,0
mov cx,bx
label1:
DispNum dx
dispstr crlf
inc dl
loop label1
Exit
main endp
end main
--------------------------------------------------------------------;; Name of this file is numbers.h
;; This file is used to be included in above program
DispStr macro string
push dx
push ax
mov dx,offset string
mov ah,9
int 21h
pop ax
pop dx
endm
Exit macro
mov ax,4c00h
int 21h
endm

NumRead proc
mov ax,0
mov bx,0
a1:
mov ah,1
int 21h
cmp al,0dh
je done
cmp al,30h
jl warn1
cmp al,39h
jg warn1
sub al,30h
cbw
xchg ax,bx
mov cx,10d
mul cx
xchg ax,bx
add bx,ax
jmp a1
warn1:
mov ah,2
mov dl,0ah
int 21h
mov dl,0dh
int 21h
Exit
done:
ret
NumRead endp
DispNum macro numvar
push ax
push bx
push cx
push dx
mov ax,numvar
mov bx,10
mov dx,0
mov cx,0
cd:
div bx
push dx
mov dx,0
inc cx
cmp ax,0
jnz cd
wd:
pop dx
add dx,30h
mov ah,2
int 21h
loop wd
pop dx
pop cx
pop bx
pop ax
endm
=============================================
Title takes any integer and print the table of it
.model small
.stack 100h
.data
num1 db 0
msg db "Enter any integer for table: ",'$'
msg2 db "Here is the table:",0ah,0dh,'$'
crlf db 0ah,0dh,'$'
.code
include numbers.h
main proc
mov ax,@data
mov ds,ax
DispStr msg
call NumRead
mov num1,bl
dispstr crlf
dispstr msg2
mov bl,01h
mov cl,0ah
label1:
mov al,num1
mul bl

-3mov dx,ax
DispNum dx
dispstr crlf
inc bl
loop label1
Exit
main endp
end main
=============================================
;; This program totals all the odd nos. between 1 and 100
;; and then display the sum
;;
.model small
.stack 100h
.code
include numbers.h
main proc
mov ax,0
mov bx,1 ; First odd no.
lbl:
add ax,bx
inc bx
inc bx

; Add odd no. in ax to get the sum


; Get next odd no. by adding two
; in the previous one

cmp bx,100 ; repeat this process upto 100th term


jl lbl
DispNum ax ; Display the sum
mov ax,4c00h
int 21h
main endp

mov bp,sp
push ax
push bx
push cx
push dx
push si
mov si,[bp+4]
mov ax,[si]
;; mov ax,[[bp+4]] can be used but it will
;; collect data from stack segment not from
;; data segment because bp is a pointer
;; register in stack segment
mov cx,0
mov si,0ah
lbl1:
mov dx,0
div si
push dx
inc cx
cmp ax,0
jg lbl1
mov ah,2
lbl2:
pop dx
add dl,30h
int 21h
loop lbl2
pop si
pop dx
pop cx
pop bx
pop ax
pop bp
ret 2
dispresult endp
end main

=============================================
=============================================
.model small
.stack 100h
.data
firstparam dw 8
secondparam dw 12
addresult dw 0
.code
main proc
mov ax,@data
mov ds,ax
lea bx,addresult
push bx
lea bx,secondparam
push bx
lea bx,firstparam
push bx
call c_conv
lea bx,addresult
push bx
call dispresult
mov ax,4c00h
int 21h
main endp
c_conv proc
push bp
mov bp,sp
push ax
push bx
mov si,[bp+4]
mov ax,[si]
;; mov ax,[[bp+4]] can be used but it will
;; collect data from stack segment not from
;; data segment because bp is a pointer
;; register in stack segment
mov si,[bp+6]
mov bx,[si]
add ax,bx
mov si,[bp+8]
mov [si],ax
pop bx
pop ax
pop bp
ret 6
c_conv endp
dispresult proc
push bp

;; Program to display the factorial of a number stored in ax


.model small
.stack 100h
.data
result dw 0
.code
include numbers.h
main proc
mov ax,@data
mov ds,ax
mov ax,3
push ax
call factorial
DispNum result
mov ax,4c00h
int 21h
main endp
factorial proc
push bp
mov bp,sp
mov ax,[bp+4]
cmp ax,1
ja l1
mov ax,1
jmp l2
l1:
dec ax
push ax
call factorial
mov bx,[bp+4]
mul bx
l2:
pop bp
mov result,ax
ret 2
factorial endp
end main
=============================================
;;
;; This program takes input two numbers and then multiply
;; them without using the MUL instruction
;;

-4.model small
.stack 100h
.data
mssg1 db 'Enter first number : $'
mssg2 db 'Enter Second number: $'
mssg3 db 'Their product is : $'
crlf db 0ah,0dh,'$'
.code
include numbers.h
main proc
mov ax,@data
mov ds,ax
DispStr mssg1
call NumRead ; Procdure to take input a number in bx
DispStr crlf
push bx
DispStr mssg2
call NumRead ; Procdure to take input a number in bx
DispStr crlf
push bx
DispStr mssg3
pop bx
pop ax
mov dx,0
; dx is used to hold the product
addloop:
test bx,1 ; Test that last bit of bx is 0 or not
jz shifting
add dx,ax ; Add ax in the product
shifting:
shl ax,1
; Shift ax by one bit to be added in
; dx to resemble multiplication
shr bx,1
; Shift bx to collect the last bit
jnz addloop ; Check that all bits are processed or not
mov ax,dx ; Now display the product
DispNum ax
exitprog:
exit
main endp
end main
=============================================
.model small
.stack 100h
.data
var1 dd 08ac392beh
var2 dd 0fa3b9ec6h
result dw 3 dup(0)
dummy db 0
.code
main proc
mov ax,@data
mov ds,ax
mov si,offset var1
mov di,offset var2
mov bx,offset result
mov cx,2
clc ; Clears carry flag
; CMC complements carry flag
lbl1:
mov ax,[si]
adc ax,[di]
pushf
mov [bx],ax
add si,2
add di,2
add bx,2
popf
loop lbl1
adc word ptr [bx],0
mov cx,6
mov bx,offset dummy
dec bx
mov ah,2
lbl2:
mov dl,[bx]
mov dh,dl
and dl,0f0h
push cx
mov cl,4
shr dl,cl
pop cx
add dl,30h

cmp dl,3ah
jl disp1
add dl,7
disp1:
int 21h
mov dl,dh
and dl,0fh
add dl,30h
cmp dl,3ah
jl disp2
add dl,7
disp2:
int 21h
dec bx
loop lbl2
mov ax,4c00h
int 21h
main endp
end main
=============================================
;; This program input a number and then test
;; is it prime or not
.model small
.stack 100h
.data
mssg db 'Enter a number: $'
prime db 0ah,0dh,'It is a prime number $'
notprime db 0ah,0dh,'It is not a prime number $'
crlf db 0ah,0dh,'$'
.code
include numbers.h
main proc
mov ax,@data
mov ds,ax
DispStr mssg
call NumRead ; Procdure to take input a number in bx
mov di,1 ; di is used to indicate the nature
; of the number
; 1 = Number is not prime
; 2 = Number is prime
mov cx,2
divlbl:
; Now continuously divide the given number
mov dx,0
; until range is reached or any remainder
mov ax,bx ; becomes zero
div cx
cmp dx,0
je exitprog
inc cx
cmp cx,bx
jl divlbl
mov di,2
exitprog:
; Now display the nature of input number
mov ah,9
mov dx,offset prime
cmp di,2
je dispmssg
mov dx,offset notprime
dispmssg:
int 21h
Exit
main endp
end main
=============================================
.model small
.stack 100h
.data
arr1 db '$','S','C','D','U','K'
arr2 db 6 dup(0)
.code
main proc
mov ax,@data
mov ds,ax
mov es,ax
mov di,offset arr2
mov si,offset arr2
dec si

-5mov cx,6
call revcopy
mov dx,offset arr2
mov ah,9
int 21h
mov ax,4c00h
int 21h
main endp
revcopy proc
copyloop:
mov al,byte ptr[si]
mov byte ptr[di],al
dec si
inc di
loop copyloop
ret
revcopy endp
end main
=============================================
title Generating sound
.model small
.stack 100h
speaker equ 61h
timer equ 42h
delay equ 0dfffh
.code
main proc
in al,speaker
push ax
or al,00000011b
out speaker,al
mov al,200
l2:
out timer,al
mov cx,delay
l3:
loop l3
sub al,1
jnz l2
pop ax
and al,11111100b
out speaker,al
mov ax,4c00h
int 21h
main endp
end main
=============================================
title Macros
.model small
.stack 100h
disp_char macro char
local lp4
ifnb <char>
mov dl,char
else
mov dl,'?'
endif
mov cx,4
mov ah,2
lp4:
int 21h
loop lp4
endm
.code
main proc
disp_char 'a'
disp_char
mov ah,4ch
int 21h
main endp
end main
=============================================
; To create .com file
; Use /t switch with tlink
.model tiny
.code
org 100h

start:
jmp main
msg db 'hello$'
main proc
lea dx,msg
mov ah,9
int 21h
mov ah,4ch
int 21h
main endp
end start

;get message
;display string function
;display 'hello'
;dos exit

=============================================
.model small
.stack 100h
.data
str1a db 20
str1b db ?
str1c db 20 dup(?)
str2 db 20 dup(?)
newline db 0dh,0ah,'$'
msg1 db 'Type any string: ','$'
msg2 db 'You have typed: ','$'
.code
main proc
mov ax,@data
mov ds,ax
mov es,ax
mov dx,offset msg1
mov ah,9
int 21h
mov dx,offset str1a
mov ah,0ah
int 21h
mov ch,0
mov cl,str1b
push cx
mov si,offset str1c
mov di,offset str2
cld
rep movsb
pop cx
mov bx,cx
mov str2+[bx],'$'
mov ah,9
mov dx,offset newline
int 21h
mov dx,offset msg2
int 21h
mov dx,offset str2
int 21h
mov ax,4c00h
int 21h
main endp
end main
=============================================
.model small
.stack 100h
.code
main proc
mov ax,@data
mov ds,ax
mov ah,6
mov al,0
mov ch,0
mov cl,0
mov dh,24
mov dl,79
mov bh,7
int 10h
mov ah,2
mov bh,0
mov dh,0
mov dl,0
int 10h
mov ah,9
mov dx,offset msg1
int 21h
mov dx, offset msg2
int 21h
mov ax,4c00h

-6int 21h
main endp
.data
msg1 db 'Department of Computer Science',0ah,0dh,'$'
msg2 db 'University of Karachi',0ah,0dh,'$'
end main

call convert
mov ah,4ch
int 21h
main endp
end main

;convert to upper case


;dos exit

----------------------------------------------------------------------------=============================================
.model small
.stack 100h
.code
main proc
;set ds to active display page
mov ax,0b800h ;color active display page
mov ds,ax
mov cx,2000 ;80*25 = 2000 words
mov di,0
;initialize di
;fill active display page
fill_buf:
mov word ptr[di],1441h ;red a on blue
add di,2
;go to next word
loop fill_buf ;loop until done
mov ah,4ch
int 21h
main endp
end main

; This is the second program linked with above program


public convert
.model small
.data
msg db
0dh,0ah,'in upper case it is '
char db
-20h,'$'
.code
convert proc near
;converts char in al to upper case
push bx
push dx
add char,al
;convert to upper case
mov ah,9
;display string fcn
lea dx,msg
;get msg
int 21h
;display it
pop dx
pop bx
ret
convert
endp
end

=============================================
=============================================
;draws horizontal line in high res
;in row 100 from col 301 to col 600
.model small
.stack 100h
.code
main proc
;set graphics mode
movax,6 ;select mode 6, hi res
int 10h
;draw line
movah,0ch ;write pixel
moval,1 ;white
movcx,301 ;beginning col
movdx,100
;row
l1: int 10h
inc cx
;next col
cmpcx,600 ;more columns?
jle l1 ;yes, repeat
;read keyboard
movah,0
int 16h
;set to text mode
movax,3 ;select mode 3, text mode
int 10h
;return to dos
movah,4ch ;return
int 21h ;to dos
main endp
end main
=============================================
;;
;; This is an example of linked programs
;; Suppose this program name is a1.asm and
;; second program name is a2.asm
;; Now assemble both programs separately
;; and then linked them as:
;; tlink a1 a2
;; a1.exe program is generated
.model small
extrn convert: proc
.stack 100h
.data
msg db
'enter a lower case letter:$'
.code
main proc
mov ax,@data
mov ds,ax
;initialize ds
mov ah,9
;display string fcn
lea dx,msg
;get msg
int 21h
;display it
mov ah,1
;read char fcn
int 21h
;input char

; program that displays the current time


;
extrn get_time:near
.model small
.stack 100h
.data
time_buf db '00:00:00$';time buffer hr:min:sec
.code
main proc
mov ax,@data
mov ds,ax ;initialize ds
;get and display time
lea bx,time_buf ;bx points to time_buf
call get_time ;put current time in time_buf
lea dx,time_buf ;dx points to time_buf
mov ah,09h
;display time
int 21h
;exit
mov ah,4ch
;return
int 21h ;to dos
main endp
end main
----------------------------------------------------------------------------public get_time
.model small
.code
get_time proc
;get time of day and store ascii digits in time buffer
;input: bx = address of time buffer
mov ah,2ch
;gettime
int 21h ;ch = hr, cl = min, dh = sec
;convert hours into ascii and store
mov al,ch ;hour
call convert
;convert to ascii
mov [bx],ax ;store
;convert minutes into ascii and store
mov al,cl
;minute
call convert
;convert to ascii
mov [bx+3],ax ;store
;convert seconds into ascii and store
mov al,dh ;second
call convert
;convert to ascii
mov [bx+6],ax ;store
ret
get_time endp
;
convert proc
;converts byte number (0-59) into ascii digits
;input: al = number
;output: ax = ascii digits, al = high digit, ah = low digit
mov ah,0
;clear ah

-7mov dl,10
;divide ax by 10
div dl ;ah has remainder, al has quotient
or ax,3030h ;convert to ascii, ah has low digit
ret
;al has high digit
convert endp
end
=============================================
.model small
.stack 100h
.data
doscall equ 21h
var1 label word
var2 db 41h
var3 db 44h
var4 label byte
var5 dw 464Ah
.code
main proc
mov ax,@data
mov ds,ax
mov dx,var1
mov ah,2
int doscall ; A
mov dl,var4
int doscall ; J
mov dx,var5
int doscall ; J
mov dx,word ptr var4
int doscall ; J
mov dl,dh
int doscall ; F
mov dx,word ptr var2
int doscall ; A
mov dl,byte ptr var5
int doscall ; J
mov dl,byte ptr var1
int doscall ; A
mov bx,offset var1
mov dl,[bx]
int doscall ; A
mov dx,word ptr[bx]
int doscall ; A
mov bx,offset var4
mov dx,word ptr[bx]
int doscall ; J
mov bx,offset var5
mov dx,word ptr[bx]
int doscall ; J
mov bx,offset var3
mov dx,word ptr[bx]
int doscall ; D
mov dl,dh
int doscall ; J
push var5
mov bp,sp
mov dl,[bp]
int doscall ; J
mov dx,word ptr[bp]
int doscall ; J
mov dl,[bp+1]
int doscall ; F
pop dx
mov ax,4c00h
int doscall
main endp
end main

start:
push ds
sub ax,ax
push ax
mov ax,strg
mov ds,ax
lea dx,msg
mov ah,9
int 21h
ret
main endp
code ends
end start
=============================================
extrn proc1:far
public var1
s_seg segment stack
db 100h dup(0)
s_seg ends
d_seg segment public 'mydata'
var1 db 41h
d_seg ends
c_seg segment
assume cs:c_seg,ds:d_seg,ss:s_seg
main proc
mov ax,d_seg
mov ds,ax
mov dl,var1
mov ah,2
int 21h
mov dh,0
push dx
call proc1
mov ax,4c00h
int 21h
main endp
c_seg ends
end main
---------------------------------------------------------------------------public proc1
extrn var1:byte
d_seg segment public 'mydata'
var2 db 42h
d_seg ends
c_seg segment
assume cs:c_seg,ds:d_seg
proc1 proc far
;mov ax,d_seg
;mov ds,ax
push bp
mov bp,sp
mov dx,[bp+6]
;pop dx
mov ah,2
int 21h
mov dl,var1
mov ah,2
int 21h
mov dl,var2
int 21h
pop bp
ret 2
proc1 endp
c_seg ends
end

=============================================
; Another style for writing Assembly programs
stk segment stack
db 100h dup(0)
stk ends
strg segment
msg db 'KUDCS$'
strg ends
code segment
main proc far
assume cs:code,ds:strg,ss:stk

=============================================
;;
;; This program displays first ten fibonacci numbers
;;
.model small
.stack 100h
.code
include numbers.h
main proc
mov cx,9 ; Initialize the counter to display fibonacci nos.
mov ax,1
mov bx,0
DispNum ax ; Display first no.
fabnc:

-8mov dx,ax ; Preserve the answer


add ax,bx ; Add two nos. ax contains the new no.
DispNum ax ; Display the new no.
mov bx,dx ; Collect previous number
loop fabnc
mov ax,4c00h
int 21h
main endp
end main
=============================================
.model small
.stack 100h
.data
var1 db 0
var2 db 0
msg1 db 0dh,0ah,'First is 5 $'
msg2 db 0dh,0ah,'Second is -5 $'
msg3 db 0dh,0ah,'First is -5 $'
msg4 db 0dh,0ah,'Second is 5 $'
msg5 db 0dh,0ah,'First is greater $'
msg6 db 0dh,0ah,'Second is greater $'
.code
main proc
mov ax,@data
mov ds,ax
mov ah,9
lea dx,msg1
int 21h
lea dx,msg2
int 21h
mov var1,5
mov var2,-5
call jproc
lea dx,msg3
int 21h
lea dx,msg4
int 21h
mov var1,-5
mov var2,5
call jproc
mov ax,4c00h
int 21h
main endp
jproc proc
mov al,var1
mov cl,var2
cmp al,cl
ja lab
lbl1:
mov al,var1
mov cl,var2
cmp al,cl
jb lbl
lbl2:
mov al,var1
mov cl,var2
cmp al,cl
jg lgr
lbl3:
mov al,var1
mov cl,var2
cmp al,cl
jl lls
jmp exit
lab:
lea dx,msg5
int 21h
jmp lbl1
lbl:
lea dx,msg6
int 21h
jmp lbl2
lgr:
lea dx,msg5
int 21h
jmp lbl3
lls:
lea dx,msg6
int 21h
exit:
ret
jproc endp

end main
============================================
.model small
.stack 100h
.code
main proc
l1:
mov ah,10h
int 16h
mov dl,al
mov ah,2
int 21h
cmp al,1bh
jne l1
mov ax,4c00h
int 21h
main endp
end main
============================================
.model small
.stack 100h
.code
main proc
mov ax,0
int 33h
mov ax,1
int 33h
mov bh,00010111b
disp:
mov ah,6
mov al,0
mov ch,8
mov cl,30
mov dh,16
mov dl,50
int 10h
mov bl,0
push bx
press:
mov ax,3
int 33h
cmp bx,2
je exit
cmp bx,1
jne press
chk:
mov ax,5
mov bx,0
int 33h
and ax,1
cmp ax,1
je chk
pop bx
cmp bh,00010111b
je red
mov bh,00010111b
jmp disp
red:
mov bh,01000111b
jmp disp
exit:
pop bx
mov ax,4c00h
int 21h
main endp
end main
end
=================================================
# include <stdio.h>
void main(void) {
char alpha; alpha='A';
printf("%c",alpha);
asm mov dl,65
asm mov ah,2
asm int 21h
asm mov ax,4c00h
asm int 21h
}
==============================================
public _addproc ; Name it asmcode.asm

-9dosseg
.model small
.code
_addproc proc near
push bp
mov bp,sp
mov ax,[bp+8]
add ax,[bp+6]
add ax,[bp+4]
pop bp
ret
_addproc endp
end
---------------------------------------------------------------# include <stdio.h> // Name it asmcall.cpp
extern "C" int addproc(int,int,int);
void main(void) {
int total=0; int a=1; int b=2; int c=3;
printf("\nAdding 1+2+3: ");
total=addproc(a,b,c);
printf("Total=%d\n",total); }
---------------------------------------For Compilation using TCC:
First make tasm directory in path and then use:
tcc efnlcd Id:\tc\include Ld:\tc\lib asmcode.asm asmcall
For Compilation using IDE:
- Options---- transfer---- Turbo Assembler---- Edit---- Program Path
- Project---- Open Project---- Filename
- Project---- Add Item---- asmcall.cpp then asmcode.asm
- Compile---- Compile
- Compile---- Make
- Run---- Run
- Project---- Close Project

You might also like