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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
abcd efgh
  • 13
  • 1
  • 4
  • 3
    If you have questions about something so fundamental it's a sign you need a good x86 assembly reference guide. – tadman Oct 06 '17 at 19:58
  • `db` means 1 byte in space, `mov ..., 1` means set value to 1 – Iłya Bursov Oct 06 '17 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. – harold Oct 06 '17 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" ? – abcd efgh Oct 06 '17 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? – harold Oct 06 '17 at 20:30
  • Are you sure the two ways will give the same result? I think we should use "mov [Block_Size], 1" rather than "mov DS:Block_Size,1" to assign value 1 to Block_Size. Am I correct here? – abcd efgh Oct 06 '17 at 20:44
  • In MASM syntax, those are equivalent. `DS:` is the default segment for absolute addresses, and for base registers other then EBP/ESP or RBP/RSP. (You can mostly ignore segment stuff unless you're writing 16-bit code. The defaults works fine, and normal programs for Windows or Linux never do anything with segments (except thread-local storage, but that's only relevant if you don't use it.)) – Peter Cordes Oct 06 '17 at 21:09

2 Answers2

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
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.

  • 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? – abcd efgh Oct 06 '17 at 20:23