x86 ASM Linux - 使用 .bss 部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8145243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 21:42:40  来源:igfitidea点击:

x86 ASM Linux - Using the .bss Section

linuxassemblyx86nasm

提问by nmagerko

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

我希望这些问题相当简单:(NASM Compiler、Linux、x86 Intel Syntax)

PART 1:

第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:

我想弄清楚如何使用汇编程序的 .bss 部分来找到一种方法来存储值,例如来自操作 (+ - * /) 的值到声明的变量。例如:

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 variablevariable 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:

第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)?

有没有办法删除 .bss 部分中变量的值?换句话说,如果我想在 .bss 变量中存储一个新值,如果变量中已有的字符/值没有与新值混合,我该怎么做?

Thanks

谢谢

采纳答案by Matthew Slattery

section .bss

variable: resb 4

... the symbol variablenow refers to the address of 4 bytes of storage in the .bsssection (i.e. enough to store a 32-bit value in).

...该符号variable现在指的是该.bss部分中 4 个字节的存储地址(即足以存储 32 位值)。

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

... sets the eaxregister to 123, and then stores the value of eaxin the location addressed by the symbol variable.

... 将eax寄存器设置为123,然后将 的值存储eax在符号寻址的位置variable

mov eax, [variable]

... reads the value currently stored in the location addressed by variableinto the eaxregister.

... 将当前存储在地址variable为 的位置中的值读取到eax寄存器中。

mov eax, 456
mov [variable], eax

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

... 存储一个新值,覆盖前一个值。