Thursday, May 17, 2012

8086 Assembly (TASM): "Toy" Program

I thought I'd post this here, just in case I end up losing it and needing it later on.  Or whatever...

This was the assignment given:


Write a program to emulate a child's toy which is played by dropping a marble in one of the three holes ( A, B, or C ) at the top of the toy and watching the changes.  As the marble moves past switches, the values of the switch are changed.   The path which the marble will take is dependent upon the current state of the switch.  Your game display should look something like the following: the letters should be displayed, the numbers representing the switch positions, and the value of each switch which is initially a 0:



                   A                           B                           C
                   1                           2                           3
                   0                           0                           0
                                4                           5
                                0                           0
                   6                           7                           8
                   0                           0                           0


          Treat the toy as a multi-switch device with each switch having two states and being independent of each other.   Each switch's input and output are defined as follows:

          INPUT :
                   hole A :  the marble goes to Switch 1
                   hole B :  the marble goes to Switch 2
                   hole C :  the marble goes to Switch 3
          OUTPUT:
                   When Switch 1 is 0, the marble goes to Switch 6
                   When Switch 1 is 1, the marble goes to Switch 4
                   When Switch 2 is 0, the marble goes to Switch 5
                   When Switch 2 is 1, the marble goes to Switch 4
                   When Switch 3 is 0, the marble goes to Switch 5
                   When Switch 3 is 1, the marble goes to Switch 8
                   When Switch 4 is 0, the marble goes to Switch 7
                   When Switch 4 is 1, the marble goes to Switch 6
                   When Switch 5 is 0, the marble goes to Switch 8
                   When Switch 5 is 1, the marble goes to Switch 7
                   When Switch 6 is 0, the marble exits the Toy
                   When Switch 6 is 1, the marble exits the Toy
                   When Switch 7 is 0, the marble exits the Toy
                   When Switch 7 is 1, the marble exits the Toy
                   When Switch 8 is 0, the marble exits the Toy
                   When Switch 8 is 1, the marble exits the Toy

          The Toy starts with a display of the board having all switches set to 0.

          Accept the input from the keyboard (provide an appropriate prompt)  of A, B, or C which is the selection of the hole into which to drop the marble.  Calculate the state changes of the switches, change them, and then re-display the board in its new state. Continue until the proper exit code is provided.  You must idiot proof your program to ignore improper values.  Use of procedures and Macros as appropriate is required.
And here's the garbage that I regurgitated into Notepad!

; Toy.asm
; 
;    What the array looks like when not crammed onto one line.
;    a[0,0] is the array element at position [0,0].  and so on.
;    Essentially, there are 3 rows, just like the assignment description:
;        1st Row has 3 columns.
;        2nd Row has 2 columns.
;        3rd Row has 3 Columns. 
;        +--------+--------+--------+
;        | a[0,0] | a[0,1] | a[0,2] |
;        | OFF 0  | OFF 1  | OFF 2  |
;        +--------+--------+--------+
;          | a[1,0] | a[1,1] | 
;           | OFF 3  | OFF 4  | 
;        +--------+--------+--------+
;        | a[2,0] | a[2,1] | a[2,2] |
;        | OFF 6  | OFF 7  | OFF 8  |
;        +--------+--------+--------+

.MODEL small

.STACK 100h

.DATA
  ; 1FH = ON
  ; 7FH = OFF
  PRMPT1  DB 10, 10, 13, '[q] Or [Q] To Quit'
     DB 10, 10, 13, '[A], [B], [C] To Drop Ball: $'

  HEADER  DB 10, 10, 13, '          ', 41H, '          ', 42H, '          ', 43H, '$'                 ; A  B  C     
  
  LINE1A  DB 10, 10, 13, '          $'
  LINE1B  DB '          $'
  LINE1C  DB '          $'

  LINE2A  DB 10, 10, 13, '               $'
  LINE2B  DB '          $'

  LINE3A  DB 10, 10, 13, '          $'
  LINE3B  DB '          $'
  LINE3C  DB '          $'
  
  SWITCHES DB 1FH, 1FH, 1FH
     DB 1FH, 1FH
     DB 1FH, 1FH, 1FH
  
.CODE

 START PROC
   MOV AX, @DATA   ; Startup code.
   MOV DS, AX

  DRAWGAME:
    CALL CLRSCRN   ; Clear the screen
  
    MOV DX, OFFSET HEADER  ; Display menu
    MOV AH, 09H
    INT 21H

    MOV DX, OFFSET LINE1A  ; Draw Row 1, Col A
    MOV AH, 09H
    INT 21H

      MOV DL, SWITCHES + 0 ; Display switch 1 value.
      MOV AH, 02h             
      INT 21H                     
    
    MOV DX, OFFSET LINE1B  ; Draw Row 1, Col B
    MOV AH, 09H
    INT 21H

      MOV DL, SWITCHES + 1 ; Display switch 2 value.
      MOV AH, 02h             
      INT 21H                   
    
    MOV DX, OFFSET LINE1C  ; Draw Row 1, Col C
    MOV AH, 09H
    INT 21H    
    
      MOV DL, SWITCHES + 2 ; Display switch 3 value.
      MOV AH, 02h         
      INT 21H                 
    
    MOV DX, OFFSET LINE2A  ; Draw Row 2, Col B
    MOV AH, 09H
    INT 21H

      MOV DL, SWITCHES + 3  ; Display switch 4 value.
      MOV AH, 02h             
      INT 21H                 
    
    MOV DX, OFFSET LINE2B  ; Draw Row 2, Col C
    MOV AH, 09H
    INT 21H

      MOV DL, SWITCHES + 4  ; Display switch 5 value.
      MOV AH, 02h             
      INT 21H                 
       
    MOV DX, OFFSET LINE3A  ; Draw Row 3, Col A
    MOV AH, 09H
    INT 21H

      MOV DL, SWITCHES + 5  ; Display switch 6 value.
      MOV AH, 02h             
      INT 21H                 
    
    MOV DX, OFFSET LINE3B  ; Draw Row 3, Col B
    MOV AH, 09H
    INT 21H

      MOV DL, SWITCHES + 6  ; Display switch 7 value.
      MOV AH, 02h             
      INT 21H                 
    
    MOV DX, OFFSET LINE3C  ; Draw Row 3, Col C
    MOV AH, 09H
    INT 21H 

      MOV DL, SWITCHES + 7  ; Display switch 8 value.
      MOV AH, 02h             
      INT 21H                     

    MOV DX, OFFSET PRMPT1  ; Display menu
    MOV AH, 09H
    INT 21H 
   
  GETINPUT:
   MOV AX, 0
   MOV AH, 08H    ; Get user input.
   INT 21H
   
   CMP_bA:
    CMP AL, 41H    ; Is input = "A"?
    JNE CMP_sA    ; If not, check for "a".
    JMP SWITCH1    ;  If it is, Jump to Switch #1.
    
   CMP_sA:      
    CMP AL, 61H    ; Is input = "a"?
    JNE CMP_bB    ; If not, check for "B".
    JMP SWITCH1    ; If it is, jump to Swtich #1.
    
   CMP_bB:
    CMP AL, 42H    ; Is input = "B"?
    JNE CMP_sB    ; If not, check for "b".
    JMP SWITCH2    ; If it is, jump to Switch #2.
    
   CMP_sB:
    CMP AL, 62H    ; Is input = "b"?
    JNE CMP_bC    ; If not, check for "C".
    JMP SWITCH2    ; If it is, jump to Switch #2.
    
   CMP_bC:
    CMP AL, 43H    ; Is input = "C"?
    JNE CMP_sC    ; If not, check for "c".
    JMP SWITCH3    ; If it is, jump to Swtich #3.
    
   CMP_sC:
    CMP AL, 63H    ; Is input = "c"?
    JNE CMP_bQ    ; If not, check for "Q".
    JMP SWITCH3    ; If it is, jump to Switch #3.
    
   CMP_bQ:
    CMP AL, 51H    ; Is input = "Q"?
    JNE CMP_sQ    ; If not, check for "q".
    JMP EXITPROG   ; If it is, jump to EXITPROG.
    
   CMP_sQ:
    CMP AL, 71H    ; Is input = "q"?
    JNE GETINPUT   ; If it is not, then user did not enter any of the above keys.
    JMP EXITPROG   ; If it is, jump to EXITPROG.
   
  SWITCH1:  
   CMP [SWITCHES + 0], 7FH   ; if character in switch 1 is 7FH
   JE SWITCH1A
   
   CMP [SWITCHES + 0], 1FH   ; if character in switch 1 is 1FH.
   JE SWITCH1B   
   
   SWITCH1B:
    MOV [SWITCHES + 0], 7FH     ; switch character to OFF
    JMP SWITCH4

   SWITCH1A:
    MOV [SWITCHES + 0], 1FH    ; switch character to ON
    JMP SWITCH6
    
  SWITCH2:
   CMP [SWITCHES + 1], 7FH   ; if character in switch 2 is 7FH
   JE SWITCH2A
   
   CMP [SWITCHES + 1], 1FH   ; if character in switch 2 is 1FH.
   JE SWITCH2B   
   
   SWITCH2B:
    MOV [SWITCHES + 1], 7FH  ; switch character to OFF
    JMP SWITCH2

   SWITCH2A:
    MOV [SWITCHES + 1], 1FH  ; switch character to ON
    JMP SWITCH5
 
  SWITCH3:
   CMP [SWITCHES + 2], 7FH   ; if character in switch 3 is 7FH
   JE SWITCH3A
   
   CMP [SWITCHES + 2], 1FH   ; if character in switch 3 is 1FH.
   JE SWITCH3B   
   
   SWITCH3B:
    MOV [SWITCHES + 2], 7FH  ; switch character to OFF
    JMP SWITCH8

   SWITCH3A:
    MOV [SWITCHES + 2], 1FH  ; switch character to ON
    JMP SWITCH5
   
  SWITCH4:
   ; Code for Switch #4 goes here.
   CMP [SWITCHES + 3], 7FH   ; if character in switch 4 is 7FH
   JE SWITCH4A
   
   CMP [SWITCHES + 3], 1FH   ; if character in switch 4 is 1FH.
   JE SWITCH4B   
   
   SWITCH4B:
    MOV [SWITCHES + 3], 7FH  ; switch character to OFF
    JMP SWITCH6

   SWITCH4A:
    MOV [SWITCHES + 3], 1FH  ; switch character to ON
    JMP SWITCH7
   
  SWITCH5:
   CMP [SWITCHES + 4], 7FH   ; if character in switch 5 is 7FH
   JE SWITCH5A
   
   CMP [SWITCHES + 4], 1FH   ; if character in switch 5 is 1FH.
   JE SWITCH5B   
   
   SWITCH5B:
    MOV [SWITCHES + 4], 7FH  ; switch character to OFF
    JMP SWITCH7

   SWITCH5A:
    MOV [SWITCHES + 4], 1FH  ; switch character to ON
    JMP SWITCH8
   
  SWITCH6:
   CMP [SWITCHES + 5], 7FH   ; if character in switch 6 is 7FH
   JE SWITCH6A
   
   CMP [SWITCHES + 5], 1FH   ; if character in switch 6 is 1FH.
   JE SWITCH6B   
   
   SWITCH6B:
    MOV [SWITCHES + 5], 7FH  ; switch character to OFF
    JMP DRAWGAME

   SWITCH6A:
    MOV [SWITCHES + 5], 1FH  ; switch character to ON
    JMP DRAWGAME
   
  SWITCH7:
   CMP [SWITCHES + 6], 7FH   ; if character in switch 7 is 7FH
   JE SWITCH7A
   
   CMP [SWITCHES + 6], 1FH   ; if character in switch 7 is 1FH.
   JE SWITCH7B   
   
   SWITCH7B:
    MOV [SWITCHES + 6], 7FH  ; switch character to OFF
    JMP DRAWGAME

   SWITCH7A:
    MOV [SWITCHES + 6], 1FH  ; switch character to ON
    JMP DRAWGAME
   
  SWITCH8:
   CMP [SWITCHES + 7], 7FH   ; if character in switch 8 is 7FH
   JE SWITCH8A
   
   CMP [SWITCHES + 7], 1FH   ; if character in switch 8 is 1FH.
   JE SWITCH8B   
   
   SWITCH8B:
    MOV [SWITCHES + 7], 7FH  ; switch character to OFF
    JMP DRAWGAME

   SWITCH8A:
    MOV [SWITCHES + 7], 1FH  ; switch character to ON
    JMP DRAWGAME
    
  EXITPROG:
   CALL CLRSCRN
   
   MOV AL, 0    ; exit to DOS.
   MOV AH, 04ch
   INT 21h
 START ENDP

 CLRSCRN PROC
   MOV AH, 0fh
   INT 10h
   
   MOV AL, 00h
   MOV AH, 0
   INT 10h 
   
   RET
 CLRSCRN ENDP 
END START   

No comments:

Post a Comment