CSc 343
Computer Organization and Programming
Fall Quarter 1997
Programming Assignment #3 (14 November 1997, Friday)

Assignment Objectives: To learn how to manipulate the screen in graphics mode by writing directly to the video buffer, and to become more proficient at writing assembly programs.

Problem Description: Write an assembler program that will display a sorting (using bubblesort) in progress of 20 numbers on the screen. Each of the 20 numbers will be displayed on the screen as a vertical bar of proportional height. We shall use graphics mode 13H, the VGA 256-color mode. This mode divides the screen into a 200 row by 320 column array of pixels. Since there are 200 rows in this mode, the numbers will be in the range 0 to 200 (i.e. the height of the bars will between 0 and 200).

The programs should be written in three phases described below:

Phase 1:
When the program starts, it should display 20 bars on the screen. Each bar should have a height of 100 pixels with its base at the bottom of the screen. The bars should be displayed initially in color 1. The background must be black. Since we have 320 columns, the first bar should be displayed after 4 blank columns, each bar should occupy 8 columns and there should be 8 blank columns between any two bars (160 columns for the 20 bars, 152 columns for the blank space between columns and 4 columns each for the blank space before the first bar and after the last bar). By pressing the space bar, the user may change the color of all bars to the next color (color 2, 3, 4 and so on). The highest color has the number 247. If the user presses the space bar at color 247, the colors should start over with 1. The program remains in this phase until the user presses the escape key (causing the program to terminate) or the enter key (causing the program to go to phase 2). In phase 1, the program ignores all keys other than space bar, escape and enter keys.

Phase 2:
When the program enters phase 2, the leftmost bar should be highlighted (displayed in bright white - color 15). The user may now move the highlight to the left or right by pressing the left arrow and the right arrow keys. If the user presses the left arrow key when the leftmost bar is highlighted, the highlight should move to the rightmost bar. Similarly, if the user presses the right arrow key when the rightmost bar is highlighted, the highlight should move to the leftmost bar. Once the desired bar is highlighted, the user may press the up arrow key (to increase the height of the highlighted bar by 5 pixels) or the down arrow key (to decrease the height of the highlighted bar by 5 pixels). Once the height of the bar reaches the maximum (200) the up arrow key should have no effect and similarly when the height of the bar reaches the minimum (0) the down arrow key should have no effect. Any number of bars may be altered in this manner. The program remains in phase 2 until the user presses the escape key (causing the program to terminate) or the enter key (causing the program to remove the highlight and go to phase 3). In phase 2, the program ignores all keys other than the left arrow, right arrow, up arrow, down arrow, escape and enter keys.

Phase 3:
The sorting of the numbers now begins in phase 3. After each comparison of two bars (and possibly exchange of the two bars) the right bar should be highlighted. If the left bar was previously highlighted, its highlight should be removed. During this phase, the user may press the escape key at any time to terminate the execution of the program. If the sorting completes before the user presses any key, the final screen should be displayed indefinitely until the user presses the escape key.

The program should introduce a delay between between two comparisons in the sorting process otherwise the whole process will complete in a very short time. The initial delay should be 32768 and the user should be able to increase or decrease the delay using the Ins key (to double the delay) and the Del key (to halve the delay). The Ins key should have no effect once the delay reaches 32768 and the Del key should have no effect when the delay reaches 1.

The delay can be introduced by the following code:

DELAY  DW  32768


  MOV  DX,0
WAIT_LOOP:
  CALL READ_KEY
  INC  DX
  CMP  DX,DELAY
  JB   WAIT_LOOP
Note that we are checking for user input within the delay loop to respond to the user key strokes instantaneously. If the user does not press any key, READ_KEY returns immediately.

The bubblesort algorithm to be used for this phase is described below:

  LIMIT := 19;
  repeat
    SWAPPED := false;
    for I := 0 to (LIMIT-1) do
      begin
        introduce_delay;
        if A[I] > A[I+1] then
          begin
            swap(A[I],A[I+1]);
            SWAPPED := true
          end
      end;
    LIMIT := LIMIT - 1
  until not SWAPPED

Submission Instructions: Electronically submit sort.asm and sort.exe by the deadline and submit program listing of sort.asm.

Hints:

  1. Write your program in phases. Get the first phase working before you go to the second phase. Similarly get the second phase working before you go to the third phase. Credit will be given for each phase.
  2. You data segment should include the following definition:
      bars  dw  20 DUP(100)
    to define the 20 numbers initialized to 100.
  3. To achieve acceptable performance, your program will write directly to video memory. In mode 13H, the VGA buffer is organized into 200 rows, with 320 bytes per row. Row 0 begins at address A000:0000H; rows 1 to 199 follow sequentially.
  4. Some new DOS/BIOS services to use in this assignment (you will need some of the services used in Assignments 1 and 2 as well):
      INT 10H, Function 00H: Set Video Mode
      INT 10H, Function 0BH: Set Background Color
  5. It would be useful to write the following procedures:
  6. You should use the ES segment register to address the video memory. You need to define the segment as follows:
      video_segment segment at 0A000H
        video_area db 0FFFFH DUP(BLACK)  ; Substitute the color code for BLACK
      video_segment ends
    and execute the following instructions at the beginning of the main program:
      MOV   AX,video_segment
      MOV   ES,AX
      assume es:video_segment
    After this is done, you may now access the video_area variable in your program in the usual manner.



Raj Sunderraman
Sun Nov 2 19:49:30 PST 1997