Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 堆栈的汇编x86实现_Assembly_X86 - Fatal编程技术网

Assembly 堆栈的汇编x86实现

Assembly 堆栈的汇编x86实现,assembly,x86,Assembly,X86,我需要在x86程序集中实现一个堆栈,所以我写了以下内容: section .bss my_stack: resb 5 但是在我继续我的程序后,这个地址中的数据消失了 有一种更好的方法可以实现堆栈????我不确定您想要什么,但是由于x86汇编语言提供了自己的堆栈,为什么不使用它呢 push reg ; push register reg to the stack pop reg ; pop register reg from the stack ; actu

我需要在x86程序集中实现一个堆栈,所以我写了以下内容:

section .bss

my_stack: 
       resb 5
但是在我继续我的程序后,这个地址中的数据消失了


有一种更好的方法可以实现堆栈????

我不确定您想要什么,但是由于x86汇编语言提供了自己的堆栈,为什么不使用它呢

push reg ; push register reg to the stack
pop reg  ; pop register reg from the stack
         ; actual stack pointer = sp

顺便说一句,您的代码只为变量保留了5字节的空间,要了解数据消失的原因,程序的其余部分会很有趣。在堆栈中仅使用5个字节也很奇怪。

下面是一个简单的示例,说明如何在x86 asm中创建自己的堆栈:

format pe console
entry start

include 'win32ax.inc' ;used for proc macros only

MAXSIZE = 256

section '.text' code readable executable

    start:
            ;test code
            push 12345
            call myPush
            push 22222
            call myPush
            call myPop
            call myPop
            ret

    proc myPush x
         cmp [_top], MAXSIZE        ;did we exceed stack size
         ja stack_full
         inc [_top]                 ;update the last element position
         mov eax, [_top]
         mov esi, _stack
         mov edx, [x]
         mov dword [esi+eax*4], edx ;write the value to stack
    stack_full:
         ;do something when stack is full
         ret
    endp

    proc myPop
         cmp [_top], 0              ;did we write anything previously
         jbe stack_empty
         mov eax, [_top]
         mov [_stack+eax*4], 0       ;clear stack value at last position
         dec [_top]                 ;decrease last element position
    stack_empty:
         ;do something when stack is empty
         ret
    endp

section '.data' data readable writeable
    _stack dd MAXSIZE dup ?
    _top dd ?  

我在这里使用了FASM语法,但这应该不是问题。此外,我建议在内存中分配堆栈,例如使用VirtualAlloc。

要使用堆栈,如果是16位,则必须正确设置ss:sp,否则(ss):esp。 设置ss:sp的首选方法是在同一条指令中加载ss和sp的LSS指令

section .bss

my_stack: 
       resb 5

section .text

setup_stack:
       lss sp, [my_stack]
       ; rest of your code

你的问题毫无意义;请改写它。我能说的唯一一件事是,你需要常规的PUSH和POP没有为你提供什么?实际上,LSS接收一个远指针,所以它有2个字节用于SS选择器,4或8个字节用于将偏移量放入ESP。