1 2 Segmentation in General Segmentation in Assembly Language Programming UNIX programs have 3 segments Text segment Executable machine instructions Data segment Initialized data BSS segment (Block Started by Symbol) Uninitialized data 3 4 Segmentation in 8086 Simplest Model 8086 programs begin with default segment definitions CS and DS must have pre-determined values ES and SS have some value but may not be used Some (or all) segments may overlap Define separate (non-overlapping) CS, DS, SS All code sits in one CS All stack accesses refer to the one SS Both constants and variables are stored in one DS Assembler/compiler associates a memory location to each data unit All variables are global Can be very inefficient: A full segment is 64 KB in size No spaces between segments Small code in a 64 KB segment wastes space
5 6 Modular Programming (Main + Functions) Easier to read and understand code Write, debug, and change modules independently Write some modules in high level language Compiler creates object code Machine code w/o linked addresses Write critical sections in assembly language Link together at end Local variables, pass parameters Memory Models For Modular Programming Each code module is in a separate CS Common DS for global variables Separate DS for local variables Protection Each module has its own data area Segmentation limits access to local data segment Pass parameters on stack or in registers 7 8 Problems with Multiple Segments Every function call is a far call and requires changing CS Global/Local variables requires many DS updates Only 16 non-overlapping segments in the 8086 address space 16 64 KB = 2 4 2 16 Bytes = 2 20 Bytes = 1 MB Can use overlapping segments, but lose protection Overlapping Segments Segment is up to 64 KB, Segment base address = segment register 10h Can begin a new segment every 16 = 10h bytes (paragraph) Example: Segment Register Address Segment Length Hex Round To Nearest KB Length Decimal 1280 12800 DS 400 h 400 h 1024 1240 12400 DS 400 h 400 h 1024 0240 02400 DS 10000 h 10000 h 65536 0200 02000 CS 3F4 h 400 h 1012 0140 01400 CS BE5 h C00 h 3045 0100 01000 CS 3DB h 400 h 987
9 10 Single Code Segment Modular Programming 1 Advantages Uses Call Near functions Eliminates segment register updates Saves memory Easily integrated with high level code Fastest running programs under DOS or Windows Disadvantages Requires bookkeeping for local variables and stacks Single Code Segment Modular Programming 2 Every module is a function (as in C) Start: Push BP onto stack Use SP as new BP (BP SP) Define variables based on BP [BP-02] ~ first word variable [BP-04] ~ second word variable [BP-06] ~ third word variable Point SP at bottom of variable list <Module Code> End: Restore SP (SP BP, which was unchanged) Pop BP off stack 11 Entry To Function Before entry to function SP (points to the last ) Old BP After entry to function old SP Old BP BP Variable BP 02 Variable BP 04 SP Empty Stack Empty Stack 12 Single Code Segment Modular Programming 3 Parameter passing: Calling modules Push parameters onto stack Function call pushes IP onto the stack Called function Reads (not pop) parameters from stack SP points to BP SP+02 points to IP SP+04 points to last parameter Performs function Returns single parameter in AX Returns parameter list pointer in AX
13 14 On Entry To Function Old SP (points to the old ) Passed Parameter Passed Parameter Passed Parameter SP after pushing passed parameters Old IP SP after call instruction pushes old IP Old BP SP after pushing old BP (also the new BP) Variable A BP 02 (new variables defined in function) Variable B BP 04 Variable C BP 06 (also the adjusted SP) SP 02 first empty stack location usable by function Example 1 main() { int a = 0, x; x = function(a); a = a+1; } int function(a) { int r; r = a + 5; return(r); } 15 Example 2 22CC:0000 JMP 003D skip to end SP 0 =A2 BP 0 =00 22CC:0003 PUSH BP save BP SP 1 SP 0-02 = A0 [SP 1 ] BP 0 22CC:0004 MOV BP,SP use SP as BP BP 1 SP 1 = A0 22CC:0006 SUB SP,+04 adjust SP to include next 2 SP 2 SP 1-04 = 9C integers 22CC:0009 MOV [BP-02],0000 define a = 0 a = [BP 1-02] = [9E] 0 22CC:000E PUSH [BP-02] put a onto stack SP 3 SP 2-02 = 9A [SP 3 ] [009E] = a 22CC:0011 CALL 0027 call to 0027 SP 4 SP 3-02 = 98 [SP 4 ] IP = 0014 IP 0027 22CC:0014 ADD SP,+02 remove pushed a from stack SP 10 SP 9 +02 = SP 2 = 9C 22CC:0017 MOV [BP-04],AX Put returned AX into x x = [BP3-04] = [BP1-04] AX 22CC:001A MOV AX,[BP-02] move a to AX AX [BP3-02] = [BP1-02] = a 22CC:001D ADD AX,0001 add 1 to a AX AX + 1 22CC:0020 MOV [BP-02],AX AX back to a a = [BP3-02] = [BP1-02] AX 22CC:0023 MOV SP,BP restore SP SP 11 BP 1 = SP 1 = A0 22CC:0025 POP BP restore BP BP 4 [SP 1 ] = BP 0 = 00 SP 12 SP 11 +02 = SP 0 = A2 22CC:0026 RET return to DOS 16 Example 3 22CC:0027 PUSH BP save BP SP 5 SP 4-02 = 96 [SP 5 ] BP 1 = A0 22CC:0028 MOV BP,SP use SP as BP BP 2 SP 5 = 96 22CC:002A SUB SP,+02 adjust SP to include next integer SP 6 SP 5-02 = 94 22CC:002D MOV AX,[BP+04] location of pushed argument: AX [BP 2 +04] = [9A] = a call: SP 4 SP 3 2 push BP: SP 5 SP 4 2 BP 2 = SP 5 SP 4 = BP 2 + 4 is where ARG was pushed 22CC:0030 ADD AX,0005 add 5 to AX AX AX+5 = a + 5 22CC:0033 MOV [BP-02],AX put AX into r r = [BP 2-02] = [94] AX 22CC:0036 MOV AX,[BP-02] put r into AX for returning AX r = [BP 2-02] (pass-by-register) 22CC:0039 MOV SP,BP restore SP SP 7 BP 2 = SP 5 = 96 22CC:003B POP BP restore BP BP 3 [SP 7 ] = BP 1 = A0 SP 8 SP 7 +02 = SP 4 = 98 22CC:003C RET return to calling spot IP [SP 4 ] = 0014 SP 9 SP 8 + 02 = SP 3 = 9A 22CC:003D SS:
17 18 Example 4 Example 5 A2 = SP 0 (points to the old ) A2 = SP 0 (points to the old ) 19 20 Example 6 Example 7 A2 = SP 0 (points to the old ) A2 = SP 0 (points to the old )
21 22 Example 8 Example 9 A2 = SP 0 (points to the old ) old IP = 0014 98 = SP 4 after call instruction pushes old IP A2 = SP 0 (points to the old ) old IP = 0014 98 = SP 4 after call instruction pushes old IP BP 1 = A0 96 = SP 5 after pushing BP 1 (is also BP 2 ) 23 24 Example 10 Example 11 A2 = SP 0 (points to the old ) old IP = 0014 98 = SP 4 after call instruction pushes old IP BP 1 = A0 96 = SP 5 after pushing BP 1 (is also BP 2 ) location of integer r 94 = SP 6 after adjusting for integer r (is also BP 2 02) 92 = SP 02 = first empty stack location usable by function A2 = SP 0 (points to the old ) old IP = 0014 98 = SP 4 after call instruction pushes old IP BP 1 = A0 96 = SP 7 = SP 5 after copying BP 2 into SP
25 26 Example 12 Example 13 A2 = SP 0 (points to the old ) old IP = 0014 98 = SP 8 = SP 4 after popping old BP (BP 3 = BP 1 = 0A) A2 = SP 0 (points to the old ) a = 0 9A = SP 9 = SP 3 after return instruction pops old IP 27 28 Example 14 Example 15 A2 = SP 0 (points to the old ) location of integer x 9C = SP 10 = SP 2 after removing passes parameter from stack A2 = SP 0 (points to the old ) 00 = BP 0 A0 = SP 11 = SP 1 after restoring SP from BP
29 Example 16 A2 = SP 12 = SP 0 after popping old BP from stack