1

Assume I have a variable called Block_Size and without initialization.

Would

Block_Size db ? mov DS:Block_Size, 1 

be equal to

Block_Size db 1 
7
  • 3
    If you have questions about something so fundamental it's a sign you need a good x86 assembly reference guide. Commented Oct 6, 2017 at 19:58
  • db means 1 byte in space, mov ..., 1 means set value to 1 Commented Oct 6, 2017 at 20:02
  • What do you mean by Block_Size = 1? The most direct meaning (changing the numeric value of the label at runtime) is nonsensical, and usually that wouldn't be the intended meaning but in the context of this question it sort of looks like that might be what you meant. Commented Oct 6, 2017 at 20:07
  • To Harold: Ok, so let me paraphrase my question like this: "Block_Size db ?" and "mov DS:Block_Size, 1" together are equal to "Block_Size db 1" ? Commented Oct 6, 2017 at 20:14
  • OK that's more clear, but then what does "equal" mean precisely. Even if two ways of doing something have a similar result (usually), if they are fundamentally different ways to get there, can they really be called equal? Commented Oct 6, 2017 at 20:30

2 Answers 2

3

No, Block_Size db ? has to go in the BSS or data section, not mixed in with your code.

If you wrote

my_function: Block_Size db ? mov DS:Block_Size, 1 ... ret 

your code would crash. ? isn't really uninitialized, it's actually zeroed. So then the CPU decoded the instructions starting at my_function (e.g. after some other code ran call my_function), it would actually decode the 0 as code. (IIRC, opcode 0 is add, and then the opcode of the mov instruction would be decoded as the operand byte of add (ModR/M).)

Try assembling it, and then use a disassembler to show you how it would decode, along with the hex dump of the machine code.

db assembles a byte into the output file at the current position, just like add eax, 2 assembles 83 c0 02 into the output file.

You can't use db the way you declare variable in C

void foo() { unsigned char Block_size = 1; } 

A non-optimizing compiler would reserve space on the stack for Block_size. Look at compiler asm output if you're curious. (But it will be more readable if you enable optimization. You can use volatile to force the compiler to actually store to memory so you can see that part of the asm in optimized code.)

Maybe related: Assembly - .data, .code, and registers...?


If you wrote

.data Block_size db ? .code set_blocksize: mov [Block_size], 1 ret 

it would be somewhat like this C:

unsigned char Block_size; void set_blocksize(void) { Block_size = 1; } 

If you don't need something to live in memory, don't use db or dd for it. Keep it in registers. Or use Block_size equ 1 to define a constant, so you can do stuff like mov eax, Block_size + 4 instead of mov eax, 5.

Variables are a high-level concept that assembly doesn't really have. In asm, data you're working with can be in a register or in memory somewhere. Reserving static storage for it is usually unnecessary, especially for small programs. Use comments to keep track of what you put in which register.

Sign up to request clarification or add additional context in comments.

Comments

1

db literally stands for "define byte" so it will put the byte there, where the move command can have you place a particular value in a register overwriting whatever else was there.

1 Comment

Thank you for your reply. Do those codes are equal to "Block_Size db 1"? The DS:Block_Size just makes me confused. It can be understood as the address at (DS+Block_Size) has 1 as its content. Am I correct here?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.