Showing posts with label Assembly Language. Show all posts
Showing posts with label Assembly Language. Show all posts

Monday, 27 August 2018

Assuming that there is a spelling of “Europe” in an electronic dictionary and a user types in “Euorope”, write a program that compares these two and displays the following message, depending on a result: 1. If they are equal, display “The spelling is correct” 2. If they are not equal, display “Wrong Spelling”

.MODEL SMALL
.STACK 100H
.DATA
;from the data segment
DAT_DIC DB 'Europe'
DAT_TYPED DB 'Euorope'
MESSAGE1 DB 'The spelling is correct$'
MESSAGE2 DB 'Wrong spelling$'

;from code segment:
.CODE
MAIN PROC
   
    MOV AX, @DATA
    MOV DS, AX
   
    CLD                         ;DF=0 for increment
    MOV SI,OFFSET DAT_DIC       ;SI=offset of DAT_DIC
    MOV DI,OFFSET DAT_TYPED     ;DI=offset of DAT_TYPED
    MOV CX,06                   ;load the counter
    REPE CMPSB                  ;repeat as long as equal or until CX=0
    JE OVER                     ;if ZF=1 then display MESSAGE1
   
    MOV DX,OFFSET MESSAGE2      ;if ZF=0 then display MESSAGE2
    JMP DISPLAY
   
OVER:
    MOV DX,OFFSET MESSAGE1

DISPLAY:
    MOV AH,09
    INT 21H

    MOV AH, 4CH
    INT 21H
    MAIN ENDP
END MAIN

Tuesday, 31 July 2018

Write a program in Assembly Language that finds the number of 1s in a byte.

.MODEL SMALL
.STACK 100H
.DATA
    DATA1 DB 21H
    COUNT DB ?

.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
   
    SUB BL,BL ;clear BL to keep number of 1s
    MOV DL,8 ;rotate total of 8 times
    MOV AL,DATA1
   
    AGAIN:
    ROL AL,1 ;rotate it once
    JNC NEXT ;check for 1
    INC BL ;if CF =1 than increment count
   
    NEXT:
    DEC DL ;go through this 8 times
    JNZ AGAIN ;if not finished go back
    MOV COUNT,BL ;save the number of ones
   
    MOV AH, 2
    ADD BL, 30H
    MOV DL, BL
    INT 21H 
   
    MOV AH, 4CH
    INT 21H
    MAIN ENDP
END MAIN

Saturday, 28 July 2018

How to add two decimal numbers using AAA instruction in assembly language?

.MODEL SMALL
.STACK  64H
.DATA
        V1      DB      5 
        V2      DB      4
.CODE
MAIN PROC
   
        MOV     AX,@DATA
        MOV     DS,AX
   
        MOV AL,V1
        ADD AL,V2
        AAA       
       
        MOV BL,AL
        OR BL, 30H
   
        MOV AH,02H
        MOV DL, BL
        INT 21H
     
   
        MOV     AH,4CH
        INT     21H
        MAIN     ENDP
END MAIN

How to add two hexadecimal numbers in Assembly Language?

.MODEL SMALL
.STACK  64H
.DATA
        V1      DB      29H  
        V2      DB      18H
.CODE
MAIN PROC
   
        MOV     AX,@DATA
        MOV     DS,AX
   
        MOV AL,V1
        ADD AL,V2
        DAA         ;Decimal Adjust for Addition

        MOV BL,AL
   
        MOV AH,02H
        MOV DL, BL
        INT 21H
      
   
        MOV     AH,4CH
        INT     21H
        MAIN     ENDP
END MAIN

Saturday, 7 April 2018

Write a program in assembly language to reverse a string.

; Write a program to reverse a string.

.MODEL SMALL
.STACK 100H
.DATA     
    INPUT_MSG DB 'ENTER A STRING: $'
    OUTPUT_MSG DB 0AH,0DH,'OUTPUT IS: $'
.CODE

MAIN PROC

    MOV AX,@DATA
    MOV DS,AX
   
    MOV AH, 9
    LEA DX, INPUT_MSG
    INT 21H
   
    MOV AH,1
   
    MOV CX, 0  ;CX is 0
    TOP:
       
        INT 21H
   
        CMP AL,0DH   ; the comparison with carriage return done to find the end of a string
        JE END_INPUT
   
        PUSH AX
        INC CX
        JMP TOP
   
    END_INPUT:

    MOV AH,9
    LEA DX,OUTPUT_MSG
    INT 21H
   
    MOV AH, 2

    OUTPUT:
        POP DX
        INT 21H
        LOOP OUTPUT
   
    EXIT:
    MOV AH,4CH
    INT 21H
    MAIN ENDP
END MAIN

Sunday, 1 April 2018

Write a program to display a 10 x 10 solid box of asterisks. Hint: declare a string in the data segment that specifies the box, and display it using loop and level.

.MODEL SMALL
.STACK 100H 

.DATA
    SOLIDBOX  DB  '**********',0DH,0AH,'$'
 
.CODE
MAIN PROC                              ;PROC = Procedure

    MOV AX, @DATA                ; initialize DS
    MOV DS, AX 
   
    MOV CX, 10
   
    LOOP:
    MOV AH, 9
    LEA DX, SOLIDBOX
   
    TOP:
    INT 21H
    LOOP TOP
 
    EXIT:
    MOV AH, 4CH
    INT 21H
    MAIN ENDP
END MAIN

Write a program to display a 10 x 10 solid box of asterisks. Hint: declare a string in the data segment that specifies the box, and display it with INT 2lh, function 9h.

.MODEL SMALL
.STACK 100H
.DATA
    SOLIDBOX  DB  '**********',0DH,0AH,'$'

.CODE
MAIN PROC
     MOV AX, @DATA                ; initialize DS
     MOV DS, AX

     LEA DX, SOLIDBOX               ; load the string
     MOV AH, 9h                                         

     INT 21H
     INT 21H                      ; display the string 10 times
     INT 21H
     INT 21H
     INT 21H
     INT 21H
     INT 21H
     INT 21H
     INT 21H
     INT 21H

     MOV AH, 4CH                  ; return control to DOS
     INT 21H
   MAIN ENDP
 END MAIN

Saturday, 31 March 2018

Write a program to compare two values in assembly language.

; Level and Conditional Statement

.MODEL SMALL
.STACK 100H 
.DATA 
MSG1 DB 'ENTER AN INPUT: $'
MSG2 DB 'GREATER $'
MSG3 DB 'SMALLER $'
MSG4 DB 'EQUAL $'   
NEWLINE DB 0DH, 0AH, '$'

.CODE
MAIN PROC
    MOV AX,@DATA
    MOV DS,AX 
   
    LEA DX,MSG1
    MOV AH,9
    INT 21H
   
    MOV AH,1
    INT 21H
    MOV BL,AL
    SUB BL,30H 
   
    LEA DX,NEWLINE
    MOV AH,9
    INT 21H 
   
    CMP BL,5
    JE L2
    JL L1    ;JL = Jump if less than
   
    LEA DX,MSG2
    MOV AH,9
    INT 21H
   
    JMP EXIT ; JMP = Jump
   
    L1: 
    LEA DX,MSG3
    MOV AH,9
    INT 21H   
   
    JMP EXIT ; JMP = Jump
                             
    L2: 
    LEA DX,MSG4
    MOV AH,9
    INT 21H 
   
    EXIT:
    MOV AH,4CH
    INT 21H
    MAIN ENDP
END MAIN

Write a program in assembly language for converting lower case to upper case in a given character.

.MODEL SMALL
.STACK 100H

.DATA
    MSG_1  DB  'Enter a lower case letter: $'
    MSG_2  DB  0DH,0AH,'In uppercase it is: $'
    CHAR DB ?

 .CODE
   MAIN PROC
     MOV AX, @DATA                ; initialize DS
     MOV DS, AX
   
     MOV AH, 9
     LEA DX, MSG_1             ; load and display MSG_1
     INT 21H

     MOV AH, 1                    ; read the lowercase letter
     INT 21H
     MOV CHAR, AL            ; save the lowercase letter into AL
     SUB CHAR, 20H            ;convert the character into uppercase letter by subtracting 20H
   
     MOV AH, 9
     LEA DX, MSG_2                 ; load and display MSG_2
     INT 21H
   
     MOV AH, 2
     MOV DL, CHAR                   ; display the output
     INT 21H

     MOV AH, 4CH                  ; return control to DOS
     INT 21H
   MAIN ENDP
 END MAIN

Sunday, 25 March 2018

How to print A to Z using for loop in Assembly Language?

.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC                ; Print A to Z

    MOV CX,26
 
    LOOP:
 
    MOV AH,2
    MOV DL,'A'
 

    TOP:
    INT 21H
    INC DL
    LOOP TOP
   
    EXIT:
   
    MOV AH,4CH
    INT 21H
    MAIN ENDP
END MAIN

Saturday, 24 March 2018

How to print 1 to 9 using for loop in Assembly Language?

.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC    ; output: 123456789
 
 
    MOV CX, 9
 
    LOOP:
    MOV AH,2
    MOV DL, 1
    ADD DL, 30H
 

    TOP:
    INT 21H
    INC DL
    LOOP TOP
   
    EXIT:
   
    MOV AH,4CH
    INT 21H
    MAIN ENDP
END MAIN

Saturday, 3 March 2018

Read two decimal digits whose sum is less or big than 10, display them and their sum on the next line with an appropriate message in Assembly Language.

;Write a program to 
;(a) read two decimal digits whose sum is less or big than 10, 
;(b) display them and their sum on the next line with an appropriate message.


.MODEL SMALL
.STACK 100H   
.DATA
    a DB 0
    b DB 0
    MSG1 DB 'Enter a: $'
    MSG2 DB 0AH, 0DH, 'Enter b: $'                     ; variable declaration
    MSG3 DB 0AH, 0DH, 'Sum of a and b is: $'
.CODE
MAIN PROC  ;PROC = Procedure
    MOV AX, @DATA    ; initialize data segment
    MOV DS, AX
   
    LEA DX, MSG1    ; LEA = Load Effective Address      Compute address of value
    MOV AH, 9       ; Output a String
    INT 21H
   
   
    MOV AH, 1   ; INPUT FUNCTION 1
    INT 21H     ; INT = Interupt
    MOV a, AL
   
    ;MOV BH, AL 
   
    LEA DX, MSG2    ; LEA = Load Effective Address      Compute address of value
    MOV AH, 9       ; Output a String
    INT 21H
   
   
    MOV AH, 1   ; INPUT FUNCTION 2
    INT 21H     ; INT = Interupt
    MOV b, AL
   
    ADD AL, a
    MOV AH,0    ; AX = AH, AL
    AAA         ; AAA = Adjust After Addition (first digit is saved in AH register and second digit is saved in AL register)   
   
    ADD AH,30H
    ADD AL,30H
   
    MOV BX,AX    ; Since the AH register is used again and again. We cannot lose the result in AH and AL register, So to save the to be printed value in BH and BL register. By moving AX to BX.
   
    LEA DX,MSG3
    MOV AH,9
    INT 21H     
   
    MOV AH,2
    MOV DL,BH
    INT 21H
   
    MOV AH,2
    MOV DL,BL
    INT 21H
   
   
   EXIT:
   MOV AH, 4CH
   INT 21H
   MAIN ENDP
END MAIN

Friday, 2 March 2018

How to add two numbers in Assembly Language?

; Addition of two numbers

.MODEL SMALL
.STACK 100H   
.DATA
    MSG1 DB 'Enter a: $'
    MSG2 DB 0AH, 0DH, 'Enter b: $'                     ; variable declaration
    MSG3 DB 0AH, 0DH, 'Sum of a and b is: $'
    RESULT DB ?
.CODE
MAIN PROC  ;PROC = Procedure
    MOV AX, @DATA    ; initialize data segment
    MOV DS, AX
   
    LEA DX, MSG1    ; LEA = Load Effective Address      Compute address of value
    MOV AH, 9       ; Output a String
    INT 21H
   
   
    MOV AH, 1   ; INPUT FUNCTION
    INT 21H     ; INT = Interupt
    MOV BL, AL
   
    MOV BH, AL 
   
    LEA DX, MSG2    ; LEA = Load Effective Address      Compute address of value
    MOV AH, 9       ; Output a String
    INT 21H
   
   
    MOV AH, 1   ; INPUT FUNCTION
    INT 21H     ; INT = Interupt
    ADD AL, BL
   
    MOV RESULT, AL
    SUB RESULT, 30H
   
    LEA DX, MSG3    ; LEA = Load Effective Address      Compute address of value
    MOV AH, 9       ; Output a String
    INT 21H
   
    MOV AH, 2   ; OUTPUT FUNCTION
    MOV DL, RESULT 
    INT 21H 
   
   
   EXIT:
   MOV AH, 4CH
   INT 21H
   MAIN ENDP
END MAIN

How to take multiple input in Assembly Language?

; Multiple Input

.MODEL SMALL
.STACK 100H
.CODE

MAIN PROC  ;PROC = Procedure
   
    MOV AH, 1   ; INPUT FUNCTION
    INT 21H     ; INT = Interupt
    MOV BL, AL
    INT 21H
   
    MOV BH, AL     
   
    MOV AH, 2
    MOV DL, 0AH 
    INT 21H        ; NEWLINE
    MOV DL, 0DH
    INT 21H
   
   
    MOV AH, 2   ; OUTPUT FUNCTION
    MOV DL, BL 
    INT 21H 
   
    MOV DL, BH
    INT 21H
   
   EXIT:
   MOV AH, 4CH
   INT 21H
   MAIN ENDP
END MAIN

How to take input from user in Assembly Language?

.MODEL SMALL       ; Define the memory model
.STACK 100H             ; Reserving 256 bytes for this program
.CODE                         ; Code Segment

MAIN PROC               ;PROC = Procedure
   
    MOV AH, 1             ; INPUT FUNCTION
    INT 21H                   ; INT = Interupt
    MOV BL, AL     
   
    MOV AH, 2
    MOV DL, 0AH 
    INT 21H                   ; NEWLINE
    MOV DL, 0DH
    INT 21H
   
   
    MOV AH, 2              ; OUTPUT FUNCTION
    MOV DL, BL 
    INT 21H 
   
   EXIT:
   MOV AH, 4CH
   INT 21H
   MAIN ENDP
END MAIN

How to print a String using Assembly Language?

.MODEL SMALL       ; Define the memory model
.STACK 100H             ; Reserving 256 bytes for this program
.DATA                          ; Data Segment     for variable declaration

    MSG1 DB 'CSE 3014.5 $'       ; DB = Define Byte
    MSG2 DB 'Microprocessor and Assembly Language Lab $'
   
.CODE                          ; Code Segment

MAIN PROC   
   
    MOV AX, @DATA   ; INITIALIZATION of DATA Segment
    MOV DS, AX
   
    LEA DX, MSG1         ; LEA = Load Effective Address      Compute address of value
    MOV AH, 9                ; Output a String
    INT 21H
   
    MOV AH, 2                 ; Output a character
    MOV DL, 0DH
    INT 21H                       ; NEWLINE
    MOV DL, 0AH
    INT 21H 
   
    LEA DX, MSG2           ; LEA = Load Effective Address
    MOV AH, 9                  ; Output a String
    INT 21H
   
   EXIT:
   MOV AH, 4CH              ; Exit
   INT 21H
   MAIN ENDP
END MAIN
   
   
   
   
   

Wednesday, 24 June 2015

Write a program to read one of the hex digits A-F, and display it on the next line in decimal.

; Write a program to read one of the hex digits A-F, and display it on the next line in decimal.

 .MODEL SMALL
 .STACK 100H

 .DATA
    MSG_1  DB  'Enter the Hex digit (A-F) : $'
    MSG_2  DB  0DH,0AH,'The corresponding Decimal dgit is : $'
    HEX DB ?

 .CODE
   MAIN PROC
     MOV AX, @DATA                ; initialize DS
     MOV DS, AX
   
     MOV AH, 9
     LEA DX, MSG_1             ; load and display MSG_1
     INT 21H

     MOV AH, 1                   
     INT 21H
     MOV HEX, AL                  ; read the hex digit
     SUB HEX, 11H                  ; convert the character into second digit by subtracting 11H
   
     MOV AH, 9
     LEA DX, MSG_2             ; load and display MSG_2
     INT 21H
   
     MOV AH, 2
     MOV DL, 31H        ; display the first decimal digit i.e. 1
     INT 21H
                       
     MOV DL, HEX
     INT 21H

     MOV AH, 4CH                  ; return control to DOS
     INT 21H
   MAIN ENDP
 END MAIN

An AL Program that ; (a) prompt the user ; (b) read first, middle, and last initials of a person's name ; (c) display them down the left margin


 .MODEL SMALL
 .STACK 100H

 .DATA
    MSG_1  EQU  'Enter three initials : $'
    MSG_2  EQU  0DH,0AH,'The initials are :',0DH,0AH,'$'
    MSG_3  EQU  0DH,0AH,'$'

    PROMPT_1  DB  MSG_1
    PROMPT_2  DB  MSG_2
    NEWLINE   DB  MSG_3

 .CODE
   MAIN PROC
     MOV AX, @DATA                ; initialize DS
     MOV DS, AX

     LEA DX, PROMPT_1             ; load and display PROMPT_1
     MOV AH, 9
     INT 21H

     MOV AH, 1                    ; read first initial
     INT 21H

     MOV BL, AL                   ; save the first initial into BL

     MOV AH, 1                    ; read second initial
     INT 21H

     MOV BH, AL                   ; save the second initial into BH

     MOV AH, 1                    ; read third initial
     INT 21H

     MOV CL, AL                   ; save the third initial into CL

     LEA DX, PROMPT_2             ; load and display PROMPT_2
     MOV AH, 9
     INT 21H

     MOV AH, 2                    ; display the first initial
     MOV DL, BL
     INT 21H

     LEA DX, NEWLINE              ; display new line
     MOV AH, 9
     INT 21H

     MOV AH, 2                    ; display the second initial
     MOV DL, BH
     INT 21H

     LEA DX, NEWLINE              ; display new line
     MOV AH, 9
     INT 21H

     MOV AH, 2                    ; display the third initial
     MOV DL, CL
     INT 21H

     MOV AH, 4CH                  ; return control to DOS
     INT 21H
   MAIN ENDP
 END MAIN



Code for Program to read two digits whose sum is less than 0, computes and display their sum (using variables) in Assembly Language



 .MODEL SMALL
 .STACK 100H

 .DATA
    PROMPT_1  DB  'Enter the First digit : $'
    PROMPT_2  DB  'Enter the Second digit : $'
    PROMPT_3  DB  'Sum of First and Second digit : $'

    VALUE_1   DB  ?
    VALUE_2   DB  ?

 .CODE
   MAIN PROC
     MOV AX, @DATA                ; initialize DS
     MOV DS, AX

     LEA DX, PROMPT_1             ; load and display the PROMPT_1
     MOV AH, 9
     INT 21H

     MOV AH, 1                    ; read a character
     INT 21H

     SUB AL, 30H                  ; save First digit in VALUE_1 in ASCII code
     MOV VALUE_1,AL

     MOV AH, 2                    ; carriage return
     MOV DL, 0DH
     INT 21H

     MOV DL, 0AH                  ; line feed
     INT 21H

     LEA DX, PROMPT_2             ; load and display the PROMPT_2
     MOV AH, 9
     INT 21H

     MOV AH, 1                    ; read a character
     INT 21H

     SUB AL, 30H                  ; save Second digit in VALUE_2 in ASCII code
     MOV VALUE_2,AL

     MOV AH, 2                    ; carriage return
     MOV DL, 0DH
     INT 21H

     MOV DL, 0AH                  ; line feed
     INT 21H

     LEA DX, PROMPT_3             ; load and display the PROMPT_3
     MOV AH, 9
     INT 21H

     MOV AL, VALUE_1              ; add First and Second digit
     ADD AL, VALUE_2

     ADD AL, 30H                  ; convert ASCII to DECIMAL code

     MOV AH, 2                    ; display the character
     MOV DL, AL
     INT 21H

     MOV AH, 4CH                  ; return control to DOS
     INT 21H
   MAIN ENDP
 END MAIN