flat assembler
Message board for the users of flat assembler.

Index > DOS > some help in c to asm please

Author
Thread Post new topic Reply to topic
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 19 Feb 2005, 15:02
I found some code in c++ and dont know what these functions would be in asm. How would I do these in asm? malloc, memset, and free.

Thanks

_________________
----> * <---- My star, won HERE
Post 19 Feb 2005, 15:02
View user's profile Send private message Reply with quote
S.T.A.S.



Joined: 09 Jan 2004
Posts: 173
Location: Ru#27
S.T.A.S. 24 Feb 2005, 23:44
It's possible to use C runtime library (like MSVCRT.DLL) for some functions:
Code:
   format  pe

;     hmm.. cdecl doesn't release the stack :-\   
  mov     ebp,esp

 push    read_mode
   push    source_name
 call    [fopen]
     test    eax,eax
     jz      not_found
   xchg    eax,esi

@@:  push    0
   mov     edi,esp

 push    esi
 push    1
   push    1
   push    edi
 call    [fread]
     test    eax,eax
     jz      close

   push    edi
 push    form
        call    [printf]

        mov     esp,ebp
     jmp     @b

close:    push    esi
 call    [fclose]

exit:       mov     esp,ebp
     ret

not_found:
        push       error_message
       call    [printf]
    jmp     exit

error_message       db "coudn't open file `%s'.",0 
form             db "%s",0
read_mode        db "r",0
source_name       db "crt.asm",0


align 4
data import

              dd 0,0,0,rva kernel_name,rva kernel_table
           dd 0,0,0,rva msvcrt_name,rva msvcrt_table
           dd 0,0,0,0,0

;   2K needs this to load PE
kernel_table:   dd 0

msvcrt_table:
fclose         dd rva fclose$
fread         dd rva fread$
fopen          dd rva fopen$
printf         dd rva printf$
              dd 0    

kernel_name     db 'KERNEL32.DLL',0
msvcrt_name    db 'MSVCRT.DLL',0
fclose$          db 0,0,'fclose',0
fread$           db 0,0,'fread',0
fopen$            db 0,0,'fopen',0
printf$           db 0,0,'printf',0

end data
    

However, in your case it's better to use Win32 APIs like HeapAlloc, HeapFree (in fact, CRT's functions just wrap arround them).
memset() can be substituted by simple macro:
Code:
macro memset dest, c, count
{
    mov  edi, dest
    mov  al, c
    mov  ecx, count
    rep   stosb
}    
Post 24 Feb 2005, 23:44
View user's profile Send private message Reply with quote
Dilshod



Joined: 23 Feb 2005
Posts: 23
Location: Uzbekistan, Tashkent
Dilshod 11 Mar 2005, 08:57
For DOS you must use Int 21h functions:
48h - memAlloc, Enter: BX- memSize(in parags), Exit: AX-Segment
49h - memFree, Enter: ES- Segment
4Ah - memset, Enter: BX-newsize(in paragraphs)
Example:
mov ah,48h
mov bx,100h ;size=256*16bytes
int 21h
jc Error
mov es,ax
...
mov ah,49h
int 21h

and do not forget for COM files befor MAlloc set the new size of current using of COM mem to that you need with 4Ah functions.
Post 11 Mar 2005, 08:57
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.