8086 Assembly Program to Find Smallest Number from Given Numbers

​In this blog post, we’ll explore how to find the smallest number from a given set of values using an 8086 assembly language program. The following code snippet demonstrates this process:

data segment
STRING1 DB 08h,14h,05h,0Fh,09h
res db ?
data ends

code segment
assume cs:code, ds:data
start: mov ax, data
mov ds, ax
mov cx, 04h

mov bl, 79h
LEA SI, STRING1
up:
mov al, [SI]
cmp al, bl
jge nxt
mov bl, al
nxt:
inc si
dec cx
jnz up

mov res,bl
int 3
code ends
end start

Step-by-Step Explanation:

1. Data Segment:

  • STRING1: Defines an array of bytes (8-bit values) to store the numbers: 08h, 14h, 05h, 0Fh, 09h.​
  • res: Reserves a single byte to store the smallest number found.​

2. Code Segment:

Initialization:

  • mov ax, data: Loads the address of the data segment into the AX register.​
  • mov ds, ax: Sets the DS register to point to the data segment, allowing access to variables.​
  • mov cx, 04h: Initializes the loop counter CX to 4, as there are 4 numbers to compare.​

3. Finding the Smallest Number:

  • mov bl, 79h: Initializes the BL register to 79h, which is higher than any number in the array, ensuring that any number in the array will be smaller.​
  • LEA SI, STRING1: Loads the address of the first element of the STRING1 array into the SI register.​

Loop:

  1. mov al, [SI]: Loads the current number from the STRING1 array into the AL register.​
  2. cmp al, bl: Compares the current number (AL) with the smallest number found so far (BL).​
  3. jge nxt: If AL is greater than or equal to BL, jump to the next iteration.​
  4. mov bl, al: If AL is less than BL, update BL with the new smallest number.​
  5. inc si: Increment the SI register to point to the next number in the array.​
  6. dec cx: Decrement the loop counter.​
  7. jnz up: Jump back to the beginning of the loop if CX is not zero.

Storing the Result:

  • mov res, bl: Stores the smallest number (in BL) into the res variable.​

Program Termination:

  • int 3: Generates an interrupt 3, which typically halts the program execution.

Overall Process:

  1. The program initializes the loop counter and a variable to store the smallest number.​
  2. It iterates through each number in the array.​
  3. For each number, it compares it with the current smallest number.​
  4. If the current number is smaller, it updates the smallest number.​
  5. After processing all numbers, the smallest number is stored in the res variable.​
  6. The program terminates.

Output

C:\TASM>masm smallest.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987.  All rights reserved.

Object filename [smallest.OBJ]:
Source listing  [NUL.LST]:
Cross-reference [NUL.CRF]:

  50836 + 450860 Bytes symbol space free

      0 Warning Errors
      0 Severe  Errors

C:\TASM>link smallest.obj

Microsoft (R) Overlay Linker  Version 3.60
Copyright (C) Microsoft Corp 1983-1987.  All rights reserved.

Run File [SMALLEST.EXE]:
List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug smallest.exe
-g

AX=0B0F  BX=0005  CX=0000  DX=0000  SP=0000  BP=0000  SI=0004  DI=0000
DS=0B37  ES=0B27  SS=0B37  CS=0B38  IP=001E   NV UP EI PL ZR NA PE NC
0B38:001E CC            INT     3
-d 0b37:0000
0B37:0000  08 14 05 0F 09 05 00 00-00 00 00 00 00 00 00 00   ................
0B37:0010  B8 37 0B 8E D8 B9 04 00-B3 79 8D 36 00 00 8A 04   .7.......y.6....
0B37:0020  3A C3 7D 02 8A D8 46 49-75 F4 88 1E 05 00 CC FC   :.}...FIu.......
0B37:0030  FE C4 9E FA FE 26 8A 47-0C 2A E4 40 50 8B C3 05   .....&.G.*.@P...
0B37:0040  0C 00 52 50 E8 19 46 83-C4 04 50 8D 86 00 FF 50   ..RP..F...P....P
0B37:0050  E8 6F 70 83 C4 06 B8 CD-05 50 8D 86 00 FF 50 E8   .op......P....P.
0B37:0060  CA 0C 83 C4 04 B8 FF FF-50 8D 86 00 FF 50 8D 46   ........P....P.F
0B37:0070  80 50 E8 4D FA 83 C4 06-0A C0 75 03 E9 7B FF 5E   .P.M......u..{.^
-q

Understanding the Memory Dump

The memory dump provided by the -d 0b37:0000 command displays the contents of memory starting from the DS (data segment) base address, which is 0b37:0000 in this case.

Here is the relevant portion of the memory:

0B37:0000  08 14 05 0F 09 05 00 00-00 00 00 00 00 00 00 00

  • 08 14 05 0F 09: These are the values of STRING1, matching the array initialized in the data segment.
  • 05: This is the value of the variable res, where the smallest value is stored.

3 thoughts on “8086 Assembly Program to Find Smallest Number from Given Numbers”

    1. 79h is chosen as an arbitrarily large value to ensure that any number in the array is smaller, allowing the program to correctly find the smallest value. It initializes BL so that comparisons can proceed without errors. As the loop iterates, each number is compared with BL, and if a smaller number is found, BL is updated. By the end of the loop, BL holds the smallest number. An alternative approach is to initialize BL with the first element of the array instead.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.