Function works with buffers on the stack but not in .data or .bss sections? [duplicate]

折月煮酒 提交于 2021-02-05 12:21:04

问题


I have the following code:

global _start
extern GetStdHandle, WriteConsoleA ;kernel32.dll

section .data:
    buf times 12 db 0

section .text:
_uint32_to_string:
;void uint32_to_string(uint32_t n, char* buffer)
;buffer: 11 bytes (minimum)
    push ebp
    push ebx
    push esi
    push edi
    mov ebp, esp
    mov ebx, [ebp + 24]
    xor edi, edi
    mov eax, [ebp + 20]
    mov ecx, 10
CREATE_UINTEGER_STRING:
    xor edx, edx
    div ecx
    add dl, '0'
    mov [ebx + edi], dl
    inc edi
    cmp eax, 0
    jne CREATE_UINTEGER_STRING
    xor esi, esi
REVERSE_STRING_UNSIGNED:
    mov ecx, 2
    mov eax, edi
    xor edx, edx
    div ecx
    cmp esi, eax
    jnl REVERSE_UNSIGNED_STRING_END
    mov eax, ebx
    add eax, edi
    sub eax, esi
    dec eax
    mov cl, [ebx + esi]
    mov dl, [eax]
    mov [ebx + esi], dl
    mov [eax], cl
    inc esi
    jmp REVERSE_STRING_UNSIGNED
REVERSE_UNSIGNED_STRING_END:
    mov byte [ebx + edi], 0
    mov esp, ebp
    pop edi
    pop esi
    pop ebx
    pop ebp
    ret

_string_length:
;uint32_t string_length(const char* s)
    push ebp
    mov ebp, esp
    mov ecx, [ebp + 8]
    xor eax, eax
GET_STRLEN:
    cmp byte [ecx + eax], 0
    je END_GET_STRLEN
    inc eax
    jmp GET_STRLEN
END_GET_STRLEN:
    mov esp, ebp
    pop ebp
    ret

_start:
    mov ebp, esp
    mov eax, 235 ;here is the number to be printed
    lea ebx, [buf] ;it works if instead of this line I write:
    ; sub esp, 12
    ; mov ebx, esp
    push ebx
    push eax
    call _uint32_to_string
    add esp, 8
    push ebx
    call _string_length
    add esp, 4
    mov edi, eax
    push dword -11
    call GetStdHandle
    push dword 0
    push dword 0
    push edi
    push ebx
    push eax
    call WriteConsoleA
    mov esp, ebp
    xor eax, eax
    ret

I am using this program to print a number on the console screen. The problem is that if, for some reason, I choose to pass a buffer allocated in .data to the _uint32_to_string function, the program gives me a runtime error and exits with a gibberish exit code. If I try to define the buffer in the .bss section like the following, NASM gives me a warning (and the program still exits with garbage status if I run it):

section .bss:
    buf resb 12
;error: print_program.asm:5: warning: uninitialised space declared in non-BSS section `.bss:': zeroing

Luckily, if I allocate the buffer on the stack the program works correctly, prints the number and exits with status zero.

Can anybody please help me understand why the program works with buffers allocated on the stack but not with .data or .bss ones?

P.S: If it matters, here is how I compile my code (platform: Windows 10 32-bit):

nasm -f win32 -o print_program.obj print_program.asm
golink print_program.obj /console /entry _start kernel32.dll

回答1:


So, thanks to @fuz, there shouldn't be any colons after the sections. That means section .data: should be section .data. Same for .bss and .text.



来源:https://stackoverflow.com/questions/60996729/function-works-with-buffers-on-the-stack-but-not-in-data-or-bss-sections

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!