86内存访问段错误 [英] x86 memory access segmentation fault

查看:184
本文介绍了86内存访问段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习x86汇编出于好奇。我目前使用的NASM汇编一个基于Linux的操作系统。我有困难的时候理解为什么

I am learning x86 assembly out of curiosity. I'm currently using a Linux based OS with the NASM assembler. I am having a difficult time understanding why

SECTION .text

global _start

_start:

    nop
    mov ebx, 25
    mov [0xFFF], ebx

   ;Exit the program
   mov eax, 1
   mov ebx, 0
   int 0x80

将导致一个分段故障(移动EBX寄存器的内容传送到存储单元0xFFF的时)。我在想,在纯ASM建设计划会给我我进程的虚拟地址空间不受限制的访问。这是不是这样的?

Would lead to a segmentation fault (when moving the contents of the ebx register to memory location 0xFFF). I was thinking that building a program in pure asm would give me unrestricted access to my process' virtual address space. Is this not the case?

您将如何实现像在装配堆?

How would you implement something like a heap in assembly?

推荐答案

在Linux(x86)的 - 尽管你有4GB的虚拟地址范围在你的过程中,不是所有的访问。上部1GB的是内核所在,并有不能使用的低存储器的区域。虚拟内存地址0xFFF的不能写入或读取(默认),这样你的程序崩溃,段错误。

On Linux(x86) - although you have a virtual address range of 4gb in your process, not all of it is accessible. The upper 1gb is where the kernel resides, and there are areas of low memory that can't be used. Virtual memory address 0xfff can't be written to or read from (by default) so your program crashes with a segfault.

在一个后续评论你建议你打算采用汇编堆。可以做,一个方法是使用的 sys_brk 系统调用。它是通过 0x80的INT EAX 的= 45访问。它需要一个指针的 EBX 的再presenting堆新的顶部。一般堆区域的底部被初始化为区域,就你的程序的数据段(你在内存中的程序段)。为了获得初始堆位置的地址,你可以调用的 sys_break 的用的 EBX 的设置为0的系统调用后的 EAX 的将是目前基地堆的指针。当您需要访问您的堆内存或分配更多的堆空间可以保存了。

In a followup comment you suggested you were intending to create a heap in assembler. That can be done, and one method is to use the sys_brk system call. It is accessed via int 0x80 and EAX=45 . It takes a pointer in EBX representing the new top of the heap. Generally the bottom of the heap area is initialized to the area just beyond your programs data segment(above your program in memory). To get the address of the initial heap location you can call sys_break with EBX set to 0. After the system call EAX will be the current base pointer of the heap. You can save that away when you need to access your heap memory or allocate more heap space.

这code,可明确(不是性能)的目的的例子,但可能是一个出发点,以了解如何可以操纵堆区:

This code provides an example for purposes of clarity (not performance), but might be a starting point to understanding how you can manipulate the heap area:

SECTION .data
heap_base: dd 0          ; Memory address for base of our heap

SECTION .text
global _start
_start:
    ; Use `brk` syscall to get current memory address
    ; For the bottom of our heap This can be achieved
    ; by calling brk with an address (EBX) of 0
    mov eax, 45          ; brk system call
    xor ebx, ebx         ; don't request additional space, we just want to 
                         ; get the memory address for the base of our processes heap area.
    int 0x80
    mov [heap_base], eax ; Save the heap base

    ;Now allocate some space (8192 bytes)
    mov eax, 45          ; brk system call
    mov ebx, [heap_base] ; ebx = address for base of heap
    add ebx, 0x2000      ; increase heap by 8192 bytes
    int 0x80

    ; Example usage
    mov eax, [heap_base]      ; Get pointer to the heap's base
    mov dword [eax+0xFFF], 25 ; mov value 25 to DWORD at heapbase+0xFFF

    ;Exit the program
    mov eax, 1
    xor ebx, ebx
    int 0x80

这篇关于86内存访问段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆