assembly NASM和对持久性有机污染物的澄清

fxnxkyjh  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(77)

我最近一直在学习汇编语言,正在编写非常简单的程序,只是为了看看事情是如何工作的,并更好地记住事情。
不管怎样,我决定简单地写一个程序来打印你的输入,我使用的是SASM和它的io宏。

%include "io.inc"

section .text
global CMAIN
CMAIN:
segment .data
    message db "Input a number", 0 
segment .bss
    input resb 4
    input2 resb 4
segment .text

    PRINT_STRING message ;prints message
    GET_UDEC 4, eax ;gets 4bytes of user input and puts into eax
    mov [input], eax ;moves eax into input's actual data
    PRINT_UDEC 4, input ;prints input
    NEWLINE
    PRINT_UDEC 4, esp ;Prints 2686764
    NEWLINE
    push input ;pushes input onto the stack
    PRINT_UDEC 4, esp ;Prints 2686760, we pushed a dw
    NEWLINE

    pop ebx ;pops (input) and puts it into ebx?
    mov [input2], ebx ;moves ebx into input2
    PRINT_UDEC 4, input2 ;prints input2. //Prints some crazy number.

    GET_UDEC 4, eax ;Stops program from exiting.

xor eax, eax
ret

我一定是错过了什么基本的东西,但我找不到它来保存我的命。有人知道吗?

6yt4nkrj

6yt4nkrj1#

你的代码有问题

在我回答你关于pop的问题之前,我想说的是,你应该做push dword [input]而不是push input,这样你就可以推送input的值而不是指向它的指针。

弹出和推送说明

在汇编中,有四种主要的方法与堆栈交互。push将第一个参数压入堆栈的顶部。pop从堆栈中取出顶部元素并将其放入第一个参数。pusha将所有寄存器压入堆栈。popa类似于pusha,但会弹出所有寄存器。
你也可以通过改变esp寄存器来改变堆栈的位置,例如:

mov esp, 0x100000
mov ebp, esp

这会将堆栈设置为内存位置0x100000。请注意,程序很少执行此操作,只有操作系统或系统执行此操作。

相关问题