移植c联合和结构到nasm程序集

时间:2015-10-15 19:48:15

标签: c nasm

我有一个内存分配器的c代码。我正在研究ubuntu 64bit。

typedef long Align ;
/* for alignment to long boundary */

union header {
    struct {
   union header * ptr ; /* next block if on free list */
   unsigned int size ; /* size of this block */
   } s;
   Align x;
 /* force alignment of blocks */
};

我如何将其移植到nasm程序集?

typedef union header Header ;

cp = sbrk ( nu * sizeof ( Header ));
if ( cp == ( char *) -1) { /* no space at all */
    return NULL ;
}
up = ( Header *) cp ;
up ->s. size = nu ;

我正在努力移植上面的代码,特别是访问union和structs

 u->s.size = nu.

汇编代码:

NALLOC equ 1024

%define nu [ebp+8] ;input parameter
%define cp_ptr [ebp-4] ;local varialbe char pointer cp 
%define up_ptr [ebp-8] ;local variable Header pointer up
%define size [ebp-12] ;local variable Header pointer up

morecore:
  push  ebp
  mov   ebp, esp
  sub esp, 12 ;local variables space

  cmp nu, NALLOC
  jge endif1
  mov nu, NALLOC
endif1:
   mov  eax, 45      ;sys_brk or 0x2d
   xor  ebx, ebx
   int  0x80

   imul size, nu, 16 ;multiply nu by 16 and store in size
   add  eax, size    ;number of bytes to be reserved
   mov  ebx, eax
   mov  eax, 45      ;sys_brk is number 45 
   int  0x80         ;call the system cal

   mov cp_ptr,eax
   cmp cp_ptr, 0 ; if sys_brk returns -1 then 
   jl  exit
   mov up_ptr, cp_ptr 
   mov ; 


exit:

  mov   esp, ebp
  pop   ebp
  ret

0 个答案:

没有答案