;This program moves an animated car randomly
car_loop: call   draw_car
	  call   pause
	  call   erase_car
	  call   random_pos
	  mov    ah, 1
	  int    16h                      ; see if a key is waiting
	  jz     car_loop

	  mov    ax, 4c00h
	  int   21h
car_row   db     10
car_col   db     30
pause:    push   si
	  push   ax
	  mov    si, 200
outter:   mov    ax, 0ffffh
lp1:      sub    ax, 1
	  jnz    lp1
	  sub    si, 1
	  jnz    outter
	  pop    ax
	  pop    si
	  ret
;  input and output:  car_row and car_col

random_pos:  push     ax

	     mov      ax, 6
	     int      62h
	     cmp      ax, 1
	     jbe      right
	     cmp      ax, 3
	     jbe      left
	     cmp      ax, 4
	     je       up
	     add      car_row, 1
	     jmp      rand_exit
right:       add      car_col, 1
	     jmp      rand_exit
left:        sub      car_col, 1
	     jmp      rand_exit
up:          sub      car_row, 1
rand_exit:    pop      ax
	     ret






erase_car:       push   ax
		 push   bx
		 push   cx
		 push   dx
		 mov    al, ' '
		 mov    bl, 07h
		 mov    cx, 1
		 mov    dh, car_row
		 mov    dl, car_col
		 call   paint
		 add    dh, 1
		 sub    dl, 2
		 mov    cx, 5
		 call   paint
		 pop    dx
		 pop    cx
		 pop    bx
		 pop    ax
		 ret

;         input (dh, dl) row and col of where to paint car
draw_car: push   ax
	  push   bx
	  push   cx
	  push   dx
	  mov    dh,    car_row
	  mov    dl,    car_col
	  mov    al, 1                 ;happy face
	  mov    cx, 1
	  mov    bl, 0Eh               ;yellow on black
	  call   paint
	  add    dh, 1
	  sub    dl, 2                 ;position of first car
	  mov    al, 0C8h              ;left side of car
	  mov    bl, 01h               ;blue on black
	  call   paint
	  add    dl, 1
	  mov    cx, 3
	  mov    al, 0CDh              ;char for middle of car
	  call   paint
	  add    dl, 3
	  mov    cx, 1
	  mov    al, 0BCh              ;right side of car
	  call   paint
	  pop   dx
	  pop   cx
	  pop   bx
	  pop   ax
	  ret



;      input    dh = row
;               dl = col
set_pos:  push   ax
	  push   bx
	  mov    bh, 0                 ;page 0
	  mov    ah, 2                 ;function set cursor
	  int    10h
	  pop    bx
	  pop    ax
	  ret
;  paints chars at the current cursor location
;         input  = cx num of char
;                  al = ascii char
;                  bl = attrib
;
paint:    push   ax
	  push   bx
	  cmp    cx, 0
	  je     paint_done
	  call   set_pos
	  mov    bh, 0                 ;page 0
	  mov    ah, 9                 ;function for writing a char
	  int    10h
paint_done:
	  pop    bx
	  pop    ax
	  ret
	  end