Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. BITS 16
  2.  
  3. ;;;;;;;;;;;;;;
  4. ; CONSTANTS
  5. ;;;;;;;;;;;;;;
  6. stacksize       EQU 0200h
  7. ; starting address of video memory
  8.  
  9. videobase       EQU 0a000h
  10.  
  11. ; some colors
  12.  
  13. black           EQU 0
  14. green           EQU 00110000b
  15. blue            EQU 00001001b
  16. red             EQU 00000100b
  17. white           EQU 00001111b
  18. grey            EQU 00000111b
  19. yellow          EQU 00001110b
  20. transparent     EQU 11111111b  ;Note: this is our convention for this exercise - see the bitmap copy later...
  21.  
  22. ; screen width in pixels, in the graphics mode 320x200
  23.  
  24. scrwidth    EQU 320
  25.  
  26. ;;;;;;;;;;;;;;
  27. ; DATA AND STACK SEGMENTS
  28. ;;;;;;;;;;;;;;
  29.  
  30. segment memscreen data
  31.     resb 320*200
  32.  
  33. segment background data
  34.     resb 320*200
  35.        
  36. segment mystack stack
  37.     resb stacksize
  38. stacktop:  
  39.    
  40. segment mydata data
  41.     ; DEFINE YOUR VARIABLES HERE
  42.     foo resw    1
  43.     pressesc dw 0
  44.     oldintseg resw  1
  45.     oldintoff resw  1
  46.     delay dw    2
  47.     oldgramod resw  1
  48.     move dw     6
  49.     pacmanloc dw    32160
  50.     toofast dw  0
  51.    
  52. segment bitmaps data
  53. PacmanMap db transparent,transparent,transparent,transparent,yellow,yellow,yellow,transparent,transparent,transparent,transparent,transparent,transparent,yellow,yellow,yellow,yellow,yellow,yellow,yellow,transparent,transparent,transparent,yellow,yellow,yellow,yellow,yellow,yellow,yellow,yellow,yellow,transparent,transparent,yellow,yellow,yellow,yellow,yellow,yellow,yellow,transparent,transparent,transparent,yellow,yellow,yellow,yellow,yellow,yellow,transparent,transparent,transparent,transparent,transparent,yellow,yellow,yellow,yellow,transparent,transparent,transparent,transparent,transparent,transparent,transparent,yellow,yellow,yellow,yellow,yellow,yellow,transparent,transparent,transparent,transparent,transparent,transparent,yellow,yellow,yellow,yellow,yellow,yellow,yellow,transparent,transparent,transparent,transparent,yellow,yellow,yellow,yellow,yellow,yellow,yellow,yellow,yellow,transparent,transparent,transparent,yellow,yellow,yellow,yellow,yellow,yellow,yellow,transparent,transparent,transparent,transparent,transparent,transparent,yellow,yellow,yellow,transparent,transparent,transparent,transparent
  54.  
  55. ;;;;;;;;;;;;;;
  56. ; The code segment - YOUR CODE HERE
  57. ;;;;;;;;;;;;;;
  58.  
  59. segment mycode code
  60.  
  61. ; Subroutines here
  62.  
  63. KeybInt:
  64.         push    ds              ; push the value of ds and ax to safety
  65.         push    ax             
  66.         mov     ax,mydata       ; Re-initialisation of
  67.         mov     ds,ax           ; the data segment
  68.         cli                     ; Disable other interrupts
  69.                                 ; during this one
  70.  
  71. .getstatus:
  72.         in      al, 64h
  73.         test    al, 02h
  74.         loopnz  .getstatus      ; wait until the port is ready
  75.         in      al,60h          ; Get the scan code of the pressed/released key
  76.  
  77.         ; here begins the actual key scanning
  78.         cmp     al, 01h         ; 1 is the 'make code' for ESC
  79.         jne     .cplus          ; if ESC was not pressed, continue
  80.         mov     word [pressesc], 1
  81.         jmp    .kbread
  82. .cplus:
  83.         cmp     al, 04Eh    ; 4E is the 'make code' for keypad plus
  84.         jne     .cminus
  85.         add     word [delay], 1
  86.         jmp     .kbread
  87. .cminus:
  88.         cmp     al, 04Ah    ; 4A is the 'make code' for keypad minus
  89.         jne     .cright
  90.         sub     word [delay], 1
  91.         jmp     .kbread
  92. .cright
  93.         cmp     al, 04Dh
  94.         jne     .cleft
  95.         mov word [move], 6
  96.         jmp     .kbread
  97. .cleft
  98.         cmp     al, 04Bh
  99.         jne     .cup
  100.         mov word [move], -6
  101.         jmp     .kbread
  102. .cup
  103.         cmp     al, 048h
  104.         jne     .cdown
  105.         mov word [move], -1920
  106.         jmp     .kbread
  107. .cdown
  108.         cmp     al, 050h
  109.         jne     .kbread
  110.         mov word [move], 1920
  111. .kbread:
  112.         in      al,61h          ; Send acknowledgment without
  113.         or      al,10000000b    ; modifying the other bits.
  114.         out     61h,al          ;                            
  115.         and     al,01111111b    ;                            
  116.         out     61h,al          ;                            
  117.         mov     al,20h          ; Send End-of-Interrupt signal
  118.         out     20h,al          ;                              
  119.         sti                     ; Enable interrupts again
  120.         pop     ax     
  121.         pop     ds              ; Regain the ds,ax from stack
  122.         iret                    ; Return from interrupt    
  123.        
  124. movePacman:
  125.     mov word ax, [move]
  126.     add word [pacmanloc], ax
  127.     ret
  128.  
  129. copybitmap:
  130.     ;PARAMETERS:
  131.     ;   SI contains the offset address of the bitmap
  132.     ;   DI contains the target coordinate
  133.     ;   ES contains the target segment
  134.     ;   CX contains the bitmap row count
  135.     ;   DX contains the bitmap col count
  136.     push ds
  137.     pusha
  138.     ;Segment registers to correct locations
  139.     mov ax, bitmaps
  140.     mov ds, ax
  141.     .rowloop:
  142.         push cx
  143.         push di
  144.         mov cx, dx
  145.         .colloop:
  146.             mov byte bl, [ds:si]
  147.             cmp byte bl, transparent
  148.             je .skip
  149.             mov byte [es:di], bl
  150.             .skip:
  151.             inc di
  152.             inc si
  153.             loop .colloop
  154.         pop di
  155.         add di, 320
  156.         pop cx
  157.         loop .rowloop
  158.     popa
  159.     pop ds
  160.     ret
  161.    
  162. drawPacman:
  163.     push cx
  164.     push dx
  165.     mov si, 1
  166.     mov di, [pacmanloc]
  167.     mov ax, memscreen
  168.     mov es, ax
  169.     mov dx, 11
  170.     mov cx, 11
  171.     call copybitmap
  172.     pop cx
  173.     pop dx
  174.     ret
  175.    
  176.    
  177.  
  178. initBackground:
  179.     mov ax, background        ; test draw for pixels
  180.     mov es, ax               ; move video memory address to ES
  181.     mov di, 0                ; move the desired offset address to DI
  182. hyppy1:
  183.     mov byte [es:di], blue   ; Move the constant 'blue' (hopefully corresponding to blue colour) to the video memory at offset DI
  184.     inc di                   ; Change the offset address
  185.     cmp di, 320
  186.     jne hyppy1
  187.     mov di, 63680
  188. hyppy2:
  189.     mov byte [es:di], blue   ; Move the constant 'blue' (hopefully corresponding to blue colour) to the video memory at offset DI
  190.     inc di                   ; Change the offset address
  191.     cmp di, 64000
  192.     jne hyppy2 
  193.     mov di, 0
  194. hyppy3:
  195.     mov byte [es:di], blue
  196.     add di, 320
  197.     cmp di, 64000
  198.     jne hyppy3
  199.     mov di, 319
  200. hyppy4:
  201.     mov byte [es:di], blue
  202.     add di, 320
  203.     cmp di, 64319
  204.     jne hyppy4
  205.     ret
  206.    
  207. draw:
  208.     call copybackground
  209.     call drawPacman
  210.     call copymemscreen
  211.     ret
  212.  
  213.  
  214. copybackground:
  215.     push ds
  216.     pusha
  217.     ;Pointers
  218.     mov word si, 0
  219.     mov word di, 0
  220.     mov cx, 64000
  221.  
  222.     ;Segment registers to correct locations
  223.     mov ax, memscreen
  224.     mov es, ax
  225.     mov ax, background
  226.     mov ds, ax
  227.  
  228.     ;REPEAT COPY!
  229.     rep movsb
  230.     popa
  231.     pop ds
  232.     ret
  233.    
  234. copymemscreen:
  235.     push ds
  236.     pusha
  237.     ;Pointers
  238.     mov word si, 0
  239.     mov word di, 0
  240.     mov cx, 64000
  241.  
  242.     ;Segment registers to correct locations
  243.     mov ax, videobase
  244.     mov es, ax
  245.     mov ax, memscreen
  246.     mov ds, ax
  247.  
  248.     ;REPEAT COPY!
  249.     rep movsb
  250.     popa
  251.     pop ds
  252.     ret
  253.    
  254. iscolour:
  255.     push di
  256.     push cx
  257.     push dx
  258.     mov ax, 0
  259.     .rowloop:
  260.         push cx
  261.         push di
  262.         mov cx, dx
  263.         .colloop:
  264.             cmp byte [es:di], bl
  265.             jne .ok
  266.                 inc ax
  267.             .ok:
  268.             inc di
  269.             loop .colloop
  270.         pop di
  271.         add di, 320
  272.         pop cx
  273.         loop .rowloop
  274.     pop dx
  275.     pop cx
  276.     pop di
  277.     ret
  278.    
  279. respectwall:
  280.     mov word di, [move]
  281.     add word di, [pacmanloc]
  282.     mov cx, 11
  283.     mov dx, 11
  284.     mov ax, bitmaps
  285.     mov es, ax
  286.     call iscolour
  287.     ret
  288.    
  289. ..start:
  290.  
  291.     mov     ax, 3509h  ;saving the old interrupt address
  292.     int     21h
  293.     mov     word [oldintseg], es
  294.     mov     word [oldintoff], bx
  295.    
  296.  
  297.     mov     ax, mydata
  298.     mov     ds, ax
  299.     mov     ax, mystack
  300.     mov     ss, ax
  301.     mov     sp, stacktop
  302.  
  303.    
  304.     push    ds
  305.     mov     ax, mycode  ;setting the new interrupt
  306.     mov     ds, ax
  307.     mov     dx, KeybInt
  308.     mov     ax, 2509h
  309.     int     21h
  310.     pop     ds
  311.    
  312.     mov     ah, 0fh   ; fetching the old graphics mode and storing it to a variable
  313.     int     10h
  314.     mov     word [oldgramod], ax
  315.  
  316.     mov     ax, 0013h  ; setting the new graphics mode
  317.     int     10h
  318.    
  319.     call initBackground
  320. .mainloop:
  321.     mov word dx, [delay]  ; delay for game speed
  322. .pause1:
  323.     mov cx, 65535
  324. .pause2:
  325.     dec cx
  326.     jne .pause2
  327.     dec dx
  328.     jne .pause1
  329.    
  330.     call draw
  331.     call respectwall ;pacman should recognize the wall...
  332.     cmp ax, 0
  333.     jne .jumppi
  334.     add word [toofast], 1  ;if pacman moves too fast
  335.     cmp word [toofast], 2
  336.     jne .jumppi
  337.     call movePacman
  338.     mov word [toofast], 0
  339. .jumppi:
  340.     cmp     word [pressesc], 1; check if pressed esc
  341.     jne     .mainloop
  342.        
  343. .dosexit:
  344.  
  345.     mov     word ax, [oldgramod]  ; setting the old graphics mode back
  346.     mov     ah, 00h
  347.     int     10h
  348.    
  349.     push    ds
  350.     mov     word dx, [oldintoff] ; Move VALUE at address oldintoff to DX (not the address!)  restoring the old interrupt here!!!
  351.     mov     word ax, [oldintseg]; Move VALUE at address oldintseg to DS, step 1 (remember you cannot move directly to segment registers)
  352.     mov     ds, ax; Move VALUE at address oldintseg to DS, step 2
  353.     mov     ax, 2509h ; Move parameters to AH and AL (or both together by moving to AX). Interrupt number is still 9 and AH should be 25h (note hexadecimal)
  354.     int     21h ; Finally, set the interrupt vector by calling int 21H
  355.     pop     ds
  356.    
  357.     mov al, 0    ; ending the program with INT 21,4c
  358.     mov     ah, 4ch
  359.     int     21h
  360.  
  361. .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement