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

mmap2函数在asm中写入,在c中调用,c,assembly,mmap,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

我在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; }
.data
MMAP2=192
MUNMAP=91
PROT_READ=0x1
MAP_ANONYMOUS=0x20
.bss
.文本
.global moje_mmap
.type moje_map@函数
moje_mmap:
推送%ebp
移动%esp,%ebp
异或%ebx,%ebx
mov 8(%ebp),%ecx
mov$PROT_读取%edx
mov$MAP\U匿名,%esi
mov$-1,%edi
mov$MMAP2,%eax
整型$0x80
mov%ebp,%esp
弹出%ebp
ret
#包括
#包括
#包括
#包括
void*moje_mmap(尺寸);
int main(){
moje_mmap(30);
返回0;}

实际上,您正在从汇编函数正确返回值-22是来自mmap2的有效返回值,表示EINVAL。当您直接从程序集使用系统调用时,例如-EINVAL或-22

现在,关于为什么会出现错误,这里有一段摘录:

查看您的代码,您正在传递-1作为偏移量参数,但这是有效的,所以这不是问题所在

问题更可能出在
标志
参数上:

   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。添加此项,您的程序应该可以运行。

查看此页上的相关链接以获取其他链接,然后查看和。谢谢帮助,我刚刚编写了一些代码,但我不知道这是否好。。。最后,我的函数返回-22到%eax,现在我不知道它是好是坏,你能告诉我一些关于这方面的事情吗?我在主要帖子中更改了代码。使用strace调试您的程序。我尝试使用,但我在linux和asm上非常弱,说真的,我不知道如何使用它,即使我阅读了堆栈上的一些主题。我使用gdb,返回的函数是-22。
   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.