home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.os.development      Operating system development chatter      4,255 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 3,135 of 4,255   
   James Harris to Scott Lurndal   
   Re: Pushing and popping segment register   
   23 Mar 22 17:20:38   
   
   From: james.harris.1@gmail.com   
      
   On 22/03/2022 15:41, Scott Lurndal wrote:   
   > James Harris  writes:   
   >> On 15/03/2022 16:19, Scott Lurndal wrote:   
   >>> James Harris  writes:   
      
   ...   
      
   >>>> ===> 2. Since two-byte values are being pushed as four bytes which bytes   
   >>>> hold the value and what gets put in the other two bytes?   
   >>>   
   >>> two byte values are zero extended for 32 or 64-bit StackAddrSize,   
   >>> except when pushing an immediate value, which will be sign extended.   
   >>   
   >> You might find this interesting. For an OS developer it turns out to be   
   >> not as simple as you suggest. Instead, what's left on the stack after a   
   >> segreg push will depend on the processor! Check out this from Intel:   
   >>   
   >> "When pushing a segment selector onto the stack, the Pentium 4, Intel   
   >> Xeon, P6 family, and Intel486 processors decrement the ESP register by   
   >> the operand size and then write 2 bytes. If the operand size is 32-bits,   
   >> the upper two bytes of the write are not modified. The Pentium processor   
   >> decrements the ESP register by the operand size and determines the size   
   >> of the write by the operand size. If the operand size is 32-bits, the   
   >> upper two bytes are written as 0s."   
   >   
   > I don't find processors prior to the Pentium particularly interesting,   
   > myself, and I don't expect that there are many operating system   
   > developers (of which number I have been counted for four decades now)   
   > that will be targeting 286, 386 or 486 processors (outside of simulators   
   > or using compatability mode in modern processors).   
   >   
   > But, your point stands that the selector is not always zero-extended   
   > stands.   
      
   Some further info, this time about more-recent processors, from the   
   current Intel manuals (dated December 2021):   
      
   "if the operand size is 32-bits, either a zero-extended value is pushed   
   on the stack or the segment selector is written on the stack using a   
   16-bit move. For the last case, all recent Intel Core and Intel Atom   
   processors perform a 16-bit move, leaving the upper portion of the stack   
   location unmodified."   
      
   Ref: https://cdrdv2.intel.com/v1/dl/getContent/671110   
      
   It's curious that the Pentium switched to pushing four bytes but later   
   processors switched back to writing just two. I'd have thought that   
   writing four would be more efficient overall as the words of stacks are   
   commonly accessed in sequence and it could prevent the processor having   
   to read a fully written line into cache.   
      
   AMD maybe behaves differently.   
      
   Either way, the point remains: if code is to be portable then the upper   
   two bytes of a segreg push cannot be relied upon.   
      
      
   --   
   James Harris   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca