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

mmap2 function write in asm, call in c

我在 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 [=10=]x80
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; }

您实际上 return 从您的汇编函数中正确地输入了值。 -22 是来自 mmap2 的有效 return 值,表示 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.

如此处所述,您 必须 在标志参数中包含 MAP_SHARED 或 MAP_PRIVATE。添加这个,你的程序应该可以运行。