FIND REVERSE OF EACH WORD IN A STRING - Assembly Language - MASM

Program:

        ;FIND REVERSE OF EACH WORD IN A STRING


.model small
.stack 200h
.data
msg1 db 10,13,'Enter the string:$'
msg2 db 10,13,'Reverse of the string:$'
newline db 10,13,' $'
buff db 60 dup(?)
flag db 0
stackempty db 1
.code
print macro msg
push ax
push dx
mov ah,09h
mov dx,offset msg
int 21h
pop dx
pop ax
endm
.startup
print msg1
print newline
mov si,offset buff
mov cx,0
mov bx,0
nxtchar:
mov ah,01h
int 21h
inc bx
cmp al,' '
je popy
cmp al,0dh
je popy
mov ah,0
push ax
mov stackempty,0
inc cx
jmp nxtchar
popy:
cmp stackempty,1
je stackkaali
cmp al,0dh
jne noflag
mov flag,1
noflag:
pop ax
mov [si],al
inc si
loop noflag
inc si
mov stackempty,1
cmp flag,1
je display
jmp nxtchar
stackkaali:
inc si
jmp nxtchar
display:
print msg2
print newline
mov si,offset buff
mov cx,bx
dispnxt:mov dh,0
mov dl,[si]
mov ah,2
int 21h
inc si
loop dispnxt
.exit
End
OUTPUT
F:\>rev
Enter the string:
kerala is called gods own country
Reverse of the string:
alarek si dellac sdog nwo yrtnuoc

Beep Sound - Assembly Language - MASM

Program:

.model tiny
.code
.startup
        mov bx,2000
        mov ax,34dch
        mov dx,12h
        div bx
        out 42h,al
        mov al,ah
        out 42h,al

        in al,61h
        or al,03h
        out 61h,al


        mov dx,0fffh
back:
        mov cx,0ffffh
same:
        loop same
        dec dx
        jnz back

        in al,61h
        xor al,03h
        out 61h,al

.exit
end

Beep Sound 1 - Assembly Language - MASM

Program:


GENERATION OF BEEP
.model tiny
.code
.startup
JMP INSTALL
ADD9 DD ?
;ISR to generate beep sound
beepISR PROC FAR
PUSH AX
PUSH BX
PUSH CX
PUSH DX
IN AL,61H
OR AL,03H
OUT 61H,AL ; Turn ON system speaker
MOV AL,0B6H ; program Counter 2
OUT 43H,AL
MOV BX,2000
MOV AX,34DCH
MOV DX,12H
DIV BX
OUT 42H,AL ;set the frequency of counter 2 to 2000Hz
MOV AL,AH
OUT 42H,AL
MOV CX,1FFFH
BACK2:
MOV DX,0FFFFH ; Delay
BACK1:
DEC DX
JNZ BACK1
LOOP BACK2
IN AL,61H
XOR AL,03H ; Turn OFF system speaker
OUT 61H,AL
PUSHF
CALL CS:ADD9 ; Call original ISR9
POP DX
POP CX
POP BX
POP AX
IRET
beepISR ENDP
INSTALL:
MOV AX,CS
MOV DS,AX ; overlap CS and DS
MOV AH,35H
MOV AL,09H
INT 21H
MOV WORD PTR ADD9,BX
MOV WORD PTR ADD9+2,ES ;Get the current vector 9 and save it in ADDR9
MOV AH,25H
MOV AL,09H
MOV DX,OFFSET beepISR ;install beepISR at Vector 9
INT 21H
MOV DX,OFFSET INSTALL
MOV CL,4
SHR DX,CL ; find the paragraph size in Dx
INC DX
MOV AH,31H ; exit to DOS as TSR
MOV AL,00H
INT 21H
end

Text Colour - Assembly Language - MASM

Program:
.model tiny
.code
.startup
    jmp install
    add8 dd ?
    tick db 0
    flag db 0bh
colorisr proc far
    push ax
    push bx
    push cx
    push di
   
    inc tick
    mov al,tick
    cmp al,5*18
    jb skip
    mov al,0
    mov tick,al
    mov al, flag
    cmp al,0bh
    je next1
    mov al,0bh
    jmp next2
next1:
    mov al,0ch
next2:
    mov flag,al
    mov bx,0b800h
    mov es,bx
    mov di,1
    mov cx,4000
back1:
    mov es:[di],al
    inc di
    inc di
    loop back1
    pushf
    CALL CS:add8
    pop di
    pop cx
    pop bx
    pop ax
colorisr endp
install:
    mov ax,cs
    mov ds,ax
    mov ah,35h
    mov al,08h
    int 21h
    mov word ptr add8,bx
    mov word ptr add8+2,es
    mov ah,25h
    mov al,08h
    mov dx,offset colorisr
    int 21h
    mov dx,offset install
    mov cl,4
    shr dx,cl
    inc dx
skip:
    mov ah,31h
    mov al,00
    int 21h
    end

Bubblesort - Assembly Language - MASM


BUBBLE SORT

Program:
.model small
.stack 200H
.data
  msg1 DB 10,13,'Enter the limit::$'
  msg2 DB 10,13,'Enter the numbers::$'
  msg3 DB 10,13,'The numbers in ascending order is::$'
  comma DB ',$'
 newline DB 10,13,'$'
 count db ?
 tab1 db 20 dup(0)
.code
  printstring MACRO msg
 PUSH AX
 PUSH DX
 MOV AH,09H
 MOV DX,OFFSET msg
  INT 21H
 POP DX
 POP AX
 ENDM
.startup
  printstring msg1
  CALL readnumtoAX
  MOV count,AL
  MOV CL,count
  MOV CH,00
  MOV BX,1
  printstring msg2
rdnxt:
  printstring newline
  CALL readnumtoAX
  MOV tab1[BX],AL
  INC BX
  LOOP rdnxt
  MOV CL,count
  MOV CH,00
  CMP CX,1
  JE DONE
  nextpass:
  MOV DL,00
  MOV BX,01
nextj:
  MOV AL,tab1[BX]
  MOV AH,tab1[BX + 1]
  CMP AL,AH
  JLE skip
  MOV tab1[BX],AH
  MOV tab1[BX + 1],AL
  MOV DL,1
skip:
  INC BX
  CMP BX,CX
  JL nextj
  DEC CX
  jz done
  CMP DL,01H
  JE nextpass
done:
  MOV CL,count
  MOV CH,00
  MOV BX,01
  printstring msg3
 prnnxt:
  MOV AX,00
  MOV AL,tab1[BX]
  CALL displayAX
  printstring comma
  INC BX
  LOOP prnnxt
  MOV AH,4CH
  MOV AL,00H
  INT 21H
readnumtoAX PROC NEAR
  PUSH BX
  PUSH CX
  MOV CX,10
  MOV BX,00
bac:
  MOV AH,01H
  INT 21H
  CMP AL,'0'
  JB skip1
  CMP AL,'9'
  JA skip1
  SUB AL,'0'
  PUSH AX
  MOV AX,BX
  MUL CX
  MOV BX,AX
  POP AX
  MOV AH,00
  ADD BX,AX
  JMP bac
skip1:
  MOV AX,BX
  POP CX
  POP BX
  RET
 readnumtoAX ENDP
displayAX PROC NEAR
  PUSH DX
  PUSH CX
  PUSH BX
  PUSH AX
  MOV CX,0
  MOV BX,10
 back1:
  MOV DX,0
  DIV BX
  PUSH DX
  INC CX
  OR AX,AX
  JNZ back1
back2:
  POP DX
  ADD DL,30H
  MOV AH,02H
  INT 21H
  LOOP back2
  POP AX
  POP BX
  POP CX
  POP DX
  RET
displayAX ENDP
END

Output:
F:\>sort
Enter the limit::7
Enter the numbers::
13
9
32
1
11
26
20
The numbers in ascending order is::1,9,11,13,20,26,32,

Concatenate String & Check Palindrome - Assembly Language - MASM

Program:
.model small
.data
    mes1 db 10,13,"enter the first string",10,13,"$"
    mes2 db 10,13,"enter the second string",10,13,"$"
    mes3 db 10,13,"the concatinated string is:",10,13,"$"
    mes4 db 10,13,"the string is a palindrome",10,13,"$"
    mes5 db 10,13,"the string is not a palindrome",10,13,"$"
    arr db 100  

.code
.startup

    lea dx,mes1
    mov ah,09h
    int 21h
    lea dx,arr
    mov ah,0ah
    int 21h

    mov si,2
    mov bl,arr[1]
    mov bh,00h
    mov di,bx
    inc di
    inc di

    lea dx,mes2
    mov ah,09h
    int 21h

l1:
   mov ah,01h
   int 21h
   cmp al,13
   je l2
   mov arr[di],al
   inc di
   jmp l1

l2:
   lea dx,mes3
   mov ah,09h
   int 21h

l3:
   cmp si,di
   je l4
   mov dl,arr[si]
   mov ah,02h
   int 21h
   inc si
   jmp l3

l4:
    mov si,2
    dec di
    
l5:
     mov bl,arr[si]
     mov bh,00h
     mov cl,arr[di]
     mov ch,00h

     cmp bl,cl
     jne l6
     cmp di,si
     je l7
     inc si
     dec di
     jmp l5

l6:
     lea dx,mes5
     mov ah,09h
     int 21h
     jmp exit1

l7:
     lea dx,mes4
     mov ah,09h
     int 21h
     jmp exit1

exit1:
   .exit
   end

  

Reverse of a String - Assembly Language - MASM


REVERSE OF A STRING

Program:
.model small
.stack 200h
.data
   msg1 db 10,13,"Enter string:",10,13,'$'
   msg2 db 10,13,"Reverse of the string is:",10,13,'$'
   arr db 20 dup(?)
.code
   print MACRO msg
   PUSH AX
   PUSH DX
   MOV AH,09H
   MOV DX,offset msg
   INT 21H
   POP DX
   POP AX
   ENDM
.startup
   print msg1
   mov arr,18
   mov dx,offset arr
   mov ah,0ah
   int 21h
   lea dx,msg2
   mov ah,09h
   int 21h
   mov ch,0h
   mov cl,arr[1]
   mov di,cx
   inc di
l1:
   mov dl,arr[di]
   mov ah,02h
   int 21h
   dec di
   loop l1
  .exit
End

Output:
F:\>revv
Enter string:
PALINDROME
Reverse of the string is:
EMORDNILAP

Sum of Digits & Number of Digits - Assembly Language - MASM


Number of Digits, Sum of Digits & Reverse of a Number
 

Program:
.model small
.stack 200h
.data
   msg1 db "Enter the number::$"
   msg2 db 'Sum of digits is::$'
   msg3 db 'No. of digits is::$'
   msg4 db 10,13,"Reverse of the given number is::$"
   newline DB 10,13, ' $'
   sum dw 0
   NO dW 0
   cnt dw 0
   arr db 20 dup(?)
.code
   print MACRO msg
   push ax
   push dx
   mov ah,09h
   mov dx,offset msg
   INT 21H
   pop dx
   pop ax
ENDM
.startup
   print msg1
   call readnumtoAX
   print msg4
   mov cx,ax
   mov BX,10
   mov si,offset arr
   mov di,offset arr
DV1:
   mov ax,cx
   mov dh,00
   mov dl,00
   div BX
   mov cx,ax
   inc NO
   mov al,dl
   mov [si],al
   mov ah,00
   call displayAX
   inc si
   add SUM,DX
   mov ax,cx
  cmp    AX,0
  jne DV1
  mov AX,NO
   print newline
   print msg3
   CALL displayAX
   MOV AX,SUM
   print newline
   print msg2
CALL displayAX
  print newline
   ;mov cx,NO
  ;dis:
   ; mov ah,00
   ; mov al,arr[di]
   ; CALL displayAX
   ;inc di
   ;loop dis
.EXIT
readnumtoAX PROC NEAR
   PUSH BX
   PUSH CX
   MOV CX,10
   MOV BX,00
back:
   MOV AH,01H
   INT 21H
   CMP AL,'0'
   JB skip
   CMP AL,'9'
   JA skip
   SUB AL,'0'
   PUSH AX
   MOV AX,BX
   MUL CX
   MOV BX,AX
   POP AX
   MOV AH,00
   ADD BX,AX
   JMP back
skip:
   MOV AX,BX
   POP CX
   POP BX
   RET
readnumtoAX ENDP
displayAX PROC NEAR
   PUSH DX
   PUSH CX
   PUSH BX
   PUSH AX
   MOV CX,0
   MOV BX,10
back1:
   MOV DX,0
   DIV BX
   PUSH DX
   INC CX
   OR AX,AX
   JNZ back1
back2:
   POP DX
   ADD DL,30H
   MOV AH,02H
   INT 21H
   LOOP back2
   POP AX
   POP BX
   POP CX
   POP DX
   RET
displayAX ENDP
END

Output:
F:\>unt
Enter the number::7835
Reverse of the given number is::5387
No. of digits is::4
Sum of digits is::23

Check Prime or Not - Assembly Language Program - MASM


FIND WHETHER A NUMBER IS PRIME OR NOT
Program:

.model small
.stack 200H
.data
 msg1 DB 10,13,'Enter the number: $'
 msg2 DB 10,13,'The number is prime: $'
 msg3 DB 10,13,'The number is not prime: $'
 newline DB 10,13,'$'
 num dw ?
 que dw ?
.code
 print MACRO msg
 PUSH AX
 PUSH DX
 MOV AH,09H
 MOV DX,offset msg
 INT 21H
 POP DX
 POP AX
 ENDM
.startup
 print newline
 print msg1
 call readnumtoAX
 mov cx,2
 mov num,ax
 mov ax,num
 mov dx,0
 div cx
 mov que,ax
 mov ax,dx
NNN:
 cmp cx,que
 ja skip1
 cmp dx,00
 je skip2
 inc cx
 mov ax,cx
 mov dx,0
 mov ax,num
 div cx
 mov ax,dx
 jmp NNN
skip1:
 print newline
 print msg2
 jmp exit
skip2:
 print newline
 print msg3
exit:
.exit
readnumtoAX PROC NEAR
                                            ;STORE NUM. TO AX PROCEDURE DEF.
 PUSH BX
 PUSH CX
 MOV CX,10
 MOV BX,00
back:
 MOV AH,01H
 INT 21H
 CMP AL,'0'
 JB skip
 CMP AL,'9'
 JA skip
 SUB AL,'0'
 PUSH AX
 MOV AX,BX
 MUL CX
 MOV BX,AX
 POP AX
 MOV AH,00
 ADD BX,AX
 JMP back
skip:
 MOV AX,BX
 POP CX
 POP BX
 RET
readnumtoAX ENDP
displayAX PROC NEAR ;DISPLAY PROCEDURE CONTENTS
 PUSH DX
 PUSH CX
 PUSH BX
 PUSH AX
 MOV CX,0
 MOV BX,10
back1:
 MOV DX,0
 DIV BX
 PUSH DX
 INC CX
 OR AX,AX
 JNZ back1
back2:
 POP DX
 ADD DL,30H
 MOV AH,02H
 INT 21H
 LOOP back2
 POP AX
 POP BX
 POP CX
 POP DX
 RET
 displayAX ENDP
END

Output:
F:\>prm
Enter the number: 9
The number is not prime:
F:\>prm
Enter the number: 46
The number is not prime:
F:\>prm
Enter the number: 17
The number is prime:

Palindrome - Assembly Language - MASM


PALINDROME OR NOT

Program:
.model small
.stack 200H
.data
   msg1 DB 10,13,'enter the string: $'
   msg2 DB 10,13,'PALINDROME $'
   msg3 DB 10,13,'NOT PALINDROME $'
   newline DB 10,13,' $'
   count db ?
   tab db 20 dup(?)
.code
   print MACRO msg ;macro definition
   PUSH AX
   PUSH DX
   MOV AH,09H
   MOV DX,offset msg
   INT 21H
   POP DX
   POP AX
   ENDM
.startup
   print msg1
   print newline
   mov si ,offset tab
   mov di,offset tab
   mov dx,00
   mov cx,00
rdnxt:
   mov ah,01h
   int 21h
   mov [si],al
   inc si
   inc dx
   cmp al,'$'
   jne rdnxt
   dec si
   dec si
   dec dx
   mov cx,dx
   print newline
nxtchar:
   mov dl,[si]
   mov dh,[di]
   cmp dl,dh
   jne palin
   inc di
   dec si
   loop nxtchar
   print msg2
   jmp ext
palin:
   print msg3
ext:
   .exit
END

Output:
F:\>pal
enter the string:
malayalam$
PALINDROME
F:\>pal
enter the string:
computer$
NOT PALINDROME

Check Odd or Even - Assembly Language - MASM


CHECK WHETHER A NUMBER IS ODD OR EVEN

Program:
.model small
.stack 200H
.data
msg1 DB 10,13,' Number is odd.$'
msg2 DB 10,13,' Number is even.$'
msg3 DB 10,13,'Enter the no:$'
newline DB 10,13,'$'
sum DW 0
count DW ?
num DW ?
.code
print MACRO msg
PUSH AX
PUSH DX
MOV AH,09H
MOV DX,offset msg
INT 21H
POP DX
POP AX
ENDM
.startup
print newline
print msg3
CALL readnumtoAX
MOV BL,02
DIV BL
CMP AH,00
JE even1
print newline
print msg1
JMP last
even1:
print newline
print msg2
last:
.exit
readnumtoAX PROC NEAR
PUSH BX
PUSH CX
MOV CX,10
MOV BX,00
back:
MOV AH,01H
INT 21H
CMP AL,'0'
JB skip
CMP AL,'9'
JA skip
SUB AL,'0'
PUSH AX
MOV AX,BX
MUL CX
MOV BX,AX
POP AX
MOV AH,00
ADD BX,AX
JMP back
skip:
MOV AX,BX
POP CX
POP BX
RET
readnumtoAX ENDP
END

Output:

F:\>od
Enter the no:135
Number is odd.
F:\>od
Enter the no:156
Number is even.


Largest of Given Numbers - Assembly Language - MASM

Program:
.model small
.stack 200H
.data
 msg1 DB 10,13,'Enter the limit: $'
 msg2 DB 10,13,'Enter no.s:  $'
msg3 DB 10,13,'The largest is:$'
 newline DB 10,13,'   $'
large  DW ?

.code
   
  print MACRO msg
        PUSH AX
        PUSH DX
        MOV AH,09H
        MOV DX,offset msg
        INT 21H
        POP DX
        POP AX
        ENDM

.startup
       print msg1
       CALL readnumtoAX
       mov cx,ax
       print newline
       print msg2
       print newline 
       CALL readnumtoAX
       MOV large,AX
       DEC CX
bac:   
        print newline
        CALL readnumtoAX
        CMP AX,large
        JBE skip1
        mov large,ax
skip1:
        loop bac
        print msg3
        mov ax,large
        print newline
        CALL displayAX
.exit

;Store num to AX procedure definition
 readnumtoAX PROC NEAR

        PUSH BX
        PUSH CX
        MOV CX,10
        MOV BX,00
 back:  MOV AH,01H
        INT 21H
        CMP AL,'0'
        JB skip
        CMP AL,'9'
        JA skip
        SUB AL,'0'
        PUSH AX
        MOV AX,BX
        MUL CX
        MOV BX,AX
        POP AX
        MOV AH,00
        ADD BX,AX
        JMP back
skip:
         MOV AX,BX
         POP CX
         POP BX
         RET

 readnumtoAX ENDP


;Display procedure
 displayAX PROC NEAR               

     PUSH DX
     PUSH CX
     PUSH BX
     PUSH AX
        MOV CX,0
        MOV BX,10
back1:  MOV DX,0
        DIV BX
        PUSH DX
        INC CX
        OR AX,AX
        JNZ back1
back2:
        POP DX
        ADD DL,30H
        MOV AH,02H
        INT 21H
        LOOP back2
        POP AX
        POP BX
        POP CX
        POP DX
        RET
displayAX ENDP

END

Output:
Enter the limit: 4
Enter no.s:  4
  
   6
   8
   9
The largest  is:
   9

Generation of Fibonacci Series -Assembly Language - MASM

Program:
.model small
.stack 300H
.data
 msg1 DB 10,13,'Entre the count: $'
 msg2 DB 10,13,'FIBANACCI SERIES:  $'
 newline DB 10,13,'$'
 comma   DB ',$'
 prev1 DW 1
 prev2 DW 0
.code
   print MACRO msg                    ;macro definition
        PUSH AX
        PUSH DX
        MOV AH,09H
        MOV DX,offset msg
        INT 21H
        POP DX
        POP AX
        ENDM
.startup
       print msg1
       call readnumtoAX
       mov cx,ax                    ;program body
       mov ax,0                   
       print newline
       print msg2
       call displayAX
       print comma
       dec cx
       jz done
       mov ax,1
       call displayAX
       print comma
       dec cx
       jz done
repeat1:
       mov ax,prev1
       add ax,prev2
       call displayAX
       print comma
       mov bx,prev1
       mov prev2,bx
       mov prev1,ax
       loop repeat1
 done:
.exit
 readnumtoAX PROC NEAR ;STORE NUM. TO AX PROCEDURE DEF.
PUSH BX
      PUSH CX
      MOV CX,10
      MOV BX,00
 back:  MOV AH,01H
        INT 21H
        CMP AL,'0'
        JB skip
        CMP AL,'9'
        JA skip
        SUB AL,'0'
        PUSH AX
        MOV AX,BX
        MUL CX
        MOV BX,AX
        POP AX
        MOV AH,00
        ADD BX,AX
        JMP back
skip:
         MOV AX,BX
         POP CX
         POP BX
         RET
      readnumtoAX ENDP
displayAX PROC NEAR                       ;DISPLAY PROCEDURE CONTENTS OF AX
     PUSH DX
     PUSH CX
     PUSH BX
     PUSH AX
     MOV CX,0
     MOV BX,10
back1:  MOV DX,0
        DIV BX
        PUSH DX
        INC CX
        OR AX,AX
        JNZ back1
back2:  POP DX
        ADD DL,30H
        MOV AH,02H
        INT 21H
        LOOP back2
        POP AX
        POP BX
        POP CX
        POP DX
        RET
displayAX ENDP
END

Output:

F:\hwlab>edit fibonacci.asm
F:\hwlab>masm fibonacci.asm
F:\hwlab>link fibonacci.obj
F:\hwlab> fibonacci
Enter the count: 5
FIBONACCI SERIES: 0,1,1,2,3,
F:\hwlab> fibonacci
Enter the count:10
FIBONACCI SERIES: 0,1,1,2,3,5,8,13,21,34,

Factorial of a number - Assembly Language - MASM

Program:
.model small
.stack 200H
.data
 msg1 DB 10,13,'Enter the number:$'
 msg2 DB 10,13,'Factorial=$'
 newline DB '$' 
.code
print MACRO msg                ;macro definition
        PUSH AX
        PUSH DX
        MOV AH,09H
        MOV DX,offset msg
        INT 21H
        POP DX
        POP AX
        ENDM
.startup
       print msg1
       print newline
       CALL readnumtoAX
       mov cx,ax
       mov ax,01
ack1:    mul cx
    dec cx
    jz done
    jmp ack1
    print newline
done:
    print msg2
     call displayAX
    .exit
 readnumtoAX PROC NEAR        ;Procedure def. for reading char to ax reg.   
      PUSH BX
        PUSH CX
        MOV CX,10
        MOV BX,00
 back:  MOV AH,01H
        INT 21H
        CMP AL,'0'
        JB skip
        CMP AL,'9'
        JA skip
        SUB AL,'0'
        PUSH AX
        MOV AX,BX
        MUL CX
        MOV BX,AX
        POP AX
        MOV AH,00
        ADD BX,AX
        JMP back
     skip:
         MOV AX,BX
         POP CX
         POP BX
         RET
      readnumtoAX ENDP
displayAX PROC NEAR    ;Procedure def. to display  char in ax reg.
     PUSH DX
     PUSH CX
     PUSH BX
     PUSH AX
        MOV CX,0
        MOV BX,10
back1:        MOV DX,0
        DIV BX
        PUSH DX
        INC CX
        OR AX,AX
        JNZ back1
back2:
        POP DX
        ADD DL,30H
        MOV AH,02H
        INT 21H
        LOOP back2
        POP AX
        POP BX
        POP CX
        POP DX
        RET
displayAX ENDP

END

Output:

F:\hwlab>edit fact.asm
F:\hwlab>masm fact.asm
F:\hwlab>link fact.obj
F:\hwlab>fact

Enter the number:5
Factorial=120
F:\hwlab>fact

Enter the number:4
Factorial=24

Addition of n Numbers - Assembly Language - MASM

Program:
.model small
.stack 200H
.data
     msg1 DB 10,13,'Enter limit: $'
     msg2 DB 10,13,'Enter the numbers:  $'
     msg3 DB 10,13,'Sum = $'
     newline DB 10,13, ' $'
     sum DW 0

.code
    printstring MACRO msg     ;macro definition

                PUSH AX
                PUSH DX
                MOV AH,09H
                MOV DX,offset msg
                INT 21H
                POP DX
                POP AX
            ENDM

.startup

            printstring msg1          
            CALL readnumtoAX       ;enter the limit
            MOV CX,AX                    ;cx←limit
            printstring newline           
        printstring msg2
label1:      printstring newline
            CALL readnumtoAX       ;enter the no.s
               ADD sum,AX               ;sum=sum+ax
            DEC CX                          ;cx←cx-1
            JNZ label1               
            printstring newline
            printstring msg3            ;print sum
            MOV AX,sum
            CALL displayAX           
      
.exit




 readnumtoAX PROC NEAR
                                        ;procedure def. For reading-
            PUSH BX            ; char to ax register   
            PUSH CX
                MOV CX,10
            MOV BX,00
 back:     MOV AH,01H
                 INT 21H
                 CMP AL,'0'
                  JB skip
                 CMP AL,'9'
                 JA skip
                SUB AL,'0'
                PUSH AX
                MOV AX,BX
                MUL CX
                MOV BX,AX
                POP AX
                MOV AH,00
                ADD BX,AX
                JMP back
     skip:
                 MOV AX,BX
                 POP CX
                 POP BX
                 RET
     readnumtoAX ENDP


displayAX PROC NEAR
                           ;procedure def. For display
                           ;a character
            PUSH DX
            PUSH CX
            PUSH BX
            PUSH AX
            MOV CX,0
back1:      MOV BX,10
            MOV DX,0
            DIV BX
            PUSH DX
            INC CX
            OR AX,AX
            JNZ back1
back2:
            POP DX
            ADD DL,30H
            MOV AH,02H
            INT 21H
            LOOP back2
            POP AX
            POP BX
            POP CX
            POP DX
            RET
displayAX ENDP

END

Output:

Enter limit: 4

Enter the numbers:
 4
 5
 6
 7

Sum = 22

Addition of Two Numbers - Assembly Language - MASM


SUM OF TWO NUMBERS

Program:
.model small
.stack 200H
.data
   msg1 DB 10,13,'Enter the first no: $'
   msg2 DB 10,13,'Enter the second no: $'
   msg3 DB 10,13,'sum = $'
   newline DB 10,13, ' $'
.code
   print MACRO msg ;macro definition
   PUSH AX
   PUSH DX
   MOV AH,09H
   MOV DX,offset msg
   INT 21H
   POP DX
   POP AX
   ENDM
.startup
   print msg1
   CALL readnumtoAX
   MOV CX,AX
   print newline
   print msg2
   CALL readnumtoAX
   print newline
   print msg3
   ADD ax,cX
   CALL displayAX
.exit
readnumtoAX PROC NEAR
   PUSH BX
   PUSH CX
   MOV CX,10
   MOV BX,00
back:
   MOV AH,01H
   INT 21H
   CMP AL,'0'
   JB skip
   CMP AL,'9'
   JA skip
   SUB AL,'0'
   PUSH AX
   MOV AX,BX
   MUL CX
   MOV BX,AX
   POP AX
   MOV AH,00
   ADD BX,AX
   JMP back
skip:
   MOV AX,BX
   POP CX
   POP BX
   RET
readnumtoAX ENDP
displayAX PROC NEAR
   PUSH DX
   PUSH CX
   PUSH BX
   PUSH AX
   MOV CX,0
   MOV BX,10
back1:
   MOV DX,0
   DIV BX
   PUSH DX
   INC CX
   OR AX,AX
   JNZ back1
back2:
   POP DX
   ADD DL,30H
   MOV AH,02H
   INT 21H
   LOOP back2
   POP AX
   POP BX
   POP CX
   POP DX
   RET
  displayAX ENDP
end

Output:
F:\>adn1
Enter the first no: 27
Enter the second no: 32
sum = 59

A Simple Assemply Language Program - MASM

Program:
.model small
.stack 20h
.data
          msg db 'Hello World$'
.code
.startup
          mov ah,09h
          mov dx,offset msg
          int 21h
          mov ah,4ch
          mov bx,00h
          int 21h
end
Related Posts Plugin for WordPress, Blogger...