Page 1 of 1

a basic problem

Posted: Sun Jan 11, 2004 11:47 pm
by shaz
the following code is part of Viridis 0.5.0.if u look at comment after setting esp,the coder aims to set asp at largest possible value.but 0xffff equals to 64k.and base of DATASEL is x0.then how could esp point to largest possible value in segment pointed to by DATASEL.
if there is some error then what value should i move to esp.

Code: Select all

mov eax,DATASEL??????
mov ds,eax??????
mov es,eax
mov fs,eax
mov gs,eax
mov ss,eax
mov esp,0xffff  ;set esp to the largest possible value


DATASEL     EQU $-GDT  ; 4GB Flat Data at 0x0 with max 0xFFFFF limit

DW     0xFFFF    ; Limit(2):0xFFFF
DW     0x0       ; Base(3)
DB     0x0       ; Base(2)
DB     0x92     ;present,ring0,data/stack,read/write(10010010)
DB     0xCF      ; Limit(1):0xF | Flags:4Kb inc,32bit (11001111)
DB     0x0       ; Base(1)

Re:a basic problem

Posted: Mon Jan 12, 2004 2:36 am
by Pype.Clicker
hmm, probably i should post this in Viridis test reports aswel.

1. First, never assign 0x*f to ESP. rather chose 0x*C, which will make dwords pushed & popped at aligned addresses. Accessing aligned dwords is sensibly faster than accessing non-aligned ones for architectural reasons.

2. The base of a segment is irrelevant when you try to define what offsets are valids within the segment. This is the role of the limit field and other flags. the base is just a value that is added to every offset in order to form a flat (linear) address.

3. If you look closely at the descriptor, you'll see that the limit field is 0xFFFFF (=1MB-1) with the granularity bit set (which means the limit is expressed in an amount of pages of 4K rather than in bytes. In other words, the segment is 0xFFFFF * 0x1000 + 0xFFF wide, which is 4GB-1. The comment about ESP is thus wrong (0xffff is not at all the highest possible value ;)