doplumi
doplumi

Reputation: 3118

Assembly x86 Understanding Define Word (DF) instruction

I'm just starting to play a little bit with assembly so excuse me if my questions are dumb.

My questions are about this (I'm trying to divide 100 by 5, playing with memory and variables of course!)

https://dl.dropboxusercontent.com/u/78049918/Pics/Capture.png

Why does the DW (Define Word) instruction is executed like that? I mean, I understand all of the others line but that in particular is a dark one for me..

Also, can you explain to me why var is instantiated at the address 0010Ah ? I think it's because the 4 istructions before the instantiation occupy 4*16bits=4*2bytes=8bytes, and so var is filed on the stack right after. But shouldn't the instruction be elsewhere? Are they loaded on the stack once the program starts to run?

Bonus question: How can I check what's on the stack in emu8086?

Upvotes: 0

Views: 1490

Answers (1)

Tox1k
Tox1k

Reputation: 105

Because you are using an assembler, not a compiler. It is literally translating your mnemonics and data into a flat binary file (or more commonly, with headers included ie PE).

dw is quite literally "declaring a word". It's placing $0200 at that place in the code. If we look at this handy table, we can see the opcode for

add r8, r/m8

While I'm not going to go into the whole opcode structure and disassembly, you'll also see an [r] meaning it has a modrm byte following the opcode. Here's another handy table that shows the meaning of the modrm byte. We can see that $00 is [bx+si].

Assembling the whole instruction, we get

add ax, [ax+si]

(We get the ax from the 0 in the reg2 bits - ax is the first register indexed, cx 2nd, dx 3rd, bx 4th, etc). Here's a simple table from the docs in my assembler:

modrm:
0 0 0 0 0 0 0 0
| | |   | |___|
|_| |   |  reg1 
mod |___|  (r/m)
     reg2

ie: op reg2, reg1

This went a little in depth, but hopefully you understand that you are using an assembler, not a compiler, and a little bit about instruction encoding. Some things may be a little off (I'm used to 32bit).

This is the reason most executable formats have sections for data and code, or in 16bit segments "isolated" for each. In a flat binary you can just have a data section at the end or in blank areas where you jmp over it/between called functions.

Upvotes: 5

Related Questions