home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / crt0 / sbrk16.asm < prev   
Encoding:
Assembly Source File  |  1994-12-13  |  2.6 KB  |  108 lines

  1. ; Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details
  2. ;
  3. ; $Id: sbrk16.asm 2.0 1994/03/14 00:47:04 dj Exp $
  4. ; $Log: sbrk16.asm $
  5. ; Revision 2.0  1994/03/14  00:47:04  dj
  6. ; initial version
  7. ;
  8. ;
  9.  
  10. ;-----------------------------------------------------------------------------
  11. ;  sbrk 16-bit helper
  12. ;
  13. ;  Transferred to 16-bit code segement to run in protected mode.
  14. ;  Will make DPMI segment altering requests and update selectors
  15. ;  as needed.  Image will always need to allocate an exact
  16. ;  multiple of 16 bytes, load offset will always be zero.
  17. ;  Number of bytes to copy will always be multiple of four.
  18. ;
  19. ;  Application must set cs_selector, ds_selector, and local_ds
  20. ;  appropriately.  Application uses first word in image to find
  21. ;  API entry point.  Call with FAR call.
  22. ;
  23. ;  Call with:    BX:CX = new size
  24. ;        SI:DI = old handle
  25. ;  Returns:    BX:CX = new base
  26. ;        SI:DI = new handle
  27. ;        all others trashed
  28.  
  29.     .type    "bin"
  30.  
  31. ;-----------------------------------------------------------------------------
  32. ;  Start of API header
  33.  
  34. offset_of_api:            ; offset of API function entry point
  35.     .dw    sbrk_16_helper
  36. cs_selector:            ; code selector to be updated
  37.     .dw    0
  38. ds_selector:            ; data selector to be updated
  39.     .dw    0
  40. local_ds:            ; selector mapped to same as local cs
  41.     .dw    0
  42. bytes_to_allocate:        ; number of bytes app allocates for this image
  43.     .dw    stack
  44.  
  45. ;-----------------------------------------------------------------------------
  46. ;  Start of local data
  47.  
  48. save_ss:
  49.     .dw    0
  50. save_esp:
  51.     .dd    0
  52. save_ds:
  53.     .dw    0
  54.  
  55. ;-----------------------------------------------------------------------------
  56. ;  Start of code
  57.  
  58. sbrk_16_helper:
  59.  
  60.     mov    ax, ds            ; switch to local data segment
  61.     mov    ds, cs:[local_ds]
  62.     mov    [save_ds], ax
  63.     mov    [save_ss], ss        ; switch to local stack
  64.     mov    [save_esp], esp
  65.     mov    ss, [local_ds]
  66.     mov    esp, stack
  67.  
  68.     mov    ax, 0x0503        ; realloc memory
  69.     int    0x31
  70.     jc    error_return        ; bx:cx = base address
  71.  
  72.     mov    dx, cx
  73.     mov    cx, bx            ; cx:dx = base address
  74.     mov    bx, [cs_selector]
  75.     mov    ax, 0x0007
  76.     int    0x31            ; set cs to new base
  77.     mov    bx, [ds_selector]
  78.     mov    ax, 0x0007
  79.     int    0x31            ; set ds to new base
  80.  
  81.     push    es            ; reload es
  82.     pop    es
  83.     push    fs            ; reload fs
  84.     pop    fs
  85.     push    gs            ; reload gs
  86.     pop    gs
  87.  
  88.     mov    bx, cx
  89.     mov    cx, dx            ; bx:cx = base address
  90.  
  91. error_return:
  92.  
  93.     mov    ss, [save_ss]        ; return to old stack
  94.     mov    esp, [save_esp]
  95.     mov    ds, [save_ds]        ; return to old data segment
  96.  
  97.     .opsize                ; 32-bit far return
  98.     retf
  99.  
  100. ;-----------------------------------------------------------------------------
  101. ;  Start of stack
  102.  
  103.     .align    4            ; so that image size is longwords
  104.     .bss
  105.     .align    16            ; so that alloc size is paragraphs
  106.     .db    512 .dup 0
  107. stack:
  108.