home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR13 / DOS32V12.ZIP / STDIO.ASM < prev    next >
Assembly Source File  |  1993-10-12  |  11KB  |  422 lines

  1. comment $
  2.  
  3.  
  4.  
  5.      FOR 32 BIT PROTECDED MODE ONLY
  6.  Writen by Adam Seychell
  7.  
  8. ▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐
  9. ▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐  VARIOUS PROCEDURES   ▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐
  10. ▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐
  11. ▐                                                                     ▐
  12. ▐      THIS FILE CONTAINS USEFUL PROCEDURES                           ▐
  13. ▐                                                                     ▐
  14. ▐      THEY SHOULDN'T NEED TO BE CHANGED TO MUSH                      ▐
  15. ▐                                                                     ▐
  16. ▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐
  17.  
  18.  
  19.  
  20.  
  21.  
  22. ▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐
  23. ▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐  VARIOUS PROCEDURES   ▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐
  24. ▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐
  25. ╒═════════════╤═══════════════════════════
  26. │  proc Name  │          Discription
  27. ╞═════════════╪═══════════════════════════
  28. │ ClrScr      │    fills video memory (B8000h-B8FFFh) with  AX
  29. │             │
  30. ├─────────────┼───────────────────────────
  31. │PRINT_CHAR   │ prints a character AL with color AH at the cursor
  32. │             │                AH = attribute byte  ( COLOR )
  33. │             │
  34. ├─────────────┼───────────────────────────
  35. │ TEXT        │  Prints a string at the cursor position.
  36. │             │ expects  :     EDX = offset of string in data segment
  37. │             │                AH = attribute byte  ( COLOR )
  38. │             │
  39. ├─────────────┼───────────────────────────
  40. │HEX_DWORD    │ Prints the value of EDX, DX or DL as hexidecimal
  41. │HEX_WORD     │  at the cursor position.
  42. │HEX_BYTE     │                 expects  :   EDX = value to display
  43. │             │                        AH = attribute byte  ( COLOR )
  44. │             │
  45. ├─────────────┼───────────────────────────
  46. │BINARY_DWORD │ Same as hex above except it prints in binary
  47. │BINARY_WORD  │
  48. │BINARY_BYTE  │
  49. ├─────────────┼───────────────────────────
  50. │DEC_DWORD    │ Same as hex above except it prints in decimal
  51. │DEC_WORD     │
  52. │DEC_BYTE     │
  53. ├─────────────┼───────────────────────────
  54. │SCROLL_SCREEN│Scrolls the screen up a line.
  55. │             │
  56. ├─────────────┼───────────────────────────
  57. │SET_TIMER    │Sets the timer 0 register of the 8242 PIC
  58. │             │ to the value in AX
  59. ├─────────────┼───────────────────────────
  60. │RANDOM       │returns EAX = a random number CL bits in size
  61. │             │
  62. ├─────────────┼───────────────────────────
  63. │SQRT         │Returns EAX with the square root of EAX.
  64. │             │
  65. └─────────────┴───────────────────────────
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. $
  75.  
  76.  
  77. ;       Various    varibles used by the procedures
  78. random_init        dd    1010111000110111101010011011b
  79. clicker                db 0
  80. HexChar            db '0123456789ABCDEF'
  81.  
  82. ; Contains the current cursor cordinates  used by the printing procs
  83. Cursor_X    db    0,0; Contain the current cursor cordinates
  84. Cursor_Y    db    0,0; used by the printing procs ( see file PROCS.INC )
  85.  
  86. dec_diver dd 1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000
  87. DecVal23    dd 0
  88. tmpdec_color07    db 0
  89.  
  90.  
  91.  
  92.  
  93. public Click
  94. public ClrScr
  95. public Scroll_Screen
  96. public Set_timer
  97. public TEXT
  98. public binary_Dword
  99. public binary_Word
  100. public binary_Byte
  101. public Hex_Dword
  102. public Hex_Word
  103. public Hex_Byte
  104. public DEC_Dword
  105. public DEC_Word
  106. public DEC_Byte
  107. public PRINT_CHAR
  108. public SQRT
  109. public Random
  110.  
  111. ;════════════════ Click speaker ═════════════════
  112. Click proc near
  113.        xor clicker,010b
  114.        mov al,clicker
  115.        out 61h,al
  116.        ret
  117. click endp
  118.  
  119. ; ══════════════ CLEAR THE SCREEN ( FILL WITH ATTRIBUTES AH ) ════════════
  120. ClrScr    proc near
  121.     PUSH edi ecx eax  es
  122.         push fs
  123.         pop  es
  124.         mov edi,0b8000h
  125.         cld
  126.         xor al,al
  127.         mov ecx,2000
  128.         rep stosw
  129.         POP es eax ecx edi
  130.         ret
  131. ClrScr endp
  132.  
  133.  
  134. ;═════════════════  SCROLL THE SCREEN UP A LINE  ══════════════════════
  135. Scroll_Screen proc near
  136.     PUSH esi edi ecx eax es
  137.         push fs
  138.         pop es
  139.         mov edi,0b8000h
  140.         mov esi,0B8000h+0a0h
  141.         cld
  142.         mov ecx,1000-40
  143.         rep movs dword ptr  es:[edi],es:[esi]
  144.         ; now screen is scrolled up, must clear bottom line
  145.         mov ecx,80
  146. cxtln:  mov byte ptr es:[0b8000h+(80*24*2)-2+ecx*2],0
  147.         loop cxtln
  148.         POP es eax ecx edi  esi
  149.     ret
  150. Scroll_Screen endp
  151.  
  152.  
  153. Set_timer  proc near
  154. ;═══════ SET THE PIT (8253) TIMER NUMBER 0 TO THE VALUE IN AX REGISTER ═════
  155.     shl eax,8
  156.     mov al,00110100b
  157.     out 43h,al
  158.         shr eax,8
  159.     out 40h,al
  160.         mov al,ah
  161.     out 40h,al
  162.         ret
  163. Set_timer endp
  164.  
  165.  
  166.  
  167.  
  168.  
  169. ;═════════════════════════PRINT TEXT═══════════════════════════════════
  170. ; PRINTS A STRING ON SCREEN ( TEXT MODE 3 )
  171. ; Expects  edx             > offset of string in the data segment
  172. ;       Cursor_X,Cursor_Y     >  position (X,Y) on the screen
  173. ;       ah                  > attribute byte ( colors )
  174. ;NOTE:  string ends with a '$'
  175. ;      Cursor_X is returned with the X position at the end of the string
  176. TEXT proc near
  177.            push eax edx
  178. textlop:    mov al,[edx]
  179.                 inc edx
  180.             cmp al,'$'
  181.             jz exit_text
  182.             call print_char
  183.                jmp textlop
  184. exit_text:     pop edx eax
  185.         ret
  186. TEXT ENDP
  187.  
  188. ;════════════════════ PRINT BINARY NUMBER OF THE REGISTER EDX═══════════════
  189. ; Similar to the TEXT proc near above
  190. ; Expects :
  191. ; ah = color
  192. ; Cursor_X,Cursor_Y   position (X,Y) on the screen
  193. ;
  194. binary_Dword    proc near            ; prints EDX
  195.         push ecx
  196.     mov ecx,32
  197.     jmp binSizeSet
  198. binary_Word    proc near            ; prints DX
  199.         push ecx
  200.     mov ecx,16
  201.         rol edx,16
  202.     jmp binSizeSet
  203. binary_Byte    proc near            ; prints DL
  204.                 push ecx
  205.         mov ecx,8
  206.             rol edx,24
  207. BinSizeSet:    push eax
  208.     binary_digit:
  209.                 mov al,'0'
  210.                 rol edx,1
  211.                 jnc bin_p_0
  212.                mov al,'1'
  213.           bin_p_0: call print_char    ;prints a char al : color = ah
  214.             loop binary_digit
  215.                 pop eax ecx
  216.             ret
  217. Binary_Byte endp
  218. Binary_Word endp
  219. Binary_DWord endp
  220.  
  221. ;═══════════════PRINT HEXIDECIMAL NUMBER OF THE REGISTER EDX═══════════════
  222. ; Expects :
  223. ; ah = color
  224. ; Cursor_X,Cursor_Y   position (X,Y) on the screen
  225. ;
  226. Hex_Dword    proc near            ; prints EDX
  227.         push ecx
  228.     mov ecx,8
  229.     jmp strt_hex
  230. Hex_Word    proc near            ; prints DX
  231.         push ecx
  232.     mov ecx,4
  233.         rol edx,16
  234.     jmp strt_hex
  235. Hex_Byte    proc near            ; prints DL
  236.         push ecx
  237.         rol edx,24
  238.     mov ecx,2
  239. strt_hex:
  240.         push esi
  241. HexPrnt:        rol edx,4
  242.                 mov esi,edx
  243.                 and esi,0fh
  244.                 mov al,HexChar+esi
  245.                 call print_char
  246.               Loop HexPrnt
  247.                 pop esi ecx
  248.             ret
  249. endp
  250. endp
  251. endp
  252.  
  253.  
  254. ;═══════════════ PRINT DECIMAL NUMBER OF THE REGISTER EDX ═══════════════
  255. ; Expects :
  256. ; ah = color
  257. ; Cursor_X,Cursor_Y   position (X,Y) on the screen
  258. ;
  259.  
  260. DEC_Dword    proc near            ; prints EDX
  261.         push ecx esi edx
  262.     mov ESI,9*4
  263.     jmp strt_dec
  264. DEC_Word    proc near            ; prints DX
  265.         push ecx esi edx
  266.     mov ESI,4*4
  267.     and edx,0FFFFh
  268.     jmp strt_dec
  269. DEC_Byte    proc near            ; prints DL
  270.         push ecx esi edx
  271.            mov ESI,2*4
  272.     and edx,0FFh
  273.  
  274.  
  275. strt_dec:
  276.     push EAX
  277.         mov tmpdec_color07,ah    ; save color
  278.         mov decVal23,EDX
  279. dec83:  mov ECX, Dec_diver + ESI
  280.  
  281.         mov eax,decVal23
  282.         xor edx,edx
  283.         div ECX
  284.         mov DecVal23,EDX    ; use remainder
  285.                     ; use quot as digit number
  286.         add al,'0'    ; print digit
  287.             mov ah,tmpdec_color07
  288.             call print_char
  289.         sub ESI,4
  290.         jge dec83
  291.  
  292.     pop EAX EDX ESI ECX
  293.            ret
  294. endp
  295. endp
  296. endp
  297.  
  298.  
  299.  
  300.  
  301.  
  302. ;════════════════════ PRINT A CHARACTER ═════════════════════════
  303. ; EXPECTS:
  304. ; al     character to print
  305. ; ah      attribute byte
  306. ; Cursor_X,Cursor_Y   position (X,Y) on the screen
  307. PRINT_CHAR    proc near
  308.                 ;-- line feed ----------------------------------
  309.                 cmp al,0Ah
  310.                 jnz no_feed
  311.                   inc Cursor_Y       ; move curser down a line
  312.             jmp char_printed
  313. no_feed:    ;-- carage return ------------------------------------
  314.                 cmp al,0Dh
  315.                 jnz no_carg
  316.                   mov Cursor_X,0        ; return curser to start
  317.             jmp char_printed
  318.    no_carg:    ;-- print the character on the screen -----------------
  319.                push eax ecx
  320.                 mov ecx,eax
  321.                 xor eax,eax
  322.                 mov eax,80
  323.                 mul Cursor_Y
  324.                 add ax,word ptr Cursor_X
  325.                 mov fs:[0b8000h+2*eax],CX
  326.                 inc Cursor_X
  327.         pop ecx eax
  328.  
  329.         ; ----- don't let the curser go off the screen -----------
  330. char_printed:      cmp Cursor_X,80
  331.              jb k23
  332.               mov Cursor_X,0
  333.                   inc Cursor_Y
  334.     k23:      cmp Cursor_Y,25
  335.              jb k24
  336.                   call Scroll_Screen
  337.               mov Cursor_Y,24
  338.     k24:      ret
  339. endp
  340.  
  341.  
  342.  
  343.  
  344.  
  345. SQRT    proc near
  346. ;═══════════════════════════════════════════════════════════════════════════
  347. ; returns EAX with the SQUARE ROOT of EAX.
  348. ;
  349. ; Uses newtons method:    Xn+1 = Xn -  f(Xn) / f'(Xn)
  350. ;    f'(x) is the derivitive of f(x)
  351. ;
  352. ; For square routes:  f(x) = x*x - ( Root value  i.e EAX)
  353. ;  and        f'(x) = 2x
  354. ;
  355. ;═══════════════════════════════════════════════════════════════════════════
  356. ;
  357.         mov edi,eax
  358. ;----- Get Estimated Root Value ( ebp = rough value of the sqrt of EAX ) ---
  359.             bsr ecx,edi    ; Get bit number of highest NZ bit
  360.             shr cl,1    ; Half this highest bit number
  361.         mov ebp,edi     ; Rotate Root Value by
  362.         shr ebp,cl    ;  the bit number to get a estimate root.
  363.  
  364.  
  365. ;----- Main Loop that makes EBP converge to the square root of EDI ------
  366.  calc_sqrt_Loop:
  367.         push EBP        ; save x  so can check if x has converged
  368.         mov  EBX,EBP
  369.         shl  EBX,1        ; ebx = f'(x) = 2x
  370.  
  371.         mov  EAX,EBP
  372.         mul  EBP
  373.         sub  EAX,EDI        ; eax = f(x) = X*X - Root_value
  374.         sbb  EDX,0        ; use 64bit subtract
  375.  
  376.         div  EBX        ; eax = f(Xn) / f'(Xn)
  377.  
  378.     sub  EBP,EAX        ; ebp =   Xn+1   =   Xn - f(Xn) / f'(Xn)
  379.         pop  EAX        ; look at old value
  380.         cmp  EBP,EAX        ; loop only if new value is different
  381.       jnz calc_sqrt_Loop
  382. ;-------------------------- End of Loop --------------------------------
  383.  
  384.         mov eax,ebp    ; EBP contians the square root of EDI
  385.     ret
  386. ;────────────────────────────────────────────────────────────────────────────
  387. endp
  388.  
  389.  
  390.  
  391. Random proc near
  392. ;═══════════════════════════════════════════════════════════════════════════
  393. ;        EAX =  a random number CL bits in size
  394. ;
  395. ;═══════════════════════════════════════════════════════════════════════════
  396.         xor eax,eax
  397.         mov bl,byte ptr random_init
  398.         and bl,1
  399.  
  400. Gen_bit:    ; make n bit numbers
  401.         shl eax,1
  402.  
  403.         mov edx,random_init
  404.  
  405.         shr edx,9
  406.         xor bl,dl
  407.  
  408.         shr edx,5
  409.         xor bl,dl
  410.  
  411.         bt ebx,1
  412.         rcr random_init,1
  413.         setc bl
  414.     or  al,bl
  415.  
  416.     dec cl
  417.         jnz Gen_bit
  418.     ret
  419. ;────────────────────────────────────────────────────────────────────────────
  420. endp
  421.  
  422.