what is the x86 opcode for assembly variables and constants?

1.5k Views Asked by At

i know that every instruction has opcodes.
i could find opcodes for mov , sub instructions.
but what is the opcode for variables and it's types.
we use assembler directives to define a variable and constant?
how they are represented in x86 opcodes?

nasm assembler x86:

segment .bss 
largest resb 2  ; reserves two bytes for largest
segment .data number1 DW 12345 ; defines a constant number1

i tried online this https://defuse.ca/online-x86-assembler.htm#disassembly assembly to opcode conveter. but when i used nasm code to define a variable it shows error!

2

There are 2 best solutions below

0
fuz On

Assembler directives like segment, resb, or dw are not instructions and do not correspond to opcodes. That's why they are directives instead of instructions. Roughly speaking, there are two kinds of directives:

  • one kind of directive configures the assembler. For example, the segment directive configures the assembler to continue assembly in the section you provided.
  • another kind of directive emits data. For example, the dw directive emits the given datum into the object file. This can be used to place arbitrary data into memory for use with your program.
3
Ped7g On

There's no opcode for variables. There are even no variables in machine code.

There is CPU and memory. Memory contains some values (bytes).

The CPU has cs:ip instruction pointer, pointing to memory address, where is the next instruction to execute, so it will read byte(s) from that address, and interpret them as opcode, and execute it as an instruction.

Whether you have in memory stored data or machine code doesn't matter, both are byte values.

What makes part of memory "data" or "variable" is the logical interpretation created by the running code, it's the code which does use certain part of memory only as "data/variables" and other part of memory as "code" (or eventually as both at the same time, like in this DOS 51B long COM code drawing Greece flag on screen, where the XLAT instruction is using the code opcodes also as source data for blue/white strips configuration).

Whether you write in your source:

x:
    add     al,al

or

x:
    db 0x00, 0xC0

Doesn't matter, the resulting machine code is identical (in both cases the CPU will execute add al,al when pointed to that memory to be executed as instruction, and mov ax,[x] will set ax to 0xC000 in both cases, when used as "variable".

You may want to check listing file from the assembler (-l <listing_file_name> command line option for nasm) to see yourself there's no way to tell which bytes are code and which are data.