Advertisement

Assembly question

Started by December 10, 2000 05:01 PM
8 comments, last by Tank 24 years, 1 month ago
I'm ~trying~ to make a routine in assembly to allocate memory in using tasm 2.0 Does anyone know how to do this? Here's what I tried, but i'm having no luck so far...
  
.model small
.386

; ------------- MACROS ------------------
MallocMemory	macro
	mov ah,48h	; alloc memory function
	mov bx,40h	; allocate 40 pages
	int 21h
	mov es,ax	; save the seg in es
endm

FreeMemory	macro
	mov ah,49h	; free memory function
	int 21h
endm

PrintSuccess	macro
	mov ah,2	; print character func
	mov dl,'*'	; print a '*'
	int 21h
endm

PrintError	macro
	mov ah,2	; print character func
	mov dl,'!'	; print a '!'
	int 21h
endm


; ------------ DATA --------------------
.data

; ------------ CODE --------------------
.code
main proc
	mov ax,@data	; point ds to the beginning 
	mov ds,ax	; of the data segment

	MallocMemory	; allocate the memory
	jnc FINISH	; if no error, goto FINISH
ERROR:
	PrintError	; an error occured
	jmp DONE	; goto DONE

FINISH:
	PrintSuccess	; victory is mine!
	FreeMemory	; free the memory

DONE:
	mov ax,4C00h	; function 4C - terminate process
	int 21h		; doit
main endp


; ------------- STACK -------------------
.stack 100h

end main

  
Does anybody know why this code can't allocate me memory??? Thx in advance. Edited by - Tank on 12/10/00 5:03:28 PM
c''mon this is part of my school project (of corse i''m not required to allocate the memory). The code is 90% complete (1000 lines of asm code) but i''ve been working on the problem above for over 2 days (8 hours...ugh).

Unfortunetly I get no errors assembling the code, just doesn''t allocate the memory
Am I missing something in the code??? Pls help!
Advertisement
Hi, the following is an excerpt from the Interrupt List:
quote:

Notes: DOS 2.1-5.0 coalesces free blocks while scanning for a block to
allocate
.COM programs are initially allocated the largest available memory
block, and should free some memory with AH=49h before attempting any
allocations


So, it looks like you need to call FreeMemory first, then you can allocate memory.
Thx, but i''m using DOS 7.0 (Win98 SE)
Anyways, if that''s the case, what segment am I supposed to free?
I just checked out the int list and it said if carry is set for func 48h ax is either a

7- Memory control blocks destroyed
8- Insufficient memory
9- Invalid memory block address

After I call func 48h I get a 9 in ax. What does err #9 mean (invalid mem block address)?

I got this quote from the Art of Assembly Language Programming online book.

''Since these are complex functions, they shouldn''t be used unless you have a very specific purpose for them. Misusing these commands may result in loss of system memory that can be reclaimed only by rebooting the system.''

Is there another way to allocate memory using assembly?
Advertisement
well its not exactly what you want but... you could use the bss segment... it''s semi dynamic heh.... the memory is not alocated until run time.... so no disk space is used... ummmm I''ve tried using the dos mem allocation funcs my self and I''ve never been able to get them to work eather.... I''m not sure what I was doing wrong eather....

Great Milenko

Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."


http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech

The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
Can you remind me of big is 40 pages in your allocation? (in bytes)

You have to watch the size of your allocation because a .com program has to fit under the 64k limit (program and data).

Try to do a very small allocation and see if it work, if it is, increase it until it continues to work.

If you dont have enough memory, try to change above the .tiny model.

hope it help
-=-=-=-=-=-=-=-=-=-=-=-=-=-luc.raymond@netcourrier.com
DOS allocates memory in what are usually called paragraphs (or pages), each 16 bytes in size (the gap between segment boundaries, so that only a segment is needed for return). 40h pages equals eaxactl 1024 bytes.

I''ve tried this too, in debug, but I get error 8. I''m using Win95 OSR2. I''ll work on this this weekend though...

I program a lot of DOS assembly programs as a hobby, and one thing i''ve discovered, is that a DOS .exe program gets all the free DOS memory allocated to it when it is executed. So when you try to allocate more memory, it fails because there is nothing left to allocate.

When your program runs, and allocates all the memory, it usually starts by filling the first part of the memory up with your data. Afte the data comes your code. After the code the rest of the memory is unused. If you look in the standard libraries that comes with The Art Of Assembly Language, you will see what their memory management functions do to use this free memory.

They create a segment after their code. This is just a dummy segment, used to get the memory location where the empty memory begins. Then they look up a value in the PSP to find out where the end of the free memory is. This is called the "heap". Then they have their own functions, for allocating and deallocating sections of this heap to be used by the main program.

This is how I understand it anyway, I may be wrong, but I''ve found this method to work for me.

This topic is closed to new replies.

Advertisement