x86 ASM Linux - Using the .bss Section

nmagerko picture nmagerko · Nov 16, 2011 · Viewed 26.9k times · Source

I hope these questions is rather simple: (NASM Compiler, Linux, x86 Intel Syntax)

PART 1:

I am trying to figure out how to use the .bss section of an Assembly program to find a way to store values, like a value from an operation (+ - * /), to an declared variable. For example:

section .bss

variable:  resb 50                                       ;Imaginary buffer

section .text

add 10,1                                                 ;Operation
;move the result into variable

So, I know it is possible to do this with the kernel intterupt for reading user input (but that involves strings, but is there a way to copy this value into the variable variable so that it can be used later? This would be much easier than having to push and pop two things on and off the stack.

PART 2:

Is there a way to remove the value of the variable in the .bss section? In other words, if I want to store a new value in the .bss variable, how could I do it without the characters/values already in the variable not getting compounded with the new value(s)?

Thanks

Answer

Matthew Slattery picture Matthew Slattery · Nov 16, 2011
section .bss

variable: resb 4

... the symbol variable now refers to the address of 4 bytes of storage in the .bss section (i.e. enough to store a 32-bit value in).

section .text
...
mov eax, 123
mov [variable], eax

... sets the eax register to 123, and then stores the value of eax in the location addressed by the symbol variable.

mov eax, [variable]

... reads the value currently stored in the location addressed by variable into the eax register.

mov eax, 456
mov [variable], eax

... stores a new value, overwriting the previous one.