首页 > 解决方案 > RESB:声明和填充未初始化的数据

问题描述

我有这个代码:

    extern  printf      ; the C function, to be called

SECTION .bss
    array resb 10

SECTION .data       ; Data section, initialized variables

    data times 10 db 0
    a:   dd 5       ; int a=5;
    fmt: db "a=%d, eax=%d", 10, 0 ; The printf format, "\n",'0'


SECTION .text                   ; Code section.

global main     ; the standard gcc entry point

    mov eax, 0
    mov ebx, 0

print:
    push    ebp     ; set up stack frame
    mov     ebp,esp
    inc ebx
    mov     eax, [array + ebx]  ; put a from store into register
    ;add     eax, 1
    push    eax     ; value of a+2 FIRST
    push    dword [a]   ; value of variable a SECOND
    push    dword fmt   ; address of ctrl string
    call    printf      ; Call C function
    add     esp, 12     ; pop stack 3 push times 4 bytes

    mov     esp, ebp    ; takedown stack frame
    pop     ebp     ; same as "leave" op

    mov     eax,0       ;  normal, no error, return value

    ret         ; return

main:               ; the program label for the entry point

    call print
    ret

我需要array为 10 个字节创建一个。然后我想填补这段记忆,我试着做一些这样的想法:

int array[10];
for(int i = 0; i < 10; i++)
    array[i] = i;

我是否正确理解这resb是一个静态字节(变量)?这些数据存在于程序的整个生命周期中。

标签: arraysassemblynasm

解决方案


推荐阅读