The same program can be written using the loop

This preview shows page 9 - 12 out of 22 pages.

The same program can be written using the LOOP instruction, in which case, DEC CX and JNZ DO-NEXT instructions are replaced by LOOP DO-NEXT instruction. LOOP decrements the value of CX and jumps to the given label, only if CX is not equal to zero. 3.3.2 Find The Largest And The Smallest Of The Array Values Let us now put together whatever we have done in the preceeding sections and write down a completc program to find the largest and the smallest numbers from a given array. The program uses the JGE (jump greater than or equal to) instruction, bec;ruse wc have assumcd the array values as signed. JAE instruction works correctly only in thc case of unsigncd numbers. For example, if we used JAE to compare -1 to 0, JAE would treat the numbers as unsigned and consider -1
Image of page 9

Subscribe to view the full document.

Mkroproccssor and h m b b (FFFFW) to be the larger number. JGE on the other hand, would treat the numbers . Language Programming as signed and consider 0 to be greater than -1. / The smallest and the largest variables are first initialized to the first number in the array. They are then compared with the other array values one by one. If the value happens to be smaller than the assumed smallest number or larger than the assumed largest value, the smallest and the largest variables are changed with the new values respectively. DI points to the current array value and LOOP instruction is used to scan the array elements. CODE SEGMENT MOV AX,DATA MOV DS,AX ; initialize DS MOV DI, OFFSET ARRAY ; DI points to the array MOV AX, [DI] ; AX contains the first element MOV LARGEST, AX ; initialize largest MOV SMALLEST, AX ; initialize smallest MOV CX,6 ; loop counter Al: MOV AX, [DI] ; get array value CMP AX, SMALLEST ; [DI] = smallest? JGE A2 ; yes : skip MOV SMALLEST, AX ; no : move [DI] to smallest JMP A3 ; as it is smallest, thus no need ; to compare it with the largest . i I A2: CMP AX,LARGEST ; [DI] = largest JLE A3 ; yes : skip f MOV LARGEST, AX ; no : mov [Dl] to largest A3: ADD DI,2 ; point to next number LOOP A1 ; repeat the loop until CX = 0 MOV AX, 4C00h INT 21h ; halt, return to DOS CODE ENDS DATA SEGMENT ARRAY DW -1, 2000, -4000, 32767, 500, 0 LARGEST DW ? SMALLEST DW ? DATA ENDS
Image of page 10
2 is added to DI to poinl to ncxl elcment of the array, as the array consists of words, whose length is equal to 2 bytes. This program can be made faster if smallest and largest are stored temporarily in registers for comparisons. , 3 3 3 Character Coded Data The arithmetic shown so far has dealt with numbers. The CPU calculates in binary. We may however be required to work with characters that are directly inputted from the console or a file. These characters are called as ASCII characters. Suppose we want to input two numbers from the console and add them together. You must have already written such kind of programs in high level language. The following is a sample console session.
Image of page 11

Subscribe to view the full document.

Image of page 12
You've reached the end of this preview.
  • Spring '10
  • Ramon
  • Assembly Language, X86, Binary numeral system, ........., Binary-coded decimal, ax

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern