Skip to content
Related Articles

Related Articles

Improve Article
8086 program to search a number in a string
  • Last Updated : 22 May, 2018

Problem – Write an assembly language program in 8086 microprocessor to search a number in a string of 5 bytes, store the offset where the element is found and the number of iterations used to find the number.

Example –

Algorithm –

  1. Move 2000 in AX and assign it to ES
  2. Assign value 600 to DI
  3. Move 25 in AL
  4. Move 0005 in CX
  5. Move the contents of CX to BX
  6. Clear the value of Directional Flag (DF)
  7. Repeat step 7 till Zero Flag (ZF) is not set
  8. Scan byte from [DI] and check its difference with contents of AL. Update the value of DI
  9. Decrements the value of DI by 1
  10. Subtract the value of BX by CX
  11. Decrements the value of BX by 1
  12. Halt the program

Program –



OFFSETMNEMONICSCOMMENT
400MOV AX, 2000AX <- 2000
403MOV ES, AXES <- AX
405MOV DI, 600DI <- 600408MOV AL, 25AL = 2540AMOV CX, 0005CX = 000540DMOV BX, CXBX <- CX40FCLDDF = 0 so that memory is accessed
from lower byte to higher byte
410REPNE SCAS BRepeat till ZF = 0. Scan value from [DI] and
compare with AL, Increment DI
414DEC DIDI = DI – 1415MOV DX, DIDX = DI417SUB BX, CXBX = BX – CX419DEC BXBX = BX – 141BHLTStop

Explanation –

  1. MOV AX, 2000 is used to move data to register AX.
  2. MOV ES, AX is used to move the data of AX to segment register ES.
  3. MOV DI, 600 is used to move offset 600 to Destination Index(DI).
  4. MOV AX, 25 is used to move data to AL.
  5. MOV CX, 0005 is used to move data to CX.
  6. MOV BX, CX is used to copy contents of CX to BX.
  7. CLD is used to clear the value of Directional Flag (DF), so that memory is accessed from lower byte to higher byte.
  8. REPNE SCAS B is used to scan data from DI and compare it with data from AL, if equal go to next step else increment the value of DI by 1 and repeat this step.
  9. DEC DI is used to decrement contents of DI by 1.
  10. MOV DX, DI is used to move the contents of DI to DX.
  11. SUB BX, CX is used to subtract the contents of BX by CX.
  12. DEC BX is used to decrement contents of BX by 1.
  13. HLT stops executing the program and halts any further execution.
My Personal Notes arrow_drop_up
Recommended Articles
Page :