home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / PCGPEV10.ZIP / ASM2.TXT < prev    next >
Text File  |  1994-05-10  |  7KB  |  246 lines

  1. ─────────────────────────────────────────────────────────────────────────────
  2.     ASM2.TXT - intro to keyboard and flow control
  3. ─────────────────────────────────────────────────────────────────────────────
  4.     
  5.     Alright.  This bit of code introduces control flow, keyboard input, and 
  6.     a way to easily print out one character.
  7.  
  8. ────────
  9.     First off, lets examine the easiest one: printing a character.
  10.  
  11.     It's like this: you put the character to print in DL, put 2 in AH and
  12.     call interrupt 21h.  Damn easy.
  13.  
  14. ────────
  15.     Ok, lets look at the next easiest one: keyboard input.
  16.  
  17.     There are quite a few functions related to INT 16h (the keyboard
  18.     interrupt.)  They are:
  19.  
  20. FUNCTION#
  21. ---------
  22.  
  23.     0h  -Gets a key from the keyboard buffer.  If there isn't one, it waits
  24.             until there is.
  25.             Returns the SCAN code in ah, and the ASCII translation in AL
  26.  
  27.     1h  -Checks to see if a key is ready to grab.  Sets the zero flag if a
  28.             key is ready to grab.  Grab it with Fn# 0
  29.             This also returns the same info about the key as Fn#0, but does
  30.             not remove it from the buffer.
  31.  
  32.     2h  -Returns the shift flags in al.  They are:
  33.             bit 7 - Insert active
  34.             bit 6 - Caps lock active
  35.             bit 5 - Num Lock active
  36.             bit 4 - Scroll lock active
  37.             bit 3 - Alt pressed
  38.             bit 2 - Ctrl pressed
  39.             bit 1 - Left shift pressed
  40.             bit 0 - right shift pressed
  41.  
  42.     3h  -You can set the Typematic Rate and delay with this function
  43.             registers must be set as follows
  44.             AL = 5
  45.             BH = Delay value (0-3: 250,500,750,1000 millisec)
  46.             BL = Typematic rate (0-1fh) 1fh = slowest (2 chars per sec)
  47.                 0 =fastest (30 chars per second)
  48.  
  49.     4h  -Key Click control - not important
  50.  
  51.     5h  -STUFF the keyboard
  52.         input:
  53.             CH = scan code
  54.             CL = ascii code
  55.  
  56.         output:
  57.             al = 0 no error
  58.             al = 1 keyboard buffer is full
  59.  
  60.    10h  -Same as #0, but its for the extended keyboard.  Checks all the keys.
  61.  
  62.    11h  -Same as #1, but for the extended keyboard.
  63.  
  64.    12h  -Same as #2, but AH contains additional shift flags:
  65.             bit 7 - Sys req pressed
  66.             bit 6 - Caps lock active
  67.             bit 5 - Num Lock active
  68.             bit 4 - Scroll lock active
  69.             bit 3 - Right Alt active
  70.             bit 2 - Right Ctrl active
  71.             bit 1 - Left Alt active
  72.             bit 0 - Right Alt active
  73.         Al is EXACTLY the same as in Fn#2
  74.  
  75.  
  76. WHERE AH= the function number when you call INT 16h
  77.  
  78. ────────
  79.     That's neat-o, eh?  Now on to flow controll via CMP and Jcc...
  80.  
  81. CMP:
  82. ───
  83.     CMP is the same as SUB, but it does NOT alter any registers, only the
  84.     flags.  This is used in conjunction with Jcc.
  85.  
  86. Jcc:
  87. ───
  88.     Ok, Jcc is not a real instruction, it means 'jump if conditionis met.'
  89.  
  90.     I'll break this into 3 sections, comparing signed numbers, comparing 
  91.     unsigned numbers, and misc.
  92.  
  93.     Note that a number being 'unsigned' or 'signed' only depends on how you
  94.     treat it.  That's why there are different Jcc for each...
  95.  
  96.     If you treat it as a signed number, the highest bit denotes whether it's
  97.     negative or not.  
  98.     
  99.     Prove to yourself that 0FFFFh is actually -1 by adding 1 to 0FFFFh.  You 
  100.     should get a big zero: 00000h.  (Remember that the number is ONLY 16 bits
  101.     and the carry dissapears..)
  102.  
  103. UNSIGNED:
  104. ────────
  105.     JA  -jumps if the first number was above the second number
  106.     JAE -same as above, but will also jump if they are equal
  107.  
  108.     JB  -jumps if the first number was below the second
  109.     JBE -duh...
  110.  
  111.     JNA -jumps if the first number was NOT above... (same as JBE)
  112.     JNAE-jumps if the first number was NOT above or the same as..
  113.             (same as JB)
  114.     JNB -jumps if the first number was NOT below... (same as JAE)
  115.     JNBE-jumps if the first number was NOT below or the same as..
  116.             (same as JA)
  117.     JZ  -jumps if the two numbers were equal (zero flag = 1)
  118.     JE  -same as JZ, just a different name
  119.  
  120.     JNZ -pretty obvious, I hope...
  121.     JNE -same as above...
  122.  
  123. SIGNED:
  124. ──────
  125.     JG  -jumps if the first number was > the second number
  126.     JGE -same as above, but will also jump if they are equal
  127.  
  128.     JL  -jumps if the first number was < the second
  129.     JLE -duh...
  130.  
  131.     JNG -jumps if the first number was NOT >... (same as JLE)
  132.     JNGE-jumps if the first number was NOT >=.. (same as JL)
  133.  
  134.     JNL -jumps if the first number was NOT <... (same as JGE)
  135.     JNLE-jumps if the first number was NOT <=... (same as JG)
  136.  
  137.     JZ, JE, JNZ, JNE - Same as for Unsigned
  138.  
  139. MISC:
  140. ────
  141.     JC  -jumps if the carry flag is set
  142.     JNC -Go figgure...
  143.  
  144.     Here's the rest of them... I've never had to use these, though...
  145.  
  146.     JO  -jump if overflow flag is set
  147.     JNO -...
  148.  
  149.     JP  -jump is parity flag is set
  150.     JNP -...
  151.     JPE -jump if parity even (same as JP)
  152.     JPO -jump if parity odd (same as JNP)
  153.  
  154.     JS  -jumps if sign flag is set
  155.     JNS -...
  156.  
  157.  
  158. Here's the flags really quickly:
  159. ────────────────────────────────
  160. bit#    8 7 6 5 4 3 2 1 0
  161.         ─────────────────
  162. symbol: O D I T S Z A P C
  163.  
  164.     O = OverFlow flag
  165.     D = Direction flag  *
  166.     I = Interrupt flag
  167.     T = Trap flag
  168.     S = Sign flag
  169.     Z = Zero flag       *
  170.     A = Auxiliary flag
  171.     C = Carry flag      *
  172.  
  173. The * denotes the ones that you should know.
  174.  
  175. ─────────────────────────────────────────────────────────────────────────────
  176.  
  177. That's it for now...  Until next time...
  178.  
  179. Draeden\VLA
  180.  
  181.  
  182. ┌──────────┬───────────────────────────────────────────────────────────────
  183. │ ASM2.ASM │
  184. └──────────┘
  185.  
  186. ;   ASM2.ASM
  187. ; Prints messages get keyboard input and has control flow
  188.  
  189.     DOSSEG
  190.     .MODEL SMALL
  191.     .STACK 200h
  192.     .DATA
  193.  
  194. Prompt      db  13,10,"Do you want to be prompted again? (Y/N) $"
  195. NoMessage   db  13,10,"Ok, then I won't prompt you anymore.$"
  196. YesMessage  db  13,10,"Here comes another prompt!$"
  197. UnKnownKey  db  13,10,"Please hit either Y or N.$"
  198.  
  199.     .CODE
  200.  
  201. START:
  202.     mov     ax,@DATA    ;moves the segment of data into ax
  203.     mov     ds,ax
  204.  
  205. MainLoop:
  206.     mov     ah,9
  207.     mov     dx,offset Prompt
  208.     int     21h         ;print a message
  209.  
  210.     mov     ah,0
  211.     int     16h         ;get a key, returned in AX
  212.                         ;AL is the ASCII part
  213.                         ;AH is the SCAN CODE
  214.     push    ax
  215.     mov     dl,al
  216.     mov     ah,2
  217.     int     21h         ;print character in dl
  218.     pop     ax
  219.  
  220.     cmp     al,"Y"      ;was the character a 'Y'?
  221.     jne     NotYes      ;nope it was Not Equal
  222.  
  223.     mov     ah,9
  224.     mov     dx,offset YesMessage
  225.     int     21h
  226.     jmp     MainLoop
  227.  
  228. NotYes:
  229.     cmp     al,"N"      ;was the character a 'N'
  230.     je      ByeBye      ;Yes, it was Equal
  231.  
  232.     mov     dx,offset UnknownKey
  233.     mov     ah,9
  234.     int     21h
  235.     jmp     MainLoop
  236.  
  237. ByeBye:
  238.     mov     dx,offset NoMessage
  239.     mov     ah,9
  240.     int     21h
  241.  
  242.     mov     ax,4c00h    ;Returns control to DOS
  243.     int     21h         ;MUST be here! Program will crash without it!
  244.  
  245. END START
  246.