Skip to main content
gamedev.net
🔒 Locked

alloc in asm

Started by Yohomyth May 11, 2004 at 5:07 PM 14 replies 800+ views
Original Post
Yohomyth
Yohomyth
What is the equivalent of malloc() and mfree() in ASM? ----------------------------------------------- Here comes the Thnikkaman!
------------------------------------------------------------"Many combilations elizagerth. I hope you see my particles." - Senor Cardgage
bytecoder
bytecoder
What operating system? What kind of assembly? System specific or platform independant? Usually you would just use the C standard library for this kind of stuff, unless you''re coding in 16 bit assembly, which means you would have to use the DOS api. And do be more specific next time.
RenderTarget
RenderTarget
In general, asm is for inner loops. malloc/free is not. The two should rarely encounter each other in any sane program.

I like pie.
[sub]My spoon is too big.[/sub]
igni ferroque
igni ferroque
The OS should provide an interface for making kernel function calls from userland code. malloc and free will be one of these functions.
Free Mac Mini (I know, I'm a tool)
Yohomyth
Yohomyth
I''m talking about an x86 processor with windows in c/c++. if the two are rarely used together, why is it so common is c++ programs?



-----------------------------------------------
Here comes the Thnikkaman!
------------------------------------------------------------"Many combilations elizagerth. I hope you see my particles." - Senor Cardgage
Sneftel
Sneftel
quote:
Original post by Yohomyth
if the two are rarely used together, why is it so common is c++ programs?
You misunderstood him. malloc and free are commonly used together in C. Malloc and free are not commonly used at all in asm, because the parts of your code that you write in asm should not be the parts where you do allocation.


"Sneftel is correct, if rather vulgar." --Flarelocke
quasar3d
quasar3d
in nasm syntax:

extern _malloc
extern _free

; allocate 1024 bytes
push dword 1024
call _malloc
add esp,4
; eax now contains the pointer

; free the allocated mem again
push eax
call _free
add esp,4

If you are using another assembler, the syntax might be a bit different, but you get the point.

My Site
Anon Mike
Anon Mike
To say it a little plainer than quasar3d, if you need malloc or free in asm you just call them the same as you would any other function. There''s no magic to it except maybe figuring out the calling convention.
-Mike
Archimage
Archimage
There are of course ways to allocate and free memory directly in ASM, normally via BIOS/INT calls (long time since I coded pure ASM). If thats what you want I'll try and dig out the details.

However, having said that, if you are coding for Windows, you might as well use the functions of the environment, as described above.

Yours,

Archimage
(Ann-Marie Ratcliffe)


[edited by - Archimage on May 11, 2004 7:31:06 PM]
Puzzler183
Puzzler183
quote:
Original post by Sneftel
You misunderstood him. malloc and free are commonly used together in C. Malloc and free are not commonly used at all in asm, because the parts of your code that you write in asm should not be the parts where you do allocation.



How do you know he isn''t doing just ASM?
Sneftel
Sneftel
quote:
Original post by Puzzler183
How do you know he isn''t doing just ASM?

I kind of assumed he''d have a basic degree of common sense. Chris Sawyer might decide he''s going to code an entire game in ASM, but the rest of us are somewhat more prudent.


"Sneftel is correct, if rather vulgar." --Flarelocke
Archimage
Archimage
quote:
Original post by Sneftel
I kind of assumed he'd have a basic degree of common sense. Chris Sawyer might decide he's going to code an entire game in ASM, but the rest of us are somewhat more prudent.



Much as I'd love to defend pure ASM (I wrote my first game in pure 6502 Assembler (Acorn BBC B) and since written complete apps in x86 ASM for DOS and Win3.1), from Win95 onwards, I have to admit you might as well use what you can get from the SDK even if the rest of the code is ASM.

Yours,

Archimage
(Ann-Marie Ratcliffe)

(I can always remember when I finally got the split screen working on the BBC B (change mode 2/3's down the screen from VSync) so I could have high res and 16 colours on the same screen..... only to buy Elite and see that they had done it first )


[edited by - Archimage on May 12, 2004 6:36:28 AM]
quasar3d
quasar3d
Even chris sawyer used c to set up his window and initialize directx.

And afaik, those interupts don''t work in windows, or at least you don''t have the privelage to use them. The dos api were those interupts, in windows it are c style functions, and so you have to use them.

My Site
VolkerG
VolkerG
quote:
Original post by Archimage
There are of course ways to allocate and free memory directly in ASM, normally via BIOS/INT calls (long time since I coded pure ASM). If thats what you want I''ll try and dig out the details.

However, having said that, if you are coding for Windows, you might as well use the functions of the environment, as described above.

Yours,

Archimage
(Ann-Marie Ratcliffe)


[edited by - Archimage on May 11, 2004 7:31:06 PM]


Memory management is part of the OS. A normal PC BIOS doesn''t care about that stuff (why should it at all).
Archimage
Archimage
quote:
Original post by quasar3d
And afaik, those interupts don't work in windows, or at least you don't have the privelage to use them. The dos api were those interupts, in windows it are c style functions, and so you have to use them.


Whilst it is possible to do it purely in ASM, you are right in that it gets very messy working around Windows - you basically have to duplicate what Windows does in its allocation/free functions, including pacifying Windows as regards privileges etc - so you might as well use the ones provided.

quote:
Original post by VolkerG
Memory management is part of the OS. A normal PC BIOS doesn't care about that stuff (why should it at all).


Ok, scrub the BIOS mention (it was a long day). Having said that, its actually the processor that knows about memory allocation etc - unless you are saying that you need an OS on which to write a new OS which does memory management - And that OS whould obviously need a OS that it was written on/in so it could do memory management - And that OS would need....

Archimage


[edited by - Archimage on May 12, 2004 6:33:14 AM]
ZedFx
ZedFx
alternatively you can use that thing called COM *shudders*

.data
Alloc_pIMalloc DWORD 0

.code

; modifies eax, ecx, edx
Alloc proc public cb:DWORD
.IF !Alloc_pIMalloc
invoke CoGetMalloc,1 , ADDR Alloc_pIMalloc
.IF eax
xor eax, eax ; NULL return pointer
ret
.ENDIF
.ENDIF
push cb
mov ecx, Alloc_pIMalloc
mov ecx, [ecx]
call DWORD PTR [ecx] + 12
ret
Alloc endp

; modifies eax, ecx, edx
Free proc public pv:DWORD
push pv
push Alloc_pIMalloc
mov ecx, Alloc_pIMalloc
mov ecx, [ecx]
call DWORD PTR [ecx] + 20
xor eax, eax
ret
Free endp

(if you didnt know, i cheated and stole this info from the MASM32 libraries ;-) )

Edit: Damn tags

[edited by - ZedFx on May 12, 2004 10:43:37 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.