How does malloc in assembly 8086 work? : <p>I'm doing C and assembly ,I thought i'll write a simple code to make sure that i understand how using malloc in assembly works ,I just want to allocate a size 4 int array and fill it with 1,2,3,4 values in assembly and then print in C.</p>

<p>C file:</p>

<pre><code>#include &lt;stdio.h&gt; 
#include &lt;stdlib.h&gt; 
#include &lt;string.h&gt;
#include &lt;malloc.h&gt;
extern void myproc(int *arr); 
void main() {
int *p;
myproc(p);//call the assembly function ,where i do p=malloc(sizeof(int)*4)
printf("%d %d %d %d ",p[0],p[1],p[2],p[3]);//supposed to print 1 2 3 4 but its not
} // main
</code></pre>

<p>Assembly file:</p>

<pre><code>.MODEL small
    .STACK 100H
    .DATA
    .CODE
    .386
    .387
    PUBLIC _myproc
    EXTRN _malloc:NEAR
    _myproc PROC NEAR
    ;This procedure is supposed to do  p=malloc(sizeof(int)*4) then p[0]=1 ,p[1]=2,p[2]=3,p[3]=4.
    PUSH BP
    MOV BP,SP
    XOR DX,DX
    PUSH BX
    MOV AX,4;prepare to pass the parameter for malloc
    SHL AX,1;multiply by 2 for int array
    PUSH AX;pass 4*2 to malloc
    CALL _malloc;calling malloc
    ADD SP,2
    MOV WORD PTR [BP+4],AX;set the pointer to the newly allocated array 
    MOV BX,WORD PTR [BP+4]
    ;do p[0]=1 ,p[1]=2,p[2]=3,p[3]=4
    MOV WORD PTR [BX],1
    ADD BX,2
    MOV WORD PTR [BX],2
    ADD BX,2
    MOV WORD PTR [BX],3
    ADD BX,2
    MOV WORD PTR [BX], 4
    POP BX
    POP BP
    RET;;when i get back to the C file to print the values it doesnt work ,its like i didn't set 1,2,3,4.
    _myproc ENDP

    END
</code></pre>
