-1

I need a free segment to save data, which segments are free to use?

I am trying to save the bitmap data in the memory, the size of the bitmap data is 64K so I want to save it in a seperate segment.

(I am afraid that i will override on a segment which is used)

(DOS, ASM 8086, TASM it it's relevance)

Amir Zak
  • 37
  • 1
  • 5
  • 1
    Question lack details. Why do you think a segment will be used? In which context you need a free segment? – m0skit0 May 19 '15 at 13:27
  • 1
    Which target OS? If it's DOS you can allocate memory with `INT 21h/AH=48h`. – Michael May 19 '15 at 13:42
  • Not enough, state OS and if you're running inside another process. – m0skit0 May 19 '15 at 17:29
  • I think you just need to read more about how your assembler works. TASM supports different [memory models](http://www.ousob.com/ng/masm/ng564f9.php). Select the memory model you need and when you write the assembly for it and link, it will do the right thing with the segment. – lurker May 20 '15 at 11:21

2 Answers2

0

You can either allocate a static buffer in your executable or dynamically allocate a buffer like suggested by Micheal. To statically allocate a 64K buffer you want to do something like this:

BITMAPBUF SEGMENT PARA PRIVATE
    DB  0ffffh DUP (?)
BITMAPBUF ENDS  

BITMAPBUF2 SEGMENT BYTE PRIVATE
    DB  ?
BITMAPBUF2 ENDS

The BITMAPBUF2 segment is a bit of hack to get around the fact that the versions of TASM and TLINK I'm using don't seem to handle 64K segments correctly. So I create two segments that the linker will place adjacent to each other.

To allocate the buffer dynamically using MS-DOS you want code something like this:

    mov ah, 48h
    mov bx, 1000h 
    int 21h
    jc  failed
    mov [bitmapbuf_seg], ax

Note that if you're trying to create a .COM file both of these methods will fail. The former because .COM files can't be greater than 64K and the second because .COM executables allocate all of free memory when they start(*), so there's no memory to dynamically allocate. You can solve the later problem by freeing unused memory before using INT 21h/AH=48h but the simpler solution is to build an .EXE instead.

Here some example code that copies the contents of the VGA frame buffer at A000:0000 to the buffer. Uncomment out one of the two lines at the start depeding whether the buffer was statically or dynamically allocated:

    ; mov   ax, BITMAPBUF
    ; mov   ax, [bitmapbuf_seg]
    mov es, ax
    xor di, di
    mov ax, 0a000h
    mov ds, ax
    xor si, si
    mov cx, 8000h
    rep movsw

(*) actually it allocates the largest free contiguous block of memory, but that usually means pretty much all of conventional memory.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
0

If we start an executable, then DOS give it all of the free ram. But we do not know which segment is really free or not. So if we want to allocate some ram, we have to give back all of the ram that we does not needed just before we can allocate some of it, for to make it sure that the segments are not in use.

          call SETFREE               ; first we have to free the rest of RAM
          mov      bx, 2000h         ; allocate 128 KB
          call GETSPACE
          jc  ERROR
          mov      [NEWSEG], ax      ; store segment address

;------------------------------------
SETFREE:  mov      bx, ss            ; calculating the amount of bytes
          mov      ax, es            ; that our program is needed
          sub      bx, ax            ; for the code, data and stacksize
          mov      ax, sp            ; and free the rest
          add      ax, 0Fh
          shr      ax, 4
          add      bx, ax
          mov      ah, 4Ah
          int    21h
          ret
;------------------------------------
GETSPACE: mov      ah, 48h
          int    21h
          ret