home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / linux_bo / netboot.zoo / asmstuff.asm < prev    next >
Encoding:
Assembly Source File  |  1993-05-04  |  9.9 KB  |  589 lines

  1. ;
  2. ;  TCP/IP support routines
  3. ;****************************************************************************
  4. ;*                                                                          *
  5. ;*                                                                          *
  6. ;*      part of NCSA Telnet                                                 *
  7. ;*      by Tim Krauskopf, VT100 by Gaige Paulsen, Tek by Aaron Contorer     *
  8. ;*                                                                          *
  9. ;*      National Center for Supercomputing Applications                     *
  10. ;*      152 Computing Applications Building                                 *
  11. ;*      605 E. Springfield Ave.                                             *
  12. ;*      Champaign, IL  61820                                                *
  13. ;*                                                                          *
  14. ;****************************************************************************
  15. ;
  16.     NAME    ASMSTUFF
  17.     include model.inc
  18.  
  19. ifdef Microsoft
  20. DGROUP  group _DATA
  21. _DATA segment public 'DATA'
  22.     assume DS:DGROUP
  23. .DATA
  24. else    
  25.     DSEG
  26. endif
  27.  
  28. ifdef Microsoft
  29. _DATA ends
  30.  
  31. _TEXT    segment public 'CODE'
  32.     assume CS:_TEXT
  33.     PUBLIC  _IPCHECK, _TCPCHECK, _LONGSWAP, _INTSWAP, _DBG, _N_PUTCHAR
  34.     PUBLIC  _SEGSS, _SEGDS, _SEGCS
  35.     PUBLIC  _DOS_EXIT
  36.     PUBLIC  _N_KBHIT, _N_GETCH
  37.     PUBLIC  _N_LMUL
  38.     PUBLIC  _N_LDIV
  39.     PUBLIC  _N_LMOVE
  40.     PUBLIC    _N_MAMOUNT
  41.     PUBLIC  _FAR2LONG
  42.     PUBLIC  _CANNON
  43.     PUBLIC  _LONG2FAR
  44. else
  45.     ENDDS
  46.     PSEG
  47.     PUBLIC    IPCHECK,TCPCHECK,LONGSWAP,INTSWAP,DBG,N_PUTCHAR
  48.     PUBLIC  SEGSS, SEGDS, SEGCS
  49.     PUBLIC  DOS_EXIT
  50.     PUBLIC  N_KBHIT, N_GETCH
  51.     PUBLIC  N_LMUL
  52.     PUBLIC  N_LDIV
  53.     PUBLIC  N_LMOVE
  54.     PUBLIC    N_MAMOUNT
  55.     PUBLIC  FAR2LONG
  56.     PUBLIC  CANNON
  57.     PUBLIC  LONG2FAR
  58. endif    
  59. ;
  60. ;  Routines for general use by the communications programs
  61. ;
  62. ;
  63. ;************************************************************************
  64. ;  DBG
  65. ;  provides a synch point for debugging
  66. ;
  67. START_PROC  dbg
  68.     nop
  69.     nop
  70.     nop
  71.     ret
  72. END_PROC dbg
  73.  
  74. ;
  75. ;*************************************************************************
  76. ;  Internet header checksum
  77. ;    header checksum is calculated for a higher level program to verify
  78. ;
  79. ;  USAGE:  ipcheck((IPKT *)ptr,(int)len)
  80. ;
  81. ;  this proc knows that the IP header length is found in the first byte
  82. ;
  83. START_PROC IPCHECK
  84.  
  85.     PUSH    BP
  86.     MOV        BP,SP
  87.     PUSH    ES
  88.     PUSH    SI
  89.     PUSH    DI
  90.  
  91.     MOV        SI,[BP+X]        ; pointer to data
  92.     MOV        CX,[BP+X+2]        ; count of words to test
  93.     XOR        BX,BX
  94.     CLC
  95. CHKSUM:
  96.     LODSW                    ; get next word
  97.     ADC        BX,AX            ; keep adding
  98.     LOOP    CHKSUM            ; til' done
  99.     ADC        BX,0            ; adds the carry bit in
  100. ;
  101.     NOT        BX                ; take one more 1-complement
  102.     MOV        AX,BX
  103.  
  104.     POP        DI
  105.     POP        SI
  106.     POP        ES
  107.     POP        BP
  108.     RET
  109.  
  110. END_PROC IPCHECK
  111. ;
  112. ;  TCP checksum, has two parts, including support for a pseudo-header
  113. ;
  114. ;  usage:   tcpcheck(psptr,tcpptr,tcplen)
  115. ;            char *psptr,*tcpptr;  pointers to pseudo header and real header
  116. ;            int tcplen            length of tcp packet in checksum
  117. ;
  118. START_PROC TCPCHECK
  119.  
  120.     PUSH    BP
  121.     MOV        BP,SP
  122.     PUSH    SI
  123.     PUSH    DI
  124.  
  125.     MOV        SI,[BP+X]        ; pointer to data
  126.     MOV        CX,6            ; length of p-hdr in words
  127.     XOR        BX,BX           ; clear to begin
  128.     CLC
  129. PCHKSUM:
  130.     LODSW                    ; get next word
  131.     ADC        BX,AX            ; keep adding
  132.     LOOP    PCHKSUM            ; til' done
  133.     ADC        BX,0            ; adds the carry bit in
  134. ;
  135. ; NOW THE REAL THING
  136. ;
  137.     MOV        SI,[BP+X+2]        ; pointer
  138.     MOV        CX,[BP+X+4]        ; count of bytes to test
  139.     MOV        DX,CX            ; keep a copy
  140.     SHR        CX,1            ; divide by two, round down
  141.     CLC
  142. RCHKSUM:
  143.     LODSW
  144.     ADC        BX,AX            ; add to previous running sum
  145.     LOOP    RCHKSUM    
  146.     ADC        BX,0            ; add the last carry in again
  147.     AND        DX,1            ; odd # of bytes?
  148.     JZ        NOTODD
  149.     LODSB                    ; get that last byte
  150.     XOR        AH,AH            ; clear the high portion
  151.     ADD        BX,AX            ; add the last one in
  152.     ADC        BX,0            ; add the carry in, too
  153. NOTODD:
  154.     NOT        BX                ; take one more 1-complement
  155.     MOV        AX,BX
  156.     POP        DI
  157.     POP        SI
  158.     POP        BP
  159.     RET
  160.  
  161. END_PROC TCPCHECK
  162.  
  163. ;
  164. ;*************************************************************************
  165. ;  longswap
  166. ;    swap the bytes of a long integer from PC
  167. ;  order (reverse) to in-order.  This will work both ways.
  168. ;  returns the new long value
  169. ;  usage:
  170. ;      l2 = longswap(l)
  171. ;    long l;
  172. ;
  173. ifdef Microsoft
  174. _LONGSWAP    PROC    NEAR
  175.     PUSH    BP
  176.     MOV        BP,SP
  177.  
  178.     MOV        AX,[BP+X+2]        ; HIGH BYTES OF THE LONG INT
  179.     MOV        DX,[BP+X]        ; LOW BYTES OF THE LONG INT
  180. ;
  181. ;  GET THE DATA
  182. ;
  183.     XCHG    AH,AL            ; SWAP THEM, THESE ARE NOW LOW
  184.     XCHG    DH,DL            ; SWAP THE OTHERS
  185.     POP        BP
  186.     RET
  187. _LONGSWAP    ENDP
  188. else
  189. LONGSWAP    PROC    NEAR
  190.     PUSH    BP
  191.     MOV        BP,SP
  192.     MOV        BX,[BP+X+2]        ; HIGH BYTES OF THE LONG INT
  193.     MOV        AX,[BP+X]        ; LOW BYTES OF THE LONG INT
  194. ;
  195. ;  GET THE DATA
  196. ;
  197.     XCHG    AH,AL            ; SWAP THEM, THESE ARE NOW LOW
  198.     XCHG    BH,BL            ; SWAP THE OTHERS
  199.     POP        BP
  200.     RET
  201. LONGSWAP    ENDP
  202. endif
  203. ;
  204. ;*************************************************************************
  205. ;  INTSWAP
  206. ;    swap the bytes of an integer, returns the swapped integer
  207. ;
  208. ;   usage:      i = intswap(i);
  209. ;
  210. START_PROC INTSWAP
  211.     PUSH    BP
  212.     MOV        BP,SP
  213.  
  214.     MOV     AX,[BP+X]
  215.     XCHG    AH,AL
  216.     POP        BP
  217.     RET
  218.  
  219. END_PROC INTSWAP
  220.  
  221. ;
  222. ;  Support for BIOS calls in NCSA Telnet
  223. ;
  224. ;   From original code by Tim Krauskopf    1984-1985
  225. ;
  226. ;   Modified and ported to Lattice C, Sept. 1986
  227. ;   ifdefs for Microsoft C, June 1987
  228. ;   Tim Krauskopf
  229. ;
  230. ;   Modified for version 2.3 release May 1990 by Heeren Pathak
  231. ;
  232. ;   National Center for Supercomputing Applications
  233. ;
  234.  
  235. ;   The subroutines to call from C
  236. ;
  237. ;/***************************************************************/
  238. ;  n_putchar(letter)
  239. ;      puts onto screen at current cursOR location
  240. ;
  241. START_PROC N_PUTCHAR
  242.  
  243.     PUSH    BP
  244.     MOV        BP,SP
  245.     PUSH    DS
  246.     PUSH    ES
  247.     PUSH    SI
  248.     PUSH    DI
  249.  
  250.  
  251.     MOV        AL,[BP+X]        ; char to write
  252.  
  253.     MOV        BL,3                ; SEND WHATEVER CHAR
  254.     MOV        BH,0
  255.     MOV        AH,14
  256.     INT     10H
  257.  
  258.     POP        DI
  259.     POP        SI
  260.     POP        ES
  261.     POP        DS
  262.     POP        BP
  263.     RET
  264.  
  265. END_PROC N_PUTCHAR
  266.  
  267. START_PROC SEGSS
  268.     mov    ax,ss
  269.     ret
  270. END_PROC SEGSS
  271.  
  272. START_PROC SEGCS
  273.     mov    ax,cs
  274.     ret
  275. END_PROC SEGCS
  276.  
  277. START_PROC SEGDS
  278.     mov    ax,ds
  279.     ret
  280. END_PROC SEGDS
  281.  
  282.  
  283. START_PROC dos_exit
  284.  
  285.     ; assume dos version 2
  286.  
  287.     PUSH    BP
  288.     MOV        BP,SP
  289.  
  290.     MOV        AL,[BP+X]        ; exit code
  291.     mov        ah, 4ch
  292.     int    21h
  293.     pop    bp                ; shouldn't get here
  294.     ret
  295. END_PROC dos_exit
  296.  
  297.  
  298. START_PROC n_kbhit
  299.     PUSH    BP
  300.     MOV        BP,SP
  301.     PUSH    DS
  302.     PUSH    ES
  303.     PUSH    SI
  304.     PUSH    DI
  305.  
  306.     MOV        AH,1
  307.     INT     16H
  308.     mov    ax,0
  309.     jz    not_there
  310.     inc    ax
  311.  
  312. not_there:
  313.  
  314.     POP        DI
  315.     POP        SI
  316.     POP        ES
  317.     POP        DS
  318.     POP        BP
  319.     RET
  320.  
  321. END_PROC n_kbhit
  322.  
  323. START_PROC n_getch
  324.     PUSH    BP
  325.     MOV        BP,SP
  326.     PUSH    DS
  327.     PUSH    ES
  328.     PUSH    SI
  329.     PUSH    DI
  330.  
  331.     MOV        AH,0
  332.     INT     16H
  333.  
  334.     cmp    al,0
  335.     jz    special
  336.     xor    ah,ah
  337. special:
  338.     POP        DI
  339.     POP        SI
  340.     POP        ES
  341.     POP        DS
  342.     POP        BP
  343.     RET
  344.  
  345. END_PROC n_getch
  346.  
  347. START_PROC N_LMUL
  348.     PUSH    BP
  349.     MOV        BP,SP
  350.     PUSH    DS
  351.     PUSH    ES
  352.     PUSH    SI
  353.     PUSH    DI
  354.  
  355.     mov    ax,[x+bp]
  356.     mov    dx,[x+bp+2]     ; high
  357.     mov    bx,[x+bp+4]    ; low
  358.     mov    cx,[x+bp+6]    ; high
  359.  
  360.     mov    bp,dx        ; if both high words zero - great
  361.     or    bp,cx
  362.     jnz    star1
  363.  
  364.     mul    bx
  365.     jmp    mul_exit
  366.  
  367. star1:
  368.     mov    bp,ax
  369.     push    dx
  370.     mul    bx
  371.     xchg    ax,bp
  372.     xchg    cx,dx
  373.     mul    dx
  374.     add    cx,ax
  375.     pop    ax
  376.     mul    bx
  377.     add    cx,ax
  378.     mov    dx,cx        ; high
  379.     mov    ax,bp        ; low
  380. mul_exit:
  381.     POP        DI
  382.     POP        SI
  383.     POP        ES
  384.     POP        DS
  385.     POP        BP
  386.     RET
  387.  
  388. END_PROC N_LMUL
  389.  
  390.     ; expects (long divisor) (long dividend) returns dx:ax
  391. ISOR_HIGH EQU (X+6)
  392. ISOR_LOW  EQU (X+4)
  393. DEND_HIGH EQU (X+2)
  394. DEND_LOW  EQU (X)
  395.     ;
  396. START_PROC N_LDIV
  397.  
  398.     push    bp
  399.     mov    bp,sp
  400.     push    di
  401.     push    si
  402.     push    bx
  403.     xor    di,di
  404.     mov    ax,[bp+DEND_HIGH]
  405.     or    ax,ax
  406.     jge    dvs_1
  407.     inc    di
  408.     mov    dx,[bp+DEND_LOW]
  409.     neg    ax
  410.     neg    dx
  411.     sbb    ax,0
  412.     mov    [bp+DEND_HIGH],ax
  413.     mov    [bp+DEND_LOW],dx
  414. dvs_1:  mov    ax,[bp+ISOR_HIGH]
  415.     or    ax,ax
  416.     jge    dvs_2
  417.     inc    di
  418.     mov    dx,[bp+ISOR_LOW]
  419.     neg    ax
  420.     neg    dx
  421.     sbb    ax,0
  422.     mov    [bp+ISOR_HIGH],ax
  423.     mov    [bp+ISOR_LOW],dx
  424. dvs_2:  or    ax,ax
  425.     jnz    dvs_3
  426.     mov    cx,[bp+ISOR_LOW]
  427.     mov    ax,[bp+DEND_HIGH]
  428.     xor    dx,dx
  429.     div    cx
  430.     mov    bx,ax
  431.     mov    ax,[bp+DEND_LOW]
  432.     div    cx
  433.     mov    dx,bx
  434.     jmp    dvs_4
  435. dvs_3:  mov    bx,ax
  436.     mov    cx,[bp+ISOR_LOW]
  437.     mov    dx,[bp+DEND_LOW]
  438.     mov    ax,[bp+DEND_HIGH]
  439. dvs_5:  shr    bx,1
  440.     rcr    cx,1
  441.     shr    dx,1
  442.     rcr    ax,1
  443.     or    bx,bx
  444.     jnz    dvs_5
  445.     div    cx
  446.     mov    si,ax
  447.     mul    word ptr [bp+ISOR_HIGH]
  448.     xchg    cx,ax
  449.     mov    ax,[bp+ISOR_LOW]
  450.     mul    si
  451.     add    dx,cx
  452.     jb    dvs_6
  453.     cmp    dx,[bp+DEND_HIGH]
  454.     ja    dvs_6
  455.     jb    dvs_7
  456.     cmp    ax,[bp+DEND_LOW]
  457.     jbe    dvs_7
  458. dvs_6:  dec    si
  459. dvs_7:  xor    dx,dx
  460.     xchg    si,ax
  461. dvs_4:  dec    di
  462.     jnz    dvs_8
  463.     neg    dx
  464.     neg    ax
  465.     sbb    dx,0
  466. dvs_8:  pop    bx
  467.     pop    si
  468.     pop    di
  469.     mov    sp,bp
  470.     pop    bp
  471.     ret
  472. END_PROC N_LDIV
  473.  
  474. START_PROC n_lmove
  475.  
  476.     PUSH    BP
  477.     MOV        BP,SP
  478.     PUSH    DS
  479.     PUSH    ES
  480.     PUSH    SI
  481.     PUSH    DI
  482.  
  483.     mov    ax,ds
  484.     mov    es,ax        ;  copy ds
  485.     mov    si,[x+bp]    ; pointer
  486.     mov    cx,[x+bp+2]     ; no of words to move
  487.     mov    ax,8700h    ; extended memory block move
  488.     int    15h
  489.     jc    n_l_err
  490.     xor    ax,ax
  491.     jmp    n_l_out
  492. n_l_err:
  493.     mov    al,ah
  494.     xor    ah,ah
  495. n_l_out:
  496.     POP        DI
  497.     POP        SI
  498.     POP        ES
  499.     POP        DS
  500.     POP        BP
  501.     RET
  502.  
  503. END_PROC n_lmove
  504.  
  505.  
  506. START_PROC n_mamount
  507.  
  508.     PUSH    BP
  509.     MOV        BP,SP
  510.     PUSH    DS
  511.     PUSH    ES
  512.     PUSH    SI
  513.     PUSH    DI
  514.     mov    ax,8800h    ; extended memory amount
  515.     int    15h
  516.  
  517.     POP        DI
  518.     POP        SI
  519.     POP        ES
  520.     POP        DS
  521.     POP        BP
  522.     RET
  523.  
  524. END_PROC n_mamount
  525.  
  526. START_PROC far2long
  527.     PUSH    BP
  528.     MOV    BP,SP
  529.     mov    dx,[x+2+bp]    ; high
  530.     xor    ax,ax        ; clear low for now
  531.     mov    cx,12
  532. f2lp:
  533.     shr    dx,1        ; shift low bit into carry
  534.     rcr    ax,1        ; carry into top bit
  535.     loop    f2lp
  536.  
  537.     add    ax,[x+bp]    ; low
  538.     adc    dx,0
  539.  
  540.     POP        BP
  541.     RET
  542.  
  543. END_PROC far2long
  544.  
  545. START_PROC cannon
  546.     PUSH    BP
  547.     MOV    BP,SP
  548.     mov    dx,[x+2+bp]    ; high
  549.     mov    ax,[x+bp]    ; low
  550.     mov    cl,4
  551.     shr    ax, cl
  552.     add    dx,ax        ; this could overflow 
  553.                 ; --- but not unless fxxxx + ffff
  554.     mov    ax,[x+bp]    ; low
  555.     and    ax,0fh
  556.     POP        BP
  557.     RET
  558.  
  559. END_PROC cannon
  560.  
  561. START_PROC long2far
  562.     PUSH    BP
  563.     MOV    BP,SP
  564.     mov    dx,[x+2+bp]    ; high
  565.     mov    cl,4
  566.     shr    dx,1        ; put into carry
  567.     rcr    dx,cl        ; low 4 bits into high four bits
  568.     and    dx,0f000h
  569.     mov    ax,[x+bp]    ; low
  570.     mov    cl,4
  571.     shr    ax,cl        ; divide by 16
  572.     add    dx,ax        ; could or
  573.     mov    ax,[x+bp]    ; low again
  574.     and    ax,0fh
  575.     POP        BP
  576.     RET
  577.  
  578. END_PROC long2far
  579.  
  580. ifdef Microsoft
  581. _TEXT ends
  582.  
  583. else
  584.     ENDPS
  585. endif
  586.     END
  587.  
  588.  
  589.