List 5 Assembly Language Program Example Solved Help You To Understand

Ashutosh Kumar
By -
0
Assembly language ASM 5 examples


 IGNOU BCA Reader, 

    In this post, I am going to list the 5 solved assembly program question with explanation. That will help your to understand the assembly language concept easily.
                            

1. Find the averages of two values stored in memory location named FIRST and SECOND and put the result in the memory location AVGE.



DATA SEGMENT
    FIRST    DB    90h
    SECOND    DB 78h
    AVGE    DB     ?
DATA ENDS

CODE SEGMENT
    ASSUME CS: CODE, DS: DATA
START:    MOVE AX, DATA
                 MOVE DS, AX
                 MOV AL, FIRST
                 ADD AL, SECOND
                 MOV AH, 00h
                 ADC AH, 00h
                 MOV BL, 02h
                 DIV BL
                 MOV AVGE, AL
CODE ENDS
                END START


EXPLANATION : -

ADD instruction can't add two memory location directly, so I have more first value to AL and then added the SECOND value to it. 

But there is possibility of carry bit, because in this example the value is treated as unsigned binary numbers. so, now i have have to put the carry bit into AH register such that  AX( AH, AL ) reflects the added value.
This is done by ADC instruction. ( ADC is `add carry` works same as ADD instruction by this use to add byte and byte  and generated carry is put in LSB bits of  AH  )

Divisor is loaded into BL register.

DIV BL instruction is use to find divide sum of two numbers that is already stored in AX register. And result are stored in AX register as follow: Quotient in AL and reminder in AH.

At the end, use MOV AVGE, AL to move the data from Accumulator (AL having Quotient ) to memory address AVGE



2. Write a Program for interchanging the values of two Memory locations ( tow memory variable of same size: 8 bit program)


DATA SEGMENT
    VALUE1     DB     0Ah
    VALUE2     DB     14H
DATA ENDS

CODE SEGMENT 
                ASSUME CS:CODE, DS: DATA
                MOV AX, DATA
                MOV DS, AX
                MOV AL, VLAUE1
                XCHG VLAUE2, AL
                MOV VALUE1, AL
                INT 21h
                CODE ENDS
END


EXPLANATIONS: 

In data segment, I have  create two variable with 8 bit data stored.

Initialize DATA Segment in the first MOV instruction of Code Segment. 
( Why we here initialize the data segment? why we shouldn't use MOV and XCHG directly to exchange the value? The reason is that MOV has some constraints to have followed. that MOV can't use two memory locations as operands and also Immediate data value and memory variable may not be moved to SEGMENT register. VALUE1 & VALUE2 is memory locations) 

Load VALUE1 into AL and XCHG AL with VALUE2

Store AL value to VALUE1 by MOV VALUE1, AL

INT 21h instruction used to return to Operating system.

3. Write a program using 8086 assembly language that adds two single digit ASCII digits stored in two consecutive memory locations. The result must be moved to DL register.


DATA SEGMENT
	NUM1 DB '2';
    NUM2 DB '3';
DATA ENDS

CODE SEGMENT
START:

ASSUME CS:CODE, DS:DATA
MOV AX,DATA
MOV DS,AX

MOV AL, NUM1
ADD AL, NUM2
MOV DL AL

MOV AX, 4C00H
INT 21H

CODE ENDS

END START
 
Explanation :

In this question, two ascii single digit  is stored in consecutive memory locations. We have get the result of sum and move it to DL register ( lower of DX).

MOV AX, DATA and MOV DS, AX are two instruction that is used most of case where we have to work with DATA SEGMENT.

MOV AL, NUM1 - move the memory data to AL lower of AX accumulator register.
ADD AL, NUM2 - Add instruction to add then second number to first number and result stored in AL.
MOV DL, AL - according to question, data need to be stored in DL

MOV AX, 4C00H - It is interruption. 
INT 21H - Return to DOS  
this two instruction is used in most of assembly program to stop the CPU execution. something..

4. Matching two strings of same length stored in memory locations, if the 8086 string instruction would have used. 

In this example these following registers has been used :  CS, DS, ES, AX, DX, CX, SI, DI

 
DATA SEGMENT
	PASSWORD 	DB		'FALSESAFE';
    DESTSRT		DB		'FEELSAFE' ;
    MESSAGE		DB 		'string are equal $'
    
DATA ENDS

CODE SEGMENT
	ASSUME CS:CODE, DS:DATA, ES:DATA
    	MOV AX, DATA
        MOV DS, AX
        MOV ES, AX
        
        LEA SI, PASSWORD
        LEA	DI, DESTSTR
        MOV CX, 08
        CLD
        
        PEPE CMPSB
        JNE   NOTEQUAL
        MOV AH, 09
        MOV DX, OFFSET MESSAGE
        INT 21h
        
NOTEQUAL : MOV AX, 4C00h
		   INT 21h
CODE ENDS
        

Explanation :

In data segment, we have defined PASSWORD as a source string and DESTSTR is a destination/target string by which we have to compare. MESSAGE string which will be output when both string are equal.
In code segment, All of first we have to initialize the other segment. Here we have initialize DATA SEGMENT register and EXTRA SEGMENT (ES) register. ( String is considered to be in extra segment)
LEA instruction is used to load source pointer both of String in respectively following SI(Source Index) to PASSWORD and DI (Destination Index) to DESTSTR.
MOV CX, 08 - Load the Counter Register with length of string.
CLD - Clear direction flag, so compare will be proceed in forward direction.

REPE CMPSB - This instruction tells CPU to compare the two strings byte by byte . REPE CMPSB - repeat till not equal. ( CMPSB - Compare String Byte )
JNE NOTEQUAL - If strings are not equal, then JNE instruction will be jump to given label (NOTEQUAL). JNE
MOV AH, 09 and MOV DX, OFFSET MESSAGE - display message
INT 21h - display message

NOTEQUAL Label MOV AX, 4C00h - interrupt function to halt, return to DOS
INT 21h - Interrupt accordingly whatever in AX register have value. 

 


5. Add two 8-bit number in the memory locations NUM1 and NUM2. the result is stored in the memory location RESULT. If there was a carry from addition it will be stored as 0000 0001 in the location CARRY. ( Used: CS, DX, AX )


DATA SEGMENT
	NUM1		DB		15h
    NUM2		DB 		20h
    RESULT		DB 		?
    CARRY		DB		?
DATA ENDS
CODE SEGMENT
	ASSUME CS:CODE, DS:DATA
 START: MOV AX, DATA
 		MOV DS, AX
        MOV AL, NUM1
        ADD AL, NUM2
        MOV RESULT, AL
        RCL AL, 01
        AND AL, 00000001B
        MOV CARRY, AL
        MOV AX, 4C00h
        
        INT 21h
CODE ENDS
	END START
    
EXPAINATION

Simply, Most of assembly programm we have to initialize segment if you use that segment in CODE SEGMENT.
RCL (Rotate Carry Left) AL, 01 - Rotate carry into LSB.
AND AL, 00000001B - Mask out all except LSB
MOV AX, 4C00h with INT 21h - is used to terminate DOS.


Conclusion :

In this post I have given 5 assembly program solved codes and I have try to explain it also. I hope you will like this content. Again I will be trying to update the post as I see any mistake.

Post a Comment

0Comments

✍️ Only article/post centric comments are allowed.
❌**No Spam**
❌**Advertisement**
❌**No abuse**
Rules strictly applied ✅

Post a Comment (0)