简体   繁体   English

组装MASM在memory上保存动态数组的正确方法

[英]Assembly MASM save dynamic array on memory correct way

Hello I want to save in memory an array of length and values defined by the users.您好,我想在 memory 中保存用户定义的长度和值数组。 Actually I'm storing the values inserted at "[ebp+8+esi*4]", so after the function arguments.实际上我存储在“[ebp+8+esi*4]”处插入的值,所以在 function arguments 之后。 Is that correct or what should I do?这是正确的还是我应该怎么做? I though that local variables should placed at memory address below esp, so make space for local variables by decrementing esp.虽然我认为局部变量应该放在 esp 下面的 memory 地址,所以通过递减 esp 为局部变量腾出空间。 But I can't dynamically decrement "esp", so how should I do?但是我不能动态递减“esp”,那我该怎么办呢?

This is the code:这是代码:

.686
.xmm
.model flat, C
OPTION CaseMap:None

include ../masm32/libs/windows.inc
include ../masm32/libs/kernel32.inc
include ../masm32/libs/user32.inc
include ../masm32/libs/msvcrt.inc
include ../masm32/libs/masm32.inc

EXTERN printf:PROC
EXTERN scanf:PROC

.data
    str_insert_array_length db  "Insert Array Length: ",0
    str_insert_value    db  10,"Insert Value: ",0
    str_input_format    db  "%d",0
    str_format_print db "%d",10,0

    input_length   DWORD    0
    input_value    DWORD    0

.code

main PROC
    push ebp
    mov ebp,esp

    push offset str_insert_array_length
    call printf
    add esp,4

    push offset input_length
    push offset str_input_format
    call scanf
    add esp,8
    
    xor edi,edi

get_array_values:

    push offset str_insert_value
    call printf
    add esp,4

    push offset input_value
    push offset str_input_format
    call scanf
    add esp,8

    mov eax,4
    mul edi
    sub esp,eax
    mov eax,[input_value]
    mov [esp], eax
    mov eax,[input_length]

    inc edi
    cmp edi,eax
    jne get_array_values


    mov edi,[input_length]
    xor esi,esi

print_array:
    mov edx,[esp+4*esi]
    push edx
    push offset str_format_print
    call printf
    add esp,8
    inc esi
    dec edi
    jnz print_array


    pop ebp
    invoke ExitProcess,0
main ENDP
    end

You can dynamically decrement sp.您可以动态递减 sp。 Just use a subtract instruction, like sub esp, eax , where you have established the amount of dynamic decrement in eax .只需使用减法指令,例如sub esp, eax ,您可以在其中建立eax中的动态减量量。 If you do that the the new array starts at the address in esp .如果这样做,新数组将从esp中的地址开始。

The problem is that you can't compute the decrement amount until you know how many items are being input.问题是在您知道输入了多少项目之前,您无法计算递减量。

So.所以。 If you don't know how many items there will be until the user says to stop, then you can push individual items onto the stack as the are entered, but the resulting array will have the elements backward in nemory order — so, you can later either reverse them in place with a simple loop, or instead just choose to work with the backwards order, if the rest of the program is not too complicated.如果在用户说停止之前您不知道会有多少项目,那么您可以在输入时将单个项目推入堆栈,但生成的数组将使元素按 nemory 顺序向后 - 所以,您可以如果程序的 rest 不是太复杂,则稍后可以通过简单的循环将它们反转到位,或者只是选择使用倒序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM