我在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; }

最佳答案

您实际上是从汇编函数正确返回值。 -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作为offset参数,但这是有效的,所以这不是问题。

问题很可能与您的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。添加它,您的程序应该可以运行。

10-08 11:01