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 | April 25, 2014

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.

 BINARY_SEARCH

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 different 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 identified 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 defining 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 ”
     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 Define 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 differentiate 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 sufficient.

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 defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define 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 different letters for different 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).

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
      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
       JMP AGAIN
BIG:   JE SUCCESS
       INC AX
       MOV BX,AX
       JMP AGAIN
SUCCESS: ADD AL,01
         ADD AL,'0'
         MOV RES,AL
         LEA DX,MSG1
         JMP DISP
FAIL: LEA DX,MSG2
DISP: MOV AH,09H
      INT 21H
     
      MOV AH,4CH
      INT 21H     
CODE ENDS
END START

Explanation : 

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  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 need two Segments i.e. Code Segment and Data Segment.

Next Line – CODE SEGMENT

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 defining Segments, Where we can write the coding of the program.

Next Line –     ASSUME DS:DATA CS:CODE

In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )

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 define 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 first 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 first 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 Difference 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 first 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 flags 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
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 first 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 flags 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 first 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 flags 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 effective address of second element into the first 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 effective address of second element into the first 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 differentiate 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 :-

 Asm_program_Binary_Search_16-bit

Output After Execution :-

 Asm_program_Binary_Search_16-bit_Output

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

Leave a Reply