TITLE BOUNCE
;contains MOVE_BALL and CHECK_BOUNDARY procedures
   EXTRN DISPLAY_BALL:NEAR
   EXTRN TIMER_FLAG:BYTE,VEL_X:WORD ,VEL_Y:WORD
   PUBLIC MOVE_BALL
.MODEL SMALL
.CODE
MOVE_BALL PROC
;erase ball at current position and display ball at a new position
;input:CX= column of ball position
;      DX= row of ball position
;erase ball
    MOV AL,0           ;color 0 is a background color
    CALL DISPLAY_BALL  ;erase ball
   
    ADD CX,VEL_X
    ADD DX,VEL_Y
;check boundary
    CALL CHECK_BOUNDARY
;wait for 1 timer tick to display ball
TEST_TIMER:
    CMP TIMER_FLAG,1    ;timer ticked?
    JNE TEST_TIMER      ; no keep testing
    MOV TIMER_FLAG,0    ;yes,reset flag
    MOV AL,3            ;white color
    CALL DISPLAY_BALL   ;show ball
    RET
MOVE_BALL ENDP
CHECK_BOUNDARY PROC
   ;determine if ball is outside screen
   ;back in and change the ball direction
   ;input: CX= column of ball position
   ;       DX= row of ball position
   ;output CX= column of ball position
   ;       DX= row of ball position
   ;check column value
    CMP CX,11          ;left of 11 ? 
    JG L1              ;no,go check right margin
    MOV CX,11          ;yes,set to 11
    NEG VEL_X          ;change direction
    JMP L2             ;go test row boundary
L1: CMP CX,298         ;beyond right margin
    JL L2              ;no,go test row boundary
    MOV CX,298         ;set column to 298
    NEG VEL_X          ;change direction
;check row value
L2: CMP DX,11          ;above top margin

    JG L3              ;no,check bottom margin
    MOV DX,11          ;set to 11
    NEG VEL_Y          ;change direction
    JMP DONE           ;done
L3: CMP DX,187         ;below nottom margin?
    JL DONE            ;no,done
    MOV  DX,187        ;yes,set to 187 
    NEG VEL_Y          ;change direction
DONE:
    RET
CHECK_BOUNDARY ENDP
    END


