Anda di halaman 1dari 5

1/19/2017 Assembly program to conduct a binary search on sorted array | Computer Science Simplified ­ A Website for IGNOU MCA & BCA Students for Solved A…

Ads by Google Assembly Program Assembly Code Assembly Language

Rs.105 Rs.96 Rs.269

Rs.119 Rs.134 Rs.399

An Assembly program to conduct a binary search on a given sorted array of 16-


bit, unsigned integers, and a given 16-bit unsigned key
By Gangadhar Kopella | April 25, 2014 0 Comment

Now we will write another Assembly program to conduct a binary search on a given sorted array of 16-bit, unsigned integers, and a given 16-bit unsigned key.

The above Logic is a C like Program to conduct a binary search we need small Algorithm Shown above in a very simple way, So Just we will covert the logic into Assembly
There are many things uncommon in the programing Language. There are No While Loops or Modules but this things are to be implemented in di틣erent ways.

Let’s identify variables needed for this program.


First variables will be the one which will hold the values present in the Given Numbers in Array list and key of 16-bit and it will be array ARR and KEY. variables will be
holding the Messages MSG1 “KEY IS FOUND AT “, RES “  POSITION”, 13, 10,” $” and MSG2 ‘KEY NOT FOUND!!!.$’ to be printed for the User. Other variables will be
holding Length of the Array and it will be LEN, So in all Six variables.
The identi䇛�ed variables are ARR, KEY, LEN, RES, MSG1 and MSG2.

First Line – DATA SEGMENT

DATA SEGMENT is the starting point of the Data Segment in a Program and DATA is the name given to this segment and SEGMENT is the keyword for de䇛�ning Segments,
Where we can declare our variables.

Next Line – ARR DW 0000H,1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H,9999H


     LEN DW ($-ARR)/2
     KEY EQU 7777H
     MSG1 DB “KEY IS FOUND AT ”

http://cssimplified.com/computer­organisation­and­assembly­language­programming/an­assembly­program­to­conduct­a­binary­search­on­a­given­sorted­arr… 1/5
1/19/2017 Assembly program to conduct a binary search on sorted array | Computer Science Simplified ­ A Website for IGNOU MCA & BCA Students for Solved A…
     RES DB “  POSITION”,13,10,” $”
     MSG2 DB ‘KEY NOT FOUND!!!.$’

ARR DW 0000H,1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H,9999H this line is a declaration of 16-bit Numbers Array initialized


with 0000H,1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H,9999H the numbers are seperated by Comma (,). LEN DW $-ARR is used to Save the Length of the
Array which will be generated by $-Name of the array i.e. $-ARR. KEY EQU 7777H is used to save given KEY to be Searched in the Array and is equal to (EQU) 7777H.
MSG1 DB “KEY IS FOUND AT ” this line is a declaration of Charater Array initialized with “KEY IS FOUND AT”. (A Character is of a BYTE Hence we have to use only
DB De䇛�ne Byte ) and Similarly to RES and MSG2.  Detailed explanation is given below.

Next Line – DATA ENDS

DATA ENDS is the End point of the Data Segment in a Program. We can write just ENDS But to di틣erentiate the end of which segment it is of which we have to write the
same name given to the Data Segment.

Now, Selection of data type is DW data type the numbers which we are adding is 16-bit  integers so DW is su�cient.

Source code      

DATA SEGMENT 
     ARR DW 0000H,1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H,9999H 
     LEN DW ($‐ARR)/2 
     KEY EQU 7777H 
     MSG1 DB "KEY IS FOUND AT " 
     RES DB "  POSITION",13,10," $" 
     MSG2 DB 'KEY NOT FOUND!!!.$' 
DATA ENDS

 In Assembly programming, the variable are all de䇛�ned by bytes only.

DB – De䇛�ne Byte  (Size – 1 Byte)

DW – De䇛�ne Word  (Size – 2 Byte)

DD – De䇛�ne Double word  (Size -  4 Bytes)

DQ – De䇛�ne Quad word  (Size – 8 Bytes)

DT – De䇛�ne Ten Bytes  (Size – 10 Bytes)

NUMBER SYSTEM in Assembly Programming is Decimal, Octal, Hexadecimal, Binary.

In the Program, We are entering the values for the variables and Do arithmetical Operations like Addition, Subtraction, Multiplication and Division So the Computer
should understand which kind of Number is entered. Hence there is a di틣erent letters for di틣erent Number Systems. O or o stands for Octal, H or h stands for
Hexadecimal, B or b stands for Binary, D or d stands for Decimal. By default type of numbering system is Decimal. If you do not specify any letter then the number is
understood to be Decimal (By default).

Source code      

DATA SEGMENT 
     ARR DW 0000H,1111H,2222H,3333H,4444H,5555H,6666H,7777H,8888H,9999H 
     LEN DW ($‐ARR)/2 
     KEY EQU 7777H 
     MSG1 DB "KEY IS FOUND AT " 
     RES DB "  POSITION",13,10," $" 
     MSG2 DB 'KEY NOT FOUND!!!.$' 
DATA ENDS 
CODE SEGMENT  
    ASSUME DS:DATA CS:CODE 
START: 
      MOV AX,DATA 
      MOV DS,AX 
     
      MOV BX,00 

http://cssimplified.com/computer­organisation­and­assembly­language­programming/an­assembly­program­to­conduct­a­binary­search­on­a­given­sorted­arr… 2/5
1/19/2017 Assembly program to conduct a binary search on sorted array | Computer Science Simplified ­ A Website for IGNOU MCA & BCA Students for Solved A…
      MOV DX,LEN 
      MOV CX,KEY 
AGAIN: CMP BX,DX 
       JA FAIL 
       MOV AX,BX 
       ADD AX,DX 
       SHR AX,1 
       MOV SI,AX 
       ADD SI,SI 
       CMP CX,ARR[SI] 
       JAE BIG 
       DEC AX 
       MOV DX,AX 
Explanation : 
       JMP AGAIN 
BIG:   JE SUCCESS 
In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra 
       INC AX 
Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will
       MOV BX,AX 
need       JMP AGAIN 
two Segments i.e. Code Segment and Data Segment.
SUCCESS: ADD AL,01 
         ADD AL,'0' 
Next Line – CODE SEGMENT
         MOV RES,AL 
         LEA DX,MSG1 
CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for de䇛�ning
         JMP DISP 
Segments, Where we can write the coding of the program.
FAIL: LEA DX,MSG2 
DISP: MOV AH,09H 
Next      INT 21H 
Line –     ASSUME DS:DATA CS:CODE
      
      MOV AH,4CH 
In this Assembly Language Programming, their are Di틣erent Registers present for Di틣erent Purpose So we have to assume DATA is the name given to Data Segment
      INT 21H      
register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )
CODE ENDS 
END START
Next Line – START:

START is the label used to show the starting point of the code which is written in the Code Segment. : is used to de䇛�ne a label as in C programming.

Next Line – MOV AX,DATA


MOV DS,AX

After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register.  MOV is a keyword to move the second element into the 䇛�rst
element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the 䇛�rst and most
important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA
Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.

Next Line – MOV BX,00


      MOV DX,LEN
      MOV CX,KEY

The above two line of code is same as FIRST=0 and LAST=LEN the Di틣erence is that we are using Registers to Store Numbers, So we have t0 instruction MOV BX,00
move ZERO value to BX Register.  MOV DX,LEN move LEN variable value to DX Register. MOV CX,KEY move KEY variable value to CX Register.

Next Line – AGAIN: CMP BX,DX


JA FAIL

AGAIN: this will be starting point of while loop the condition cannot be written here So we write it Down and this is a LABEL and all the words ending in colon (:). CMP
BX,DX is used to compare Element of BX register with DX register and JA FAIL Short Jump if 䇛�rst operand (i.e. BX) is Above second operand (i.e. DX) to the respective
LABEL FAIL. The result of Comparision is not stored anywhere, but 튐ags are set according to result.

Next Line – MOV AX,BX


ADD AX,DX
SHR AX,1
MOV SI,AX
ADD SI,SI

MOV BX,CX is to move CX register to BX register. ADD AX,DX means Adding value of DX register with AX register. SHR means Shift operand1 Right. The number of
shifts is set by operand2. SHR AX,1 is used to Shift with 1. MOV SI,AX is to move AX register to SI register. ADD SI,SI means Adding value of SI register with SI register.

Next Line – CMP CX,ARR[SI]


JAE BIG
DEC AX

http://cssimplified.com/computer­organisation­and­assembly­language­programming/an­assembly­program­to­conduct­a­binary­search­on­a­given­sorted­arr… 3/5
1/19/2017 Assembly program to conduct a binary search on sorted array | Computer Science Simplified ­ A Website for IGNOU MCA & BCA Students for Solved A…
MOV DX,AX
JMP AGAIN

CMP CX,ARR[SI] is used to compare Element of CX register with Element of Array present in ARR[SI] and JAE BIG Short Jump if 䇛�rst operand (i.e. BX) is Above or Equal
to second operand (i.e. DX) to the respective LABEL BIG. The result of Comparision is not stored anywhere, but 튐ags are set according to result. DEC AX will decrement
the Address value present in AX register. MOV DX,AX is to move AX register to DX register. JMP AGAIN is used to Jump to Label AGAIN without any condition check.
This jump will basically Continue the Loop execution.

Next Line –BIG:   JE SUCCESS


       INC AX
       MOV BX,AX
       JMP AGAIN

BIG: is a LABEL and all the words ending in colon (:). JE SUCCESS Short Jump if 䇛�rst operand (i.e. BX) is Equal to second operand (i.e. DX) to the respective LABEL
SUCCESS. The result of Comparision is not stored anywhere, but 튐ags are set according to result. INC AX will increment the Address value present in AX register. MOV
BX,AX is to move AX register to BX register. JMP AGAIN is used to Jump to Label AGAIN without any condition check. This jump will basically Continue the Loop
execution.

Next Line – SUCCESS: ADD AL,01


         ADD AL,’0′
         MOV RES,AL
         LEA DX,MSG1
         JMP DISP

SUCCESS: is a LABEL and all the words ending in colon (:). ADD AL,01 means Adding value 01 to AL register. ADD AL,’0′ means Adding value of ’0′ (i.e. 30H) with AL
register. MOV RES,AL is to move AL register to RES variable. LEA DX,MSG1  in this LEA stands for LOAD EFFECTIVE ADDRESS and it loads the e틣ective address of
second element into the 䇛�rst element. JMP DISP is used to Jump to Label DISP without any condition check. This jump will basically Continue the Loop execution.

Next Line – FAIL: LEA DX,MSG2

FAIL: is a LABEL and all the words ending in colon (:). LEA DX,MSG2  in this LEA stands for LOAD EFFECTIVE ADDRESS and it loads the e틣ective address of second
element into the 䇛�rst element.

Next Line – DISP: MOV AH,09H


      INT 21H

DISP: is a LABEL and all the words ending in colon (:). MOV AH,09H  INT 21H is used to PRINT the String or Message of the address present in DX register.

Next Line – MOV AH,4CH


INT 21H

The above two line code is used to exit to dos or exit to operating system. Standard Input and Standard Output related Interupts are found in INT 21H which is also
called as DOS interrupt. It works with the value of AH register, If the Value is 4ch, That means Return to Operating System or DOS which is the End of the program.

Next Line – CODE ENDS

CODE ENDS is the End point of the Code Segment in a Program. We can write just ENDS But to di틣erentiate the end of which segment it is of which we have to write
the same name given to the Code Segment.

Last Line – END START

END START is the end of the label used to show the ending point of the code which is written in the Code Segment.

Note :- In this Assembly Language Programming, We have Com format and EXE format. We are Learning in EXE format only which simple then COM format to
understand and Write. We can write the program in lower or upper case, But i prepare Upper Case.

Screen Shots :-

http://cssimplified.com/computer­organisation­and­assembly­language­programming/an­assembly­program­to­conduct­a­binary­search­on­a­given­sorted­arr… 4/5
1/19/2017 Assembly program to conduct a binary search on sorted array | Computer Science Simplified ­ A Website for IGNOU MCA & BCA Students for Solved A…

Output After Execution :-

Note :- To see the variable and its value you have to click vars button in the emulator.

Category: Assembly Language Programs Computer Organisation and Assembly Language Programming Tags: 16-bit , array , Assembly , Binary , conduct , given ,
integer , key , program , search , sorted , unsigned

Rs.129 Rs.105 Rs.149 Rs.199 Rs.129 Rs.134 Rs.119

Iconic One Theme | Powered by Wordpress

http://cssimplified.com/computer­organisation­and­assembly­language­programming/an­assembly­program­to­conduct­a­binary­search­on­a­given­sorted­arr… 5/5

Anda mungkin juga menyukai