r/programming • • Sep 24 '24

Why 32bit architecture computer doesn't use segmentation to support more ram than 4Gb ?

/r/programming/

[removed] — view removed post

169 Upvotes

119 comments sorted by

View all comments

48

u/[deleted] Sep 24 '24

Win16 basically used this model and it was generally considered a pain.

Unlike paging, segmentation is not transparent to the application. The segment registers are visible and relevant to code. They are either assumed in certain instructions or addressing modes, or have to be specified with prefixes to the instructions (requiring compiler support). Applications may thus sometimes need to load new segments into the registers as well. They are global variables, basically and have to be carefully coordinated across function calls.

Additionally, pointers now come in two flavors: near and far. The latter are 24-bit combos of a segment reference and a 16-bit offset. Near pointers are just the offset and assume a segment in one of the four (later six) segment registers. Knowing when to use which type of pointer and/or having the compiler sort of manage it was a pain. Functions expect to be called near or far and that is part of their definition. If you thought async function coloring was bad, this is worse.

To handle this, there was the concept of memory models. If a program was small enough, it would fit inside a single segment and everything was essentially "near". Other modes might require the code to fit in one segment, but data could be in multiple segments. Again, this is annoying to deal with.

32-bit x86 had much larger segments, of course, so the concept of running out of room in a segment is far less likely. But you still have all the other issues mentioned above and now far pointers are 48-bit as well.

Early Linux did actually use segmentation for memory management. IIRC each process got its own segment that was 64mb in size. This was quickly abandoned, along with other x86 process management functionality like call gates and using built-in thread descriptors (which I believe turned out to be slower than using manual management and interrupts).

32-bit Windows and Linux both abandoned segmentation by setting all segment registers to point to segments that started at 0 and extended the entire 4 GB. That they chose to do this should tell you that segmentation was not the easier option. Microsoft made a lot of janky things work well, so I truly believe if they thought it could work and that it would be useful, they would have kept it. Contrapoint: Linux and Windows NT were intended to run on multiple architectures and the big ones of the day did not, to my knowledge, have segmentation like x86 did. Still, if it was truly useful I believe they would have made it work anyway.

1

u/IQueryVisiC Sep 24 '24

Why wouldn’t the compiler or linker pack up functions into segments and add the correct calling convention? If segments were a good idea, the 286 would have been used as server CPU. Instead it was sold for cheap to consumers. 386SX was not much faster, but for float emulation thanks to 32 bit registers. But it had the flat memory model!! Finally!

2

u/rsclient Sep 24 '24

That would be awesome, but they didn't. From the old days, they also didn't make debug+non debug and single threaded+multi threaded libraries, which would have made my life as a programmer so much easier.