home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / msvp98b1.lzh / MSNUT1.ORG < prev    next >
Text File  |  1993-05-14  |  12KB  |  585 lines

  1.        NAME    MSNUT1
  2. ; File MSNUT1.ASM
  3. ; Provides various Intel 8088 level operations, including
  4. ; display facilities (via msg buffer or DOS) for char, strings, etc.
  5. ;
  6. ; Copyright 1991, University of Waterloo.
  7. ;  Copyright (C) 1985, 1992, Trustees of Columbia University in the 
  8. ;  City of New York.  Permission is granted to any individual or institution
  9. ;  to use this software as long as it is not sold for profit.  This copyright
  10. ;  notice must be retained.  This software may not be included in commercial
  11. ;  products without written permission of Columbia University.
  12. ; Written by Erick Engelke of the University of Waterloo, Waterloo, 
  13. ;  Ontario, Canada,
  14. ;  and by Joe R. Doupnik, Utah State University, 
  15. ;  jrd@cc.usu.edu, jrd@usu.Bitnet.
  16. ;
  17. ; Edit history
  18. ; 6 Sept 1991 v3.11
  19. ; Last Edit
  20. ; 2 Feb 1992
  21.  
  22. cr    equ    0dh
  23. lf    equ    0ah
  24. conout    equ    2
  25. dos    equ    21h
  26.  
  27. BIOSCLK    equ    046ch
  28. HI_MAX    equ    018h
  29. LO_MAX    equ     0b0h
  30.  
  31. _TEXT    SEGMENT  WORD PUBLIC 'CODE'
  32. _TEXT    ENDS
  33. _DATA    SEGMENT  WORD PUBLIC 'DATA'
  34. _DATA    ENDS
  35. CONST    SEGMENT  WORD PUBLIC 'CONST'
  36. CONST    ENDS
  37. _BSS    SEGMENT  WORD PUBLIC 'BSS'
  38. _BSS    ENDS
  39. DGROUP    GROUP    CONST, _BSS, _DATA
  40.     ASSUME  CS: _TEXT, DS: DGROUP, SS: DGROUP
  41.  
  42. _DATA    SEGMENT
  43.     extrn    _doslevel:word, _msgcnt:word, _msgbuf:byte,_display_mode:byte
  44. _DATA    ENDS
  45.  
  46. _TEXT    segment
  47.  
  48.         ; Compute 1's-complement sum of data buffer
  49.         ; unsigned checksum( unsigned word *buf, unsigned cnt)
  50.  
  51.     public    _checksum
  52. _checksum proc    near
  53.     push    bp
  54.     mov    bp,sp
  55.     push    si
  56.     push    bx
  57.     push    cx
  58.     push    dx
  59.         mov     si,[bp+4+0]        ; offset of data to be checked
  60.     mov     cx,[bp+4+2]        ; cx = cnt
  61.     mov    bl,cl
  62.     shr     cx,1            ; group into words
  63.     xor    dx,dx            ; set checksum to 0
  64.         cld
  65.     jcxz    chk3
  66.         clc
  67. chk1:    lodsw
  68.     adc    dx,ax
  69.     loop    chk1
  70.                     ; resolve remainder
  71. chk2:    adc     dx,0
  72.         jc    chk2
  73. chk3:    and    bl,1
  74.     jz    chk5
  75.         xor     ah,ah
  76.     lodsb
  77.         clc
  78.         add     dx,ax
  79. chk4:    adc     dx,0
  80.     jc    chk4
  81. chk5:    mov    ax,dx            ; result into ax
  82.         or      ax,ax
  83.     jnz    chk6
  84.         mov     ax,0ffffh
  85. chk6:    pop    dx
  86.     pop    cx
  87.     pop    bx
  88.     pop    si
  89.     pop    bp
  90.     ret
  91. _checksum endp
  92.  
  93. ;  ntohl(longword x)    32 bit network (big endian) to host (little endian)
  94. ;  htonl(longword x)    32 bit host (little endian) to network (big endian)
  95. ;  intel(longword x)
  96. ;  Reverse order of 4 byte groups between big and little endian forms
  97. ;
  98.     public    _intel, _ntohl, _htonl
  99.  
  100. _intel    proc    near
  101. _ntohl    equ    this byte
  102. _htonl    equ    this byte
  103.     push    bp
  104.     mov    bp,sp
  105.     mov    ax,[bp+4+2]
  106.     mov    dx,[bp+4+0]
  107.     xchg    al,ah
  108.     xchg    dl,dh
  109.     pop    bp
  110.     ret
  111. _intel    endp
  112.  
  113. ;  ntohs(word x)    16 bit network (big endian) to host (little endian)
  114. ;  htons(word x)    16 bit host (little endian) to network (big endian)
  115. ;  intel16(word x)
  116. ;  Reverse order of 2 byte groups between big and little endian forms
  117.     public    _intel16, _ntohs, _htons
  118. _intel16 proc    near
  119. _ntohs    equ    this byte
  120. _htons    equ    this byte
  121.     push    bp
  122.     mov    bp,sp
  123.     mov    ax,[bp+4+0]
  124.     xchg    al,ah
  125.     pop    bp
  126.     ret
  127. _intel16 endp
  128.  
  129.  
  130. ; int ourmod(int top, int bottom)
  131. ; Perform modulo function on 16 bit quantities
  132.     public    _ourmod
  133. _ourmod    proc    near
  134.     push    bp
  135.     mov    bp,sp
  136.     push    bx
  137.     mov    ax,[bp+4+0]        ; top number
  138.     mov    bx,[bp+4+2]        ; bottom number (radix)
  139.     xor    dx,dx
  140.     or    bx,bx            ; bottom is zero?
  141.     jz    ourmod1            ; z = yes, return zero
  142.     div    bx
  143. ourmod1:mov    ax,dx            ; return remainder
  144.     pop    bx
  145.     pop    bp
  146.     ret
  147. _ourmod    endp
  148.  
  149. ; int ourdiv(int top, int bottom)
  150. ; Perform 16 bit integer division
  151.     public    _ourdiv
  152. _ourdiv    proc    near
  153.     push    bp
  154.     mov    bp,sp
  155.     push    bx
  156.     mov    ax,[bp+4+0]        ; top
  157.     mov    bx,[bp+4+2]        ; bottom
  158.     xor    dx,dx
  159.     or    bx,bx            ; divide by zero?
  160.     jz    outdiv1            ; z = yes, divide by one
  161.     div    bx
  162. outdiv1:pop    bx
  163.     pop    bp            ; quotient is returned in ax
  164.     ret
  165. _ourdiv    endp
  166.  
  167. ; int ourlmod(long top, int bottom)
  168. ; Perform 32 bit integer modulo function
  169.     public    _ourlmod
  170. _ourlmod proc    near
  171.     push    bp
  172.     mov    bp,sp
  173.     push    bx
  174.     mov    ax,[bp+4+0]        ; top lower 16 bits
  175.     mov    dx,[bp+4+2]        ; top upper 16 bits
  176.     mov    bx,[bp+4+4]        ; bottom
  177.     or    bx,bx            ; zero?
  178.     jz    outlmo1            ; z = yes divide by 2^16 and quit
  179.     div    bx
  180. outlmo1:mov    ax,dx            ; return remainder in ax
  181.     pop    bx
  182.     pop    bp
  183.     ret
  184. _ourlmod endp
  185.  
  186. ; int ourldiv(long top, int bottom)
  187. ; Perform 32 bit integer division of 32 bit quotient by 16 bit divisor
  188.     public    _ourldiv
  189. _ourldiv proc    near
  190.     push    bp
  191.     mov    bp,sp
  192.     push    bx
  193.     mov    ax,[bp+4+0]        ; top lower 16 bits
  194.     mov    dx,[bp+4+2]        ; top upper 16 bits
  195.     mov    bx,[bp+4+4]        ; bottom
  196.     cmp    dx,bx            ; about to overflow?
  197.     jae    ourldiv1        ; ae = yes, return 0xffffh
  198.     div    bx
  199.     xor    dx,dx            ; clear remainder (high order ret)
  200.     pop    bx
  201.     pop    bp            ; quotient is returned in dx:ax
  202.     ret
  203. ourldiv1:mov    ax,0ffffh        ; overflow indication
  204.     xor    dx,dx            ; clear high order
  205.     pop    bx
  206.     pop    bp
  207.     ret
  208. _ourldiv endp
  209.  
  210. ; long ourlmul(long top, int bottom)
  211. ; Perform 32 bit integer multiplication of 32 bit multiplicand by 16 bit mult
  212.     public    _ourlmul
  213. _ourlmul proc    near
  214.     push    bp
  215.     mov    bp,sp
  216.     push    bx
  217.     push    cx
  218.     mov    ax,[bp+4+2]        ; top upper 16 bits
  219.     mov    bx,[bp+4+4]        ; bottom
  220.     mul    bx
  221.     mov    cx,ax            ; save product (no overflow noted)
  222.     mov    ax,[bp+4+0]        ; top lower 16 bits
  223.     mul    bx
  224.     adc    dx,cx            ; new upper 16 bits
  225.     pop    cx
  226.     pop    bx
  227.     mov    sp,bp
  228.     pop    bp            ; long product is returned in dx:ax
  229.     ret
  230. _ourlmul endp
  231.  
  232. ; void * bcopy(src, dest, count)
  233. ; void *dest, *src;
  234. ; size_t count;
  235. ; copy count bytes from src to dest
  236.     public    _bcopy
  237. _bcopy proc    near
  238.     push    bp
  239.     mov    bp,sp
  240.     push    es
  241.     push    si
  242.     push    di
  243.     push    cx
  244.     mov    ax,ds            ; set to same data segment
  245.     mov    es,ax
  246.     mov    si,[bp+4+0]        ; offset of source
  247.     mov    di,[bp+4+2]        ; offset of destination
  248.     mov    cx,[bp+4+4]        ; count
  249.     cld
  250.     push    di            ; push dest address for return
  251.     jcxz    bcopy2            ; z = nothing to copy
  252.     or    di,di            ; is destination NULL?
  253.     jz    bcopy2            ; z = yes, don't do a thing
  254.     cmp    si,di            ; is source after destination?
  255.     ja    bcopy1            ; a = yes, no overlap problem
  256.     je    bcopy2            ; e = same place, do nothing
  257.     add    di,cx            ; start at the ends
  258.     dec    di
  259.     add    si,cx
  260.     dec    si
  261.     std                ; work backward
  262. bcopy1:    rep    movsb
  263. bcopy2:    pop    ax            ; recover return destination
  264.     cld
  265.     pop    cx
  266.     pop    di
  267.     pop    si
  268.     pop    es
  269.     mov    sp,bp
  270.     pop    bp
  271.     ret
  272. _bcopy endp
  273.  
  274. ; void * memset(dest, c, count)
  275. ; void *dest;
  276. ; char c;
  277. ; size_t count;
  278. ; Store count copies of byte c in destination area dest
  279.     public    _memset
  280. _memset    proc    near
  281.     push    bp
  282.     mov    bp,sp
  283.     push    es
  284.     push    di
  285.     push    cx
  286.     mov    ax,ds            ; setup data segment
  287.     mov    es,ax
  288.     mov    di,[bp+4+0]        ; offset of destination
  289.     or    di,di            ; is it NULL?
  290.     jz    memset1            ; z = yes, don't do a thing
  291.     push    di            ; save dest for return
  292.     mov    al,[bp+4+2]        ; byte of character c
  293.     mov    ah,al
  294.     mov    cx,[bp+4+4]        ; count
  295.     jcxz    memset1            ; z = do nothing
  296.     cld
  297.     shr    cx,1
  298.     jnc    memset2
  299.     stosb
  300. memset2:rep    stosw
  301.     pop    ax            ; return pointer to destination
  302. memset1:pop    cx
  303.     pop    di
  304.     pop    es
  305.     mov    sp,bp
  306.     pop    bp
  307.     ret
  308. _memset    endp
  309.  
  310. ;  Timer routines - use set_*timeout to receive a clock value which
  311. ;                   can later be tested by calling chk_timeout with
  312. ;                   that clock value to determine if a timeout has
  313. ;                   occurred.  chk_timeout returns 0 if NOT timed out.
  314. ;  Usage:   long set_timeout( int seconds );
  315. ;           long set_ttimeout( int bios_ticks );
  316. ;           int chk_timeout( long value );
  317. ;
  318. ;  (c) 1990 University of Waterloo,
  319. ;           Faculty of Engineering,
  320. ;           Engineering Microcomputer Network Development Office
  321. ;  version
  322. ;    0.1    7-Nov -1990   E. P. Engelke
  323.  
  324.     public    _set_ttimeout
  325. _set_ttimeout proc    near
  326.     push    bp
  327.     mov    bp,sp
  328.     push    es
  329.         xor     ax,ax
  330.         cwd
  331.         mov     es,ax
  332.         mov     ax,[bp+4+0]        ; initial Bios clock ticks
  333.     pushf                ; critical section
  334.     cli
  335.         add     ax,es:[BIOSCLK+0];
  336.         adc     dx,es:[BIOSCLK+2];
  337.     popf                ; end critical section
  338.     pop    es
  339.     pop    bp
  340.     ret
  341. _set_ttimeout endp
  342.  
  343.     public    _set_timeout
  344. _set_timeout proc near
  345.     push    bp
  346.     mov    bp,sp
  347.     push    es
  348.     push    cx
  349.     xor    ax,ax            ; reference low memory
  350.     mov    es,ax
  351.     mov    ax,[bp+4+0]        ; seconds
  352.     xor    dx,dx
  353.     mov    cx,1165
  354.     mul    cx            ; 1165/64 = 18.203...
  355.     mov    cx,6
  356. tmp:    shr    dx,1
  357.     rcr    ax,1
  358.     loop    tmp
  359.     pushf                ; critical section
  360.     cli
  361.     add    ax,es:[BIOSCLK+0]
  362.     adc    dx,es:[BIOSCLK+2]
  363.     popf                ; end critical section
  364.     pop    cx
  365.     pop    es
  366.     pop    bp
  367.     ret
  368. _set_timeout    endp
  369.  
  370.     public    _chk_timeout
  371. _chk_timeout    proc near
  372.     push    bp
  373.     mov    bp,sp
  374.     push    cx
  375.     push    es
  376.     xor    ax,ax
  377.     mov    es,ax
  378.     pushf                ; critical section
  379.     cli
  380.     mov    cx,es:[BIOSCLK+0]
  381.     mov    bx,es:[BIOSCLK+2]
  382.     popf                ; end critical section
  383.     pop    es
  384.     mov    ax,[bp+4+0]        ; timeout value
  385.     mov    dx,[bp+4+2]
  386.     cmp    dx,bx            ; if timeout < clock, has expired
  387.         jb      ret_tru
  388.     cmp    ax,cx
  389.         jbe    ret_tru
  390.                     ; may have gone over by one day
  391.     sub    ax,LO_MAX
  392.     sbb    dx,HI_MAX
  393.     jc    ret_fal            ; c = nope, timeout is today
  394.                     ; test timeout new values
  395.     cmp    dx,bx
  396.     jb    ret_tru
  397.     cmp    ax,cx
  398.     ja    ret_fal
  399. ret_tru:mov    ax,1            ; say have timed out
  400.     pop    cx
  401.     pop    bp
  402.     ret
  403.  
  404. ret_fal:xor    ax,ax            ; say have not timed out
  405.     pop    cx
  406.     pop    bp
  407.     ret
  408. _chk_timeout    endp
  409.  
  410. ; unsigned long realclock(void)
  411.     public    _realclock
  412. _realclock    proc near        ; get Bios time of day dword
  413.     push    es
  414.     xor    ax,ax            ; reference low memory
  415.     mov    es,ax
  416.     pushf                ; critical section
  417.     cli
  418.     mov    ax,es:[BIOSCLK+0]    ; return Bios time of day tick count
  419.     mov    dx,es:[BIOSCLK+2]
  420.     popf                ; end critical section
  421.     pop    es
  422.     ret
  423. _realclock endp
  424.  
  425. ; void _chkstk()
  426. ; Stack checker
  427. ; {
  428. ;  ;
  429. ; }
  430.     public    __chkstk, __aNchkstk
  431. __chkstk proc    near            ; MSC v5.1
  432. __aNchkstk equ    this byte        ; MSC v6.00
  433.     pop    bx            ; pop return address
  434.     sub    sp,ax            ; down adjust stack pointer
  435.     jmp    bx            ; return the no-stack way
  436. __chkstk endp
  437.  
  438.  
  439. ; void 
  440. ; outch(char ch)
  441. ; Sends character to the screen via the msgbuf buffer if operating at
  442. ; interrupt level, or via DOS if operating at task level.
  443.     public    _outch
  444. _outch    proc    near
  445.     push    bp
  446.     mov    bp,sp
  447.     push    ax
  448.     push    bx
  449.     cmp    _display_mode,0        ; quiet screen?
  450.     je    outch3            ; e = yes
  451.     mov    al,[bp+4]        ; get the character
  452.     cmp    _doslevel,0        ; at DOS task level?
  453.     je    outch1            ; e = no
  454.     mov    ah,conout        ; use DOS
  455.     push    dx
  456.     mov    dl,al
  457.     int    dos
  458.     pop    dx
  459.     jmp    short outch3
  460. outch1:    cmp    _msgcnt,256        ; is buffer filled?
  461.     ja    outch3            ; a = yes, discard this byte
  462.     mov    bx,_msgcnt
  463.     mov    _msgbuf[bx],al
  464.     inc    _msgcnt
  465. outch3:    pop    bx
  466.     pop    ax
  467.     mov    sp,bp
  468.     pop    bp
  469.     ret
  470. _outch    endp
  471.  
  472.  
  473. ; void 
  474. ; outsn(char * string, int count)
  475. ; display counted string
  476.     public    _outsn
  477. _outsn    proc    near
  478.     push    bp
  479.     mov    bp,sp
  480.     push    ax
  481.     push    si
  482.     mov    si,[bp+4]        ; string address
  483.     mov    cx,[bp+4+2]        ; string length
  484.     cld
  485. outsn1:    lodsb
  486.     or    al,al
  487.         jz      outsn2
  488.     push    ax            ; push arg
  489.         call    _outch
  490.     pop    ax            ; clean stack
  491.     loop    outsn1
  492. outsn2:    pop    si
  493.     pop    ax
  494.     mov    sp,bp
  495.     pop    bp
  496.     ret
  497. _outsn    endp
  498.  
  499. ; void 
  500. ; outs(char * string)
  501. ; display asciiz string
  502.     public    _outs
  503. _outs    proc    near
  504.     push    bp
  505.     mov    bp,sp
  506.     push    ax
  507.     push    si
  508.     mov    si,[bp+4]        ; asciiz string address
  509.     cld
  510. outs1:    lodsb
  511.         or      al,al            ; terminator ?
  512.     jz     outs2            ; z = yes
  513.     push    ax            ; push arg
  514.     call    _outch
  515.     pop    ax            ; clean stack
  516.     jmp    short outs1
  517. outs2:    pop    si
  518.     pop    ax
  519.     mov    sp,bp
  520.     pop    bp
  521.     ret
  522. _outs    endp
  523.  
  524. ; void
  525. ; outhex(char c)
  526. ; display char in hex
  527.     public _outhex
  528. _outhex    proc    near
  529.     push    bp
  530.     mov    bp,sp
  531.     push    ax
  532.         mov     ax,[bp+4]        ; incoming character
  533.         mov     cl, 4
  534.         shr     al, cl
  535.         call    outh
  536.         mov     ax,[bp+4]
  537.         call    outh
  538.     pop    ax
  539.     mov    sp,bp
  540.     pop    bp
  541.     ret
  542.  
  543. ; worker for outhex
  544. outh    proc    near
  545.     and     al,0fh
  546.         cmp     al,9
  547.         jbe     outh1
  548.         add     al,'A' - '9' - 1
  549. outh1:    add     al,'0'
  550.         push    ax
  551.         call    _outch
  552.         pop     ax
  553.         ret
  554. outh    endp
  555. _outhex    endp
  556.  
  557. ; output a string of hex chars
  558. ; void
  559. ; outhexes(char * string, int count )
  560.     public    _outhexes
  561. _outhexes proc    near
  562.     push    bp
  563.     mov    bp,sp
  564.     push    ax
  565.     push    si            ; preserve such things
  566.     mov    si,[bp+4]        ; get string pointer
  567.     mov    cx,[bp+4+2]        ; get char count
  568.     cld
  569. outhexs1:lodsb                ; read a byte
  570.     push    cx            ; save loop counter
  571.     push    ax
  572.     call    _outhex            ; display as hex pair
  573.     add    sp,2            ; clean stack
  574.     pop    cx
  575.     loop    outhexs1        ; do count's worth
  576.     pop    si
  577.     pop    ax
  578.     mov    sp,bp
  579.     pop    bp
  580.     ret
  581. _outhexes endp
  582. _TEXT    ends
  583.         end
  584.