Tuesday, October 26, 2010

Microprocessor(Program to find out largest number)

A program to find out the largest number from a given unordered array of 8-bit numbers,stored in the location starting from a known address.



Logic:    Compare the th number of the series with the (i+1)th number using CMP instruction .it will set the flags appropriately depending upon whether the i th number or the (i+1)th number is greater.if the i th number is greater than (i+1)th number is greater than (i+1)th, leave it in the AX(any register may be used).Otherwise load the (i+1)th number in AX,replacing the i th number in AX.The procedure is repeated till all the members in the array have been compared.




  ASSUME CS:CODE,DS:DATA                                                      
  DATA SEGMENT                                                     ; Data segment starts
  LIST DB 52H,23H,56H,45H--                                   ; List of byte numbers
  COUNT EQU OF                                                       ; Number of bytes in the list
  LARGEST DB 01H DUP(?)                            ; One byte is reserved for the largest no.
  DATA ENDS                                                              ; Data segment ends
  CODE SEGMENT                                                       ; Code segment starts

  START:           MOV     AX,DATA                             ; Initialize data segment
                        MOV     DS,AX
                        MOV     SI,OFFSET LIST
                        MOV     CL,COUNT                               ; Number of bytes in CL
                        MOV     AL,[SI]                                       ; Take the first number in AL

  AGAIN:            CMP     AL,[SI+1]                          ; and compare it with the next number
                         JNL      NEXT                                                        
                         MOV    AL,[SI+1]  

  NEXT:             INS      SI                                            ; Increment pointer to the byte list
                         DEC     CL                                                ; Decrement counter
                         JNZ      AGAIN                     ; If all the nos. are compared,point to result
                         MOV    SI,OFFSET LARGEST              ; Destination and store it
                         MOV    [SI],AL
                         MOV    AH,4CH                                      ;return to DOS
                         INT       21H
                         CODE   ENDS
  END               START
                        

Note: ";" these are the comment in program for better understanding.

1 comment: