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.
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.
http://cssimplified.com/computerorganisationandassemblylanguageprogramming/anassemblyprogramtoconductabinarysearchonagivensortedarr… 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!!!.$’
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.
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/computerorganisationandassemblylanguageprogramming/anassemblyprogramtoconductabinarysearchonagivensortedarr… 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.
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.
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.
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.
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.
http://cssimplified.com/computerorganisationandassemblylanguageprogramming/anassemblyprogramtoconductabinarysearchonagivensortedarr… 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.
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.
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.
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.
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.
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.
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.
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/computerorganisationandassemblylanguageprogramming/anassemblyprogramtoconductabinarysearchonagivensortedarr… 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…
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
http://cssimplified.com/computerorganisationandassemblylanguageprogramming/anassemblyprogramtoconductabinarysearchonagivensortedarr… 5/5