'Allocate a vector of bytes on stack in assembly x86

I try to put on stack the values contained by array_output but every time I print the values from the stack it prints just 0s.

If I try to print the array simple not using the stack it works. What I do wrong?

stack_alloc:
    sub esp, 1
    mov al, byte [array_output + ecx]
    mov byte [esp], al
    add ecx, 1
    cmp ebx, ARRAY_OUTPUT_LEN
    cmp ebx, ecx
    jg stack_alloc


Solution 1:[1]

cmp ebx, ARRAY_OUTPUT_LEN
cmp ebx, ecx
jg stack_alloc

You have a typo in your code. The cmp ebx, ARRAY_OUTPUT_LEN instruction should not be comparing but rather loading the EBX register.
You could correct the problem replacing the cmp with a mov but I would propose to simplify your code and just compare the index in ECX to ARRAY_OUTPUT_LEN. This will require choosing the opposite conditional branch and saves from using the additional register EBX:

    xor ecx, ecx
stack_alloc:
    sub esp, 1
    mov al, [array_output + ecx]
    mov [esp], al
    inc ecx
    cmp ecx, ARRAY_OUTPUT_LEN
    jb  stack_alloc

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sep Roland