4

I am trying to get a basic feel for assembly, here is my code:

section .text
   global _start     ;must be declared for linker (ld)

section  .bss
   num resb 5

_start:             ;tells linker entry point
   mov  edx,len     ;message length
   mov  ecx,msg     ;message to write
.
.
.

The program does not compile with the error message "warning: attempt to initialize memory in BSS section `.bss': ignored ".

I did not find helpful answers on SO, can someone tell me what's wrong?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

2 Answers2

2

Your section .bss needs to be after or before your text section. What you are doing right now is putting you code in the bss section. Instead, you should do:

section .rodata
    msg: db "Hello"
    len: equ $-msg

section .bss
    num resb 5

section .text
    global _start

_start:
    mov ecx, msg
    mov edx, len
.
.
.
Markian
  • 322
  • 1
  • 12
  • Correct, but `“` isn't an ASCII quote or double-quote. NASM won't assemble it. Whatever you're using to write code blocks for Stack Overflow, make sure you're using ASCII quote characters. You posted this within the same hour as fixing that bug in [AND vs SUB when converting lowercase to uppercase assembly](https://stackoverflow.com/q/75099141)! Also, typically you'd want a string constant in `section .rodata` (Linux) or `section .rdata` (Windows); no need to have it in a writeable page. – Peter Cordes Jan 14 '23 at 03:14
2

I had a bit of a specific problem, but I will post it in case it helps someone.

I declared the following struct:

struc ctx_t
    .next: resd 1
    .prev: resd 1
    .a:    resd 1
    .c:    resd 1
endstruc

And after some time I added a .b member:

struc ctx_t
    .next: resd 1
    .prev: resd 1
    .a:    resd 1
    .b:    resd 1
    .c:    resd 1
endstruc

But forgot to edit the actual definition of the struct on another file:

section .bss

    first_ctx:
        istruc ctx_t
            at ctx_t.next, resd 1
            at ctx_t.prev, resd 1
            at ctx_t.a,    resd 1
            ; Missing b!
            at ctx_t.c,    resd 1
        iend

And the assembler was giving me these warnings (all on the ctx_t.c line):

src/file.asm:9: warning: attempt to initialize memory in BSS section `.bss': ignored [-w+other]
src/file.asm:9: warning: attempt to initialize memory in BSS section `.bss': ignored [-w+other]
src/file.asm:9: warning: attempt to initialize memory in BSS section `.bss': ignored [-w+other]
src/file.asm:9: warning: attempt to initialize memory in BSS section `.bss': ignored [-w+other]
trxgnyp1
  • 317
  • 1
  • 10
  • This is a bit of a different feature than the question is about, despite the similar warning. Yours is because `istruc` expects to be used in a `progbits` section (unlike the `nobits` BSS). By filling out the entire `istruc` with `resd` directives you can avoid `istruc` emitting `db` data into the `nobits` space. I think you could make this a new question + answer. – ecm Aug 29 '23 at 17:57
  • @ecm So if I understood correctly, by omitting `at ctx_t.b, resd 1` it's basically declaring a dword filled with zeros? I understand this is wrong in `.bss`, but I want to make sure I understand what you mean. – trxgnyp1 Aug 29 '23 at 21:07
  • 1
    You can [look at the macros](https://github.com/netwide-assembler/nasm/blob/a916e4127b2eaa3bf40bddf3de9b0ceefc0d98a4/macros/standard.mac#L107) that NASM uses by default. (You can even redefine them.) `at` and `iend` use a `times ... db 0`. In your case this evaluates to `times 4 db 0`, hence 4 warnings. To avoid the warning it should use `resb 4` or `times 4 resb 1` if in a `nobits` section. – ecm Aug 30 '23 at 04:57
  • That is very interesting. I will follow your advice and create a new question. – trxgnyp1 Aug 30 '23 at 12:08
  • Done: [Link](https://stackoverflow.com/questions/77008013/nasm-warning-when-defining-macro-attempt-to-initialize-memory-in-bss-section/77008014). – trxgnyp1 Aug 30 '23 at 12:25