mmap2函数在asm中写入,在c中调用

时间:2015-04-26 21:07:36

标签: c assembly mmap

我在ASM AT& T中编写MMAP2并在C中调用它有问题。我写了这个,但不知道它应该如何工作。我知道代码不好但我需要帮助  你能告诉我它应该怎么样? 感谢帮助!

.data

MMAP2 = 192
MUNMAP = 91 
PROT_READ = 0x1 
MAP_ANONYMOUS = 0x20

.bss 
.text


.global moje_mmap
.type moje_map @function 
moje_mmap:

push %ebp           
mov %esp, %ebp          
xor %ebx, %ebx 
mov 8(%ebp), %ecx       
mov $PROT_READ, %edx 
mov $MAP_ANONYMOUS, %esi 
mov $-1, %edi

mov $MMAP2, %eax        
int $0x80
mov %ebp, %esp 
pop %ebp


ret                 

#include <stdlib.h> 
#include <stdio.h>
#include <sys/types.h> 
#include <sys/mman.h>

void* moje_mmap(size_t dlugosc);

int main() {

moje_mmap(30);

return 0; }

1 个答案:

答案 0 :(得分:0)

您实际上是从汇编函数中正确返回值。 -22是mmap2的有效返回值,表示EINVAL。直接从汇编errors are usually returned as the negative version of the error使用系统调用时,例如-EINVAL或-22。

现在,关于您收到错误的原因,这里有一段摘录from the mmap2 man page

   EINVAL (Various platforms where the page size is not 4096 bytes.)
          offset * 4096 is not a multiple of the system page size.

查看你的代码,你传递了-1作为偏移参数,但这是有效的,所以它不是问题。

问题更可能出在您的flags参数:

   The flags argument determines whether updates to the mapping are
   visible to other processes mapping the same region, and whether
   updates are carried through to the underlying file.  This behavior is
   determined by including exactly one of the following values in flags:

   MAP_SHARED Share this mapping.  Updates to the mapping are visible to
              other processes that map this file, and are carried
              through to the underlying file.  (To precisely control
              when updates are carried through to the underlying file
              requires the use of msync(2).)

   MAP_PRIVATE
              Create a private copy-on-write mapping.  Updates to the
              mapping are not visible to other processes mapping the
              same file, and are not carried through to the underlying
              file.  It is unspecified whether changes made to the file
              after the mmap() call are visible in the mapped region.

如此处所述,您必须在flags参数中包含MAP_SHARED或MAP_PRIVATE。添加它,你的程序应该可以工作。

相关问题