funny asm instruction ?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
asmboozer

funny asm instruction ?

Post by asmboozer »

hi all,

for a far jump instruction,

Code: Select all

...
jmp 0x08:label1

label1:
ret
when the instruction executed at jmp 0x08:label1, assuming the running cs is 0x10, and codes is executed at 0x10:offsetX, now the label1 has the value:offsetX+far jmp length.

since it's far jump,the jump address is 0x08<<4 + offsetX + length of far jmp instruction.

my question is, when step into jmp 0x08:label1, where it really jmp to? 0x08<<4 + offsetX + length of far jmp instruction or 0x10 << 4 + length of far jmp instuction?

why the answer is this or that??

thanks.
[small]<Pype> reformated for readability</Pype>[/small]
AR

Re:funny asm instruction ?

Post by AR »

Those are most likely GDT Segment Selectors, when you jump to it the offset is whatever it is set to in the GDT Entry.

If it is just a real mode segment change (and I wouldn't know why you'd want to change there) then it's Segment<<4 + Offset, the length of the instruction isn't needed for jumps.
asmboozer

Re:funny asm instruction ?

Post by asmboozer »

AR wrote: Those are most likely GDT Segment Selectors, when you jump to it the offset is whatever it is set to in the GDT Entry.

If it is just a real mode segment change (and I wouldn't know why you'd want to change there) then it's Segment<<4 + Offset, the length of the instruction isn't needed for jumps.
segment << 4 + offsetX is location of the jmp instruction
label1 's offset is offsetX + how many bytes of the jmp instruction occupies(i see five bytes the far jmp needs).

the codes is loaded into memory,
0x10<<4 + offsetX is in the loaded memory.

how about
0x08<<4 + offsetX +5(the length of far jmp)?


will the jump instruction jump to the label1 followed?

if it jumps to the followed lable1, it is 0x10<<4+offsetX+5,
not 0x08<<4 +offsetX+5.

right or not? why?
thanks.
asmboozer

Re:funny asm instruction ?

Post by asmboozer »

AR wrote: Those are most likely GDT Segment Selectors, when you jump to it the offset is whatever it is set to in the GDT Entry.

If it is just a real mode segment change (and I wouldn't know why you'd want to change there) then it's Segment<<4 + Offset, the length of the instruction isn't needed for jumps.
yes it's related to GDT. i saw such codes in a GDT tutorial.

the codes is

; This will set up our new segment registers. We need to do
; something special in order to set CS. We do what is called a
; far jump. A jump that includes a segment as well as an offset.
; This is declared in C as 'extern void gdt_flush();'
global _gdt_flush ; Allows the C code to link to this
extern _gp ; Says that '_gp' is in another file
_gdt_flush:
lgdt [_gp] ; Load the GDT with our '_gp' which is a special pointer
mov ax, 0x10 ; 0x10 is the offset in the GDT to our data segment
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
jmp 0x08:flush2 ; 0x08 is the offset to our code segment: Far jump!
flush2:
ret


i could not understand it yet.
Guest

Re:funny asm instruction ?

Post by Guest »

If it's related to the GDT, then you are not doing any bit shifting of segment bases. The segment value (0x8, 0x10) is no longer a base address but rather a selector for a descriptor (i.e. an index to a data structure) that contains a base address. It is this base address, added to the offset, that you are jumping to.
asmboozer

Re:funny asm instruction ?

Post by asmboozer »

Guest wrote: If it's related to the GDT, then you are not doing any bit shifting of segment bases. The segment value (0x8, 0x10) is no longer a base address but rather a selector for a descriptor (i.e. an index to a data structure) that contains a base address.

It is this base address, added to the offset, that you are jumping to.

after added it's 0x08+offsetX+5?and jump to it?

or if it's the base address, the label would be in another segment , not as the code above in the same segment?

thanks for your help.
AR

Re:funny asm instruction ?

Post by AR »

Code: Select all

[ORG 0x7C00]
jmp 0x7C0:label1
label1:
This jump instruction breaks down to 07C0:7C05, far jumps change the base address, the label's address is calculated relative to ORG, changing the segment will cause it to jump to weird places unless you change the ORG. Jumps do not take the length of its own instruction into account, you can jump to the same jump over and over with "jmp $"

The GDT Segment selector contains the Base of the segment as well as a length, you should probably read some docs on Protected Mode.
asmboozer

Re:funny asm instruction ?

Post by asmboozer »

AR wrote:

Code: Select all

[ORG 0x7C00]
jmp 0x7C0:label1
label1:
This jump instruction breaks down to 07C0:7C05,

have you disassembled the code? are you sure it is 0x7c0:7c05?

i think it should be 0x7c0:5,
since the code is loaded at 0x7c00, the jmp here only changes cs segment value, other behavour is just like near jmp.

and whether we change cs segment or not, the absolute address is the same. so the jmp in the above code is redundant.



far jumps change the base address, the label's address is calculated relative to ORG, changing the segment will cause it to jump to weird places unless you change the ORG. Jumps do not take the length of its own instruction into account, you can jump to the same jump over and over with "jmp $"


i mean the lable1 would be offsetX+5, 5 is what I mean by " length of jmp instruction".



The GDT Segment selector contains the Base of the segment as well as a length, you should probably read some docs on Protected Mode.
AR

Re:funny asm instruction ?

Post by AR »

What I am trying to explain is that labels are a delusion, they exist only in your imagination, the assembler converts the label into a relative address, the assembler ORG directive tells it where the program is relative to the start of CS, the assembler does not understand changing the segments it will continue to access things relative to ORG. Meaning that my example will dissassemble as 07C0:7C05. Just as "jmp 0x800:label1" will dissassemble as "0800:7C05."

And no the jump isn't redundant, it changes CS, however if I had written the code correctly I would have have had [ORG 0].
asmboozer

Re:funny asm instruction ?

Post by asmboozer »

AR wrote: What I am trying to explain is that labels are a delusion, they exist only in your imagination, the assembler converts the label into a relative address, the assembler ORG directive tells it where the program is relative to the start of CS, the assembler does not understand changing the segments it will continue to access things relative to ORG. Meaning that my example will dissassemble as 07C0:7C05. Just as "jmp 0x800:label1" will dissassemble as "0800:7C05."

And no the jump isn't redundant, it changes CS, however if I had written the code correctly I would have have had [ORG 0].
yes, you are right, i have thought label is 5 but it's 7c05.

does the jmp realy jump to 0x7c00+0x7c05?not 0x7c05?
AR

Re:funny asm instruction ?

Post by AR »

It jumps to 0x7C05 within the segment 0x7C0 which in Linear is 0xF805, far jumps are absolute not relative, if the ORG is not valid for the segment CS [and DS] is changed to then you have problems.
asmboozer

Re:funny asm instruction ?

Post by asmboozer »

AR wrote: What I am trying to explain is that labels are a delusion, they exist only in your imagination, the assembler converts the label into a relative address, the assembler ORG directive tells it where the program is relative to the start of CS, the assembler does not understand changing the segments it will continue to access things relative to ORG. Meaning that my example will dissassemble as 07C0:7C05. Just as "jmp 0x800:label1" will dissassemble as "0800:7C05."

And no the jump isn't redundant, it changes CS, however if I had written the code correctly I would have have had [ORG 0].

does boch emulator can display the address? how to setup/configure the emulator. thus i can see after jmp executed , where [e]ip is .

anyone has a boch.txt for floopy and for harddrive under windows would paste it out?


the orig only tells where the executable will be loaded in memory? it's irrevlant of cs segment register?

for example

[orig 0x7c00]

jmp 0x7c0:start
start:
times 510-($-$$) db 0   
dw 0xAA55


the code above will loaded at 0x7c00 in memory?
ie.
0x7c00: jmp 0x7c0:start
0x7c05: start:


after jmp executed, what place it jumps to?

if it's 0x7c0:0x7c05 it's outside the program.

i am confused now.




thanks
asmboozer

Re:funny asm instruction ?

Post by asmboozer »

AR wrote: It jumps to 0x7C05 within the segment 0x7C0 which in Linear is 0xF805, far jumps are absolute not relative, if the ORG is not valid for the segment CS [and DS] is changed to then you have problems.
if [e]ip 's 0xf805 after jmp, but the start label indicates 0x7c05 , i can not understand why after jmp, it executes the instructions after start label.
AR

Re:funny asm instruction ?

Post by AR »

ORG is relative to CS [and DS], all memory accesses are relative to ORG, essentially ORG is your master controller for the assembler. If ORG is 0x7C00 then the assembler will think that the code is at ????:7C00, it doesn't care about what ???? actually is as long as 0x7C00 is the correct offset.

Example:

Code: Select all

[ORG 0x7C00]
jmp 0x7C0:label1
label1:
mov eax, [data1]
data1: dd 0x12345678
The mov will be "mov [ds:0x7C0F]" (0xF is an approximation), this makes the calculation 'Segment:ORG+Offset'
Post Reply