program to generate triangular wave in 8086

program to generate triangular wave in 8086


Program 1 


TRIANGULAR WAVE GENERATOR with 8086 using 8255

MODEL SMALL
.STACK 100
.DATA
CONTROL EQU 0FFC6H ; Control port address for 8255
PORTA EQU 0FFC0H ; Port A address for 8255
PORTB EQU 0FFC2H ; Port B address for 8255
PORTC EQU 0FFC4H ; Port C address for 8255

.CODE
START:
MOV DX,CONTROL
MOV AL,80H ; Initialize all ports as output
OUT DX,AL ; Ports

BEGIN:
MOV DX,PORTB
MOV AL,00H ; Output 00 for 0V level
UP: CALL OUTPUT
INC AL ; To raise wave from 0V to 5V increment AL
CMP AL,00H
JNZ UP ; Jump UP till rising edge is reached i.e. 5V

MOV AL,0FFH
UP1: CALL OUTPUT
DEC AL ; To fall wave from 5V to 0V decrement AL
CMP AL,0FFH
JNZ UP1 ; Jump UP till falling edge is reached i.e. 0V
JMP BEGIN

OUTPUT:
OUT DX,AL
CALL DELAY
INT 21H

DELAY:
MOV CX,07H ;To vary the frequency alter the delay count
LUP1:LOOP LUP1
INT 21H

END START

Program 2


DA00: MOV DX, PORTB
      MOV AL, 00H    ;outport 00 for 0V level
UP: CALL OUTPUT
    INC AL
    CMP AL, 00H
    JNZ UP
    MOV AL, 0FFH       ;to change amplitude change count
UP1: CALL OUTPUT
     DEC AL
     CMP AL, 0FFH
     JNZ UP1   
     JMP DA00
;routine to output digital value
OUTPUT:    OUT DX, AL
    CALL DELAY
        RET
DELAY: MOV CX, 07H    ;to vary the frequency alter the delay count
LUP1: LOOP LUP1
      RET 

8086 assembly language programs for string operations

8086 assembly language programs for string operations

1. Length of the string
Aim:
To write an assembly language program to find the length of the given string.
Tools:
PC installed with TASM.
Program:
ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AL, ’$’
MOV CX, 00H
MOV SI, OFFSET STR1
BACK : CMP AL, [SI]
JE GO
INC CL
INC SI
JMP BACK
GO : MOV LENGTH, CL
HLT
CODE ENDS
DATA SEGMENT
STR1 DB ‘STUDENT BOX OFFICE$’
LENGTH DB ?
DATA ENDS
END
Result:
Input:
STR (DS : 0000H) = STUDENT BOX OFFICE
Output:
LENGTH = 18

2. Display the string
Aim:

To write an assembly language program to display the given string.(DOS PROGRAMMING)

Software required:

TASM TURBO ASSEMBLER

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AH, 09H
MOV DX,OFFSET MSG
INT 21H
MOV AH, 4CH
INT 21H
CODE ENDS
DATA SEGMENT
MSG DB 0DH, 0AH, "WELCOME TO MICROPROCESSORS LAB", 0DH, 0AH, "$"
DATA ENDS
END
Result:

WELCOME TO MICROPROCESSORS LAB

3. Reverse the string
Aim:

To write an assembly language program to reverse the given string.

Sofware required:

TASM TURBO ASSEMBLER

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV CL, COUNT
MOV SI, OFFSET STR1
MOV DI, COUNT - 1
BACK:MOV AL, [SI]
XCHG [DI], AL
MOV [SI], AL
INC SI
DEC DI
DEC CL
JNZ BACK
HLT
CODE ENDS
DATA SEGMENT
STR1 DB ‘MPMC$’
COUNT EQU 04H
STR2 DB DUP (0)
DATA ENDS
END
Result:
Input:
STR1 (DS:0000H)  =  
Output:
STR1 (DS:0004H) =  


(a) Concatenation of two strings (b) Number of occurrences of a sub-string in the given string.

Use PUBLIC and EXTERN directive. Create .OBJ files of both the modules and link them to create an .EXE file.

  • 16 Bit TASM Code
program_1.asm
.model small
.data
        menu db 10d,13d,"        MENU"
             db 10d,"1. Concatenation"
             db 10d,"2. Substring"
             db 10d,"3. Exit"
             db 10d,"Enter your choice: $"
            
.code
;concat and substring procedures will be defined in external files
extrn concat:far               
extrn substring:far

        mov ax,@data
        mov ds,ax

main:
        lea dx,menu
        mov ah,09h      ;display menu string
        int 21h

        mov ah,01h      ;single char input
        int 21h

        cmp al,'1'
        je case1
        cmp al,'2'
        je case2
        jmp exit

case1:  call concat     ;call procedure
        jmp main
case2:  call substring  ;call procedure
        jmp main
exit:
        mov ah,4Ch
        int 21h
end                     ;end of program


program_2.asm
.model small
.data
        m1 db 10d,13d,"Enter first string:  $"
        m2 db 10d,13d,"Enter second string: $"
        m3 db 10d,13d,"Concatenated string: $"
        s1 db 20 dup('$')
        s2 db 20 dup('$')
        s3 db 40 dup('$')
        nwline db 10d,"$"
        m4 db 10d,13d,"Second String is Substring.$"
        m5 db 10d,13d,"Second String is not a Substring.$"
        m6 db 10d,13d,"No. Of Occurrence: $"
        count db 1 dup(0)

.code
        public concat,substring
;procedures scope has been made public
;accessible to other segments also

;*********Generalised 2 parameters 16 bit macro************
scall macro xx,yy
        lea dx,xx
        mov ah,yy
        int 21h
endm                    ;end of macro

;*******************CONCATENATION PROCEDURE*****************
concat proc
        mov ax,@data
        mov ds,ax
       
        scall m1,09h    ;display m1 string
        scall s1,0Ah    ;buffered s1 string input

        scall m2,09h    ;display m2 string
        scall s2,0Ah    ;buffered s2 string input

        lea si,s1
        lea di,s3
        inc si
        mov cl,[si]     ;length of s1 string

;copying entire s1 string to s3
loop1:  inc si
        mov al,[si]
        mov [di],al
        inc di
        dec cl
        jnz loop1

        lea si,s2
        inc si
        mov cl,[si]     ;length of s2 string

;copying entire s2 string to s3
loop2:  inc si
        mov al,[si]
        mov [di],al
        inc di
        dec cl
        jnz loop2

        mov al,24h      ;'$'=24h, putting $ at end of string s3
        mov [di],al

        scall nwline,09h
        scall m3,09h    ;display m3 string
        scall s3,09h    ;display concatenated string

        ret             ;return from prodecure
endp                    ;end of prodecure

;****************SUBSTRING PROCEDURE**********************
substring proc
        mov ax,@data
        mov ds,ax

        scall m1,09h    ;display m1 string
        scall s1,0Ah    ;accept s1 string
        scall m2,09h    ;display m2 string
        scall s2,0Ah    ;accept s2 string

        lea si,s1
        inc si
        mov cl,[si]     ;take length of s1 string in cl
        inc si
       
        lea di,s2
        inc di
        mov ch,[di]     ;take length of s2 string in cl
        inc di
        mov dh,ch       ;backup of ch register
       
        mov [count],0   ;initialise count with zero
       
loop3:   
        mov al,[si]
        mov bp,si       ;backup of si pointer
        cmp al,[di]
        je loop4
        inc si
       
        dec cl          ;counter for main string
        jnz loop3

        mov dl,[count]
        cmp dl,0        ;dl=0 implies no string found
        je fail
        jmp result

loop4:  
        dec ch          ;counter for sub-string
        cmp ch,0
        je success
        inc si
        inc di
        mov al,[si]
        cmp al,[di]
        je loop4        ;continue this loop till string are same
       
        jmp loop3       ;inc case of mismatch, start again

success:
        add [count],01
        lea di,s2
        add di,2        ;move di to string place
        inc bp
        mov si,bp       ;restore si from bp
        dec cl
        mov ch,dh       ;restore ch from dh
        
        jmp loop3       ;start again till main string ends
       
fail:
        scall m5,09h    ;display m5
        ret             ;return from procedure

result:
        scall m4,09h    ;display m4 string
        scall m6,09h    ;display m6 string
      
        mov dl,[count] 
        add dl,30h             
        mov ah,02h      ;display dl contents
        int 21h        
        ret             ;return from procedure
endp                    ;end of procedure
end                     ;end of Program

prime number in assembly language 8086

prime number in assembly language 8086

Example Code 1 for prime number in assembly language 8086

.MODEL SMALL
.STACK 100H

.DATA

  NUM DB ?
  MSG1 DB 10,13,'ENTER NO: $'
  MSG2 DB 10,13,'NOT PRIME: $'
  MSG3 DB 10,13,'PRIME $'

.CODE
MAIN PROC

  MOV AX,@DATA
  MOV DS,AX

  LEA DX,MSG1
  MOV AH,9
  INT 21H

  MOV AH,1
  INT 21H
  SUB AL,30H
  MOV NUM,AL

  CMP AL,1
  JLE LBL2
  MOV AH,00
  CMP AL,3
  JLE LBL3
  MOV AH,00

  MOV CL,2
  DIV CL
  MOV CL,AL
 
LBL1:

  MOV AH,00
  MOV AL,NUM
  DIV CL
  CMP AH,00
  JZ LBL2
  DEC CL
  CMP CL,1
  JNE LBL1
  JMP LBL3

LBL2:

  MOV AH,9
  LEA DX,MSG2
  INT 21H
  JMP EXIT

LBL3:

  MOV AH,9
  LEA DX,MSG3
  INT 21H

EXIT:

  MOV AH,4CH
  INT 21H

 
MAIN ENDP
END MAIN 

Example Code 2 for prime number in assembly language 8086

Logic of Prime Number Check in Assembly Code
  • Before going to the assembly code let me explain the programming logic first.
  • Suppose the given number is ‘x’. Let us divide the number x with (x-1). I.e. x/(x-1).
  • Now check the reminder status. Reminder is store in DX register b default.
  • Now divide x with x-2, i.e. x/(x-2) then check the reminder.
  • Repeat this step up to the denominator become 2.
  • Denominator is like (x-1), (x-2),(x-3),….etc. 2
  • When the reminder=0, the number is ‘Not Prime’, else it is ‘Prime’
Prime Number Program in 8086
Now let’s see the 8086 assembly language code for prime number checking. [cc lang=”C”]1000 XOR CX,CX /*Clearing the registers*/
1002 XOR DX,DX /*Clearing the registers*/
1004 MOV AX,[1500] /*Assume the number is store in memory 1500*/
1006 MOV BX,AX /*Copy the value in AX to BX */
1008 DEC BX
1010 DIV BX /*Dividing*/
1012 CMP DX,0000 /*Check for Reminder, Reminder is stored in DX*/
1014 JZ step 13
1016 XOR DX,DX /*Clearing DX*/
1018 CMP BX,0002
1020 JNZ 1008
1022 MOV CX,0001
1024 MOV [2000],CX /*Storing the result*/
1026 HLT[/cc]


Example Code 3 for prime number in assembly language 8086

Data Segment
    arrprime db 20 dup (?)
    i db 02h
Data Ends

Code Segment
    Assume cs:code, ds:data
Begin:  
    mov ax, data
    mov ds, ax
    mov es, ax

    mov dl, 01h
    mov cx, 10h
    lea di, arrprime

    L1:
       mov bl, 02
       add dl, 01h

       cmp dl, 02h
       je Insert
       cmp dl, 03h
       je Insert
       cmp dl, 04h
       jge Logic

    Logic:
          mov ah, 00
          mov al, dl
          div bl
          cmp ah, 00
      je L1
          add bl, 01h
          cmp bl, al 
          jle Logic
      jmp insert

    Insert:
       mov [di], dl
       inc di
       loop L1

    Exit:
       mov ax, 4c00h
       int 21h
Code Ends
End Begin

Data Segment
    arrprime db 20 dup (?)
    i db 02h
Data Ends

Code Segment
    Assume cs:code, ds:data
Begin:  
    mov ax, data
    mov ds, ax
    mov es, ax

    mov dl, 01h
    mov cx, 10h
    lea di, arrprime

    L1:
       mov bl, 02
       add dl, 01h

       cmp dl, 02h
       je Insert
       cmp dl, 03h
       je Insert
       cmp dl, 04h
       jge Logic

    Logic:
          mov ah, 00
          mov al, dl
          div bl
          cmp ah, 00
      je L1
          add bl, 01h
          cmp bl, al 
          jle Logic
      jmp insert

    Insert:
       mov [di], dl
       inc di
       loop L1

    Exit:
       mov ax, 4c00h
       int 21h
Code Ends
End Begin