开发者

Defining labels in .com files before the code itself

开发者 https://www.devze.com 2023-02-11 19:11 出处:网络
x1 dw 7 x开发者_开发百科2 dw 6 org 100h mov ax,[x1] mov bx,[x2] call calc_mod (calcs the modolu) mov ah,4Ch
x1 dw 7
x开发者_开发百科2 dw 6

org 100h
mov ax,[x1] 
mov bx,[x2]
call calc_mod (calcs the modolu)
mov ah,4Ch
int 21h

and executed it as .com file and got weird results, is it because I have defined the labels before the actual code? is it allowed?

thanks!


Being 20 years i built .com, I may be wrong but don't believe they start at 0x100, that's for .exe.

So without jmp 0x100 at 0x0000 you'll get wierd results..

regards,

//t


Contrary to popular belief, the org directive does not tell the assembler where to put code. It tells the assembler what to assume the instruction pointer (IP) is at that point. Many x86 instructions use relative addresses, so the assembler needs to know the code's offset within a segment.

When DOS loads a .COM file, it puts a program segment prefix at 0000, and loads your code at 0100, which is also the address it initializes IP.

To illustrate the point about org:

if you do this

org  100H
call 123H

you'll get a call to address 123H as you expect:

0CD3:0100 E82000        CALL    0123   ; call IP + 20H   (IP = 100H + 3)

But if you do this

org  105h
call 123h

then when you look at the disassembly you'll see you don't get a call to 0123H, despite the call 123H right in front of your eyes in the assembly text. Instead, you'll get

0CD3:0100 E81B00        CALL    011E   ; call IP + 1BH

The reason for the discrepancy is that when you wrote org 105H, you essentially lied to the assembler—you told it to assume the code was at 105H, when in fact DOS is going to load the code at 100H regardless. That's why the offset in the call instruction is off by 5 bytes in the second example.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号