NASM .bss variable error "Access violation writing location 0x000000000000000C."

Viewed 34

I'm very much a newbie on assembly and NASM. I'm trying to define a variable in the .bss section and use it but I couldn't get it to run. It gives me this error:

"Access violation writing location 0x000000000000000C"

Here is my code:

section .bss
    var: resb 64
    
section .text
    global _start
    
_start:
    [BITS 64]
    mov qword [var],10

I tried all kind of things like mov rax, 10 and mov [var], rax and this kind of stuff but I couldn't get it to run.

1 Answers

What you are trying to do will not work.

This is because when writing shell code, there is no other section than the text section. It is not possible to define static variables and read/write them. You additionally run into the problem that shell code must be position independent, but nasm defaults to an absolute addressing mode for

[var]

To fix this, you need to write

[rel var]

choosing a rip-relative addressing mode or select such an addressing mode by default by issueing

default rel

Neverthless, there will not be a bss section at run time, so writing to a variable in it will not work. Instead, you could for example place variables on the stack like such:

sub rsp, 8
mov qword [rsp], 10
Related