home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CEXPRESS.ZIP / BITS.ASM / SETBIT16.ASM < prev    next >
Assembly Source File  |  1989-05-03  |  2KB  |  62 lines

  1. ;void  set_bits_16(value,replacement,position,bits)
  2. ;  unsigned short  *value,replacement;
  3. ;  unsigned char   position,bits;
  4.  
  5.     EXTRN  _memory_model:byte
  6.     EXTRN  _error_code:byte
  7.  
  8. _TEXT    SEGMENT  BYTE PUBLIC 'CODE'
  9.     ASSUME CS:_TEXT
  10.     PUBLIC _set_bits_16
  11. _set_bits_16 proc near
  12.     push bp            ;
  13.     mov  bp,sp        ;set up stack frame
  14.     push di            ;
  15.     cmp  _memory_model,0    ;near or far?
  16.     jle  begin        ;jump if near
  17.     inc  bp            ;else add 2 to BP
  18.     inc  bp            ;
  19. begin:    mov  _error_code,0    ;clear _error_code
  20.     cmp  _memory_model,2    ;data near or far?
  21.     jb   L0            ;jump if near
  22.     les  di,dword ptr[bp+4] ;ptr to Value
  23.     inc  bp            ;add 2 to BP since dword ptr
  24.     inc  bp            ;
  25.     jmp  short L00        ;
  26. L0:    mov  ax,ds        ;ES = DS
  27.     mov  es,ax        ;
  28.     mov  di,[bp+4]        ;
  29. L00:    mov  cl,[bp+10]        ;number bits
  30.     inc  _error_code    ;1 = zero bits
  31.     or   cl,cl        ;error check
  32.     jz   L1            ;quit if zero bits
  33.     mov  dx,0FFFFH        ;all 1s in DX
  34.     shl  dx,cl        ;field-width of 0s
  35.     not  dx            ;field-width of 1s
  36.     mov  ax,[bp+6]        ;replacement value
  37.     and  ax,dx        ;clear superfluous bits
  38.     mov  bl,cl        ;copy Bits to BL
  39.     mov  cl,[bp+8]        ;Position
  40.     inc  _error_code    ;2 = position out of range
  41.     cmp  cl,7        ;in range?
  42.     ja   L1            ;quit if not        
  43.     add  bl,cl        ;add number bits
  44.     inc  _error_code    ;3 = field doesn't fit
  45.     cmp  bl,16        ;does it fit?
  46.     ja   L1            ;quit if error
  47.     shl  ax,cl        ;shift value to position
  48.     shl  dx,cl        ;shift mask to position
  49.     not  dx            ;reverse mask
  50.     and  es:[di],dx        ;clear field
  51.     or   es:[di],ax        ;write new bits
  52.     mov  _error_code,0    ;success, return 0
  53. L1:    pop  di            ;
  54.     pop  bp            ;
  55.     cmp  _memory_model,0    ;quit
  56.     jle  quit        ;
  57.     db   0CBh        ;RET far
  58. quit:    ret            ;RET near
  59. _set_bits_16 endp
  60. _TEXT    ENDS
  61.     END
  62.