Segmentation in Assembly Language Programming

1 Segmentation in Assembly Language Programming Microprocessors (A) Segmentation in Assembly Language Programming Fall 2004 Hadassah College Dr. ...
1 downloads 0 Views 83KB Size
1

Segmentation in Assembly Language Programming

Microprocessors (A)

Segmentation in Assembly Language Programming Fall 2004

Hadassah College

Dr. Martin Land

2

Segmentation in Assembly Language Programming

Microprocessors (A)

Segmentation in General UNIX programs have 3 segments Text segment Executable machine instructions

Data segment Initialized data

BSS segment (Block Started by Symbol) Uninitialized data

Fall 2004

Hadassah College

Dr. Martin Land

3

Segmentation in Assembly Language Programming

Microprocessors (A)

Segmentation in 8086 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

Fall 2004

Hadassah College

Dr. Martin Land

4

Segmentation in Assembly Language Programming

Microprocessors (A)

Simplest Model 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 Fall 2004

Hadassah College

Dr. Martin Land

5

Segmentation in Assembly Language Programming

Microprocessors (A)

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

Fall 2004

Hadassah College

Dr. Martin Land

6

Segmentation in Assembly Language Programming

Microprocessors (A)

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

Fall 2004

Hadassah College

Dr. Martin Land

7

Segmentation in Assembly Language Programming

Microprocessors (A)

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 = 24 × 216 Bytes = 220 Bytes = 1 MB

Can use overlapping segments, but lose protection

Fall 2004

Hadassah College

Dr. Martin Land

8

Segmentation in Assembly Language Programming

Microprocessors (A)

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 1280 1240 0240 0200 0140 0100 Fall 2004

Address 12800 12400 02400 02000 01400 01000

Segment DS DS DS CS CS CS

Length Hex 400 h 400 h 10000 h 3F4 h BE5 h 3DB h Hadassah College

Round To Length Nearest KB Decimal 400 h 1024 400 h 1024 10000 h 65536 400 h 1012 C00 h 3045 400 h 987 Dr. Martin Land

9

Segmentation in Assembly Language Programming

Microprocessors (A)

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 Fall 2004

Hadassah College

Dr. Martin Land

10

Segmentation in Assembly Language Programming

Microprocessors (A)

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 End: Restore SP (SP ← BP, which was unchanged) Pop BP off stack Fall 2004

Hadassah College

Dr. Martin Land

11

Segmentation in Assembly Language Programming

Microprocessors (A)

Entry To Function Before entry to function Stack Value Stack Value

← SP (points to the last stack value)

← Old BP Stack Value Stack Value Old BP Variable Variable Empty Stack Empty Stack

Fall 2004

After entry to function ← ← ← ←

old SP BP BP – 02 BP – 04

Hadassah College

← SP

Dr. Martin Land

12

Segmentation in Assembly Language Programming

Microprocessors (A)

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 Fall 2004

Hadassah College

Dr. Martin Land

13

Segmentation in Assembly Language Programming

Microprocessors (A)

On Entry To Function Stack Value Stack Value Stack Value Stack Value Passed Parameter Passed Parameter Passed Parameter Old IP Old BP Variable A Variable B Variable C

Fall 2004

← Old SP (points to the old stack value)

← ← ← ← ← ← ←

SP SP SP BP BP BP SP

after pushing passed parameters after call instruction pushes old IP after pushing old BP (also the new BP) – 02 (new variables defined in function) – 04 – 06 (also the adjusted SP) – 02 first empty stack location usable by function

Hadassah College

Dr. Martin Land

14

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 1 main() { int a = 0, x; x = function(a); a = a+1; }

int function(a) { int r; r = a + 5; return(r); } Fall 2004

Hadassah College

Dr. Martin Land

15

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 2 22CC:0000 22CC:0003

JMP 003D PUSH BP

skip to end save BP

22CC:0004 22CC:0006

MOV BP,SP SUB SP,+04

22CC:0009 22CC:000E

MOV [BP-02],0000 PUSH [BP-02]

use SP as BP adjust SP to include next 2 integers define a = 0 put a onto stack

22CC:0011

CALL 0027

call to 0027

22CC:0014 22CC:0017 22CC:001A 22CC:001D 22CC:0020 22CC:0023 22CC:0025

ADD MOV MOV ADD MOV MOV POP

remove pushed a from stack Put returned AX into x move a to AX add 1 to a AX back to a restore SP restore BP

22CC:0026

RET

Fall 2004

SP,+02 [BP-04],AX AX,[BP-02] AX,0001 [BP-02],AX SP,BP BP

return to DOS

Hadassah College

SP0=A2 BP0=00 SP1 ← SP0-02 = A0 [SP1] ← BP0 BP1 ← SP1 = A0 SP2 ← SP1-04 = 9C a = [BP1-02] = [9E] ← 0 SP3 ← SP2-02 = 9A [SP3] ← [009E] = a SP4 ← SP3-02 = 98 [SP4] ← IP = 0014 IP ← 0027 SP10 ← SP9+02 = SP2 = 9C x = [BP3-04] = [BP1-04] ← AX AX ← [BP3-02] = [BP1-02] = a

AX ← AX + 1 a = [BP3-02] = [BP1-02] ← AX

SP11 ← BP1 = SP1 = A0 BP4 ← [SP1] = BP0 = 00 SP12 ← SP11+02 = SP0 = A2

Dr. Martin Land

16

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 3 22CC:0027

PUSH BP

save BP

22CC:0028 22CC:002A 22CC:002D

MOV BP,SP SUB SP,+02 MOV AX,[BP+04]

22CC:0030 22CC:0033 22CC:0036

ADD AX,0005 MOV [BP-02],AX MOV AX,[BP-02]

22CC:0039 22CC:003B

MOV SP,BP POP BP

use SP as BP adjust SP to include next integer location of pushed argument: call: SP4 ← SP3 – 2 push BP: SP5 ← SP4 – 2 BP2 = SP5 SP4 = BP2 + 4 is where ARG was pushed add 5 to AX put AX into r put r into AX for returning (pass-by-register) restore SP restore BP

22CC:003C

RET

return to calling spot

22CC:003D

SS:

Fall 2004

Hadassah College

SP5 ← SP4-02 = 96 [SP5] ← BP1 = A0 BP2 ← SP5 = 96 SP6 ← SP5-02 = 94 AX ← [BP2+04] = [9A] = a

AX ← AX+5 = a + 5 r = [BP2-02] = [94] ← AX AX ← r = [BP2-02] SP7 ← BP2 = SP5 = 96 BP3 ← [SP7] = BP1 = A0 SP8 ← SP7+02 = SP4 = 98 IP ← [SP4] = 0014 SP9 ← SP8 + 02 = SP3 = 9A

Dr. Martin Land

17

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 4 stack value stack value

Fall 2004

← A2 = SP0 (points to the old stack value)

Hadassah College

Dr. Martin Land

18

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 5 stack value stack value 00 = BP0

Fall 2004

← A2 = SP0 (points to the old stack value) ← A0 = SP1 after pushing old BP0 (is also BP1)

Hadassah College

Dr. Martin Land

19

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 6 stack value stack value 00 = BP0 location of integer a location of integer x

Fall 2004

← ← ← ←

A2 = SP0 (points to the old stack value) A0 = SP1 after pushing old BP0 (is also BP1) 9E 9C = SP2 after adjusting for integers a and x

Hadassah College

Dr. Martin Land

20

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 7 stack value stack value 00 = BP0 location of integer a location of integer x a = 0

Fall 2004

← ← ← ← ←

A2 A0 9E 9C 9A

= SP0 (points to the old stack value) = SP1 after pushing old BP0 (is also BP1) = SP2 after adjusting for integers a and x = SP3 after pushing passed parameter a (is also BP2 + 4)

Hadassah College

Dr. Martin Land

21

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 8 stack value stack value 00 = BP0 location of integer a location of integer x a = 0 old IP = 0014

Fall 2004

← ← ← ← ← ←

A2 = SP0 (points to the old stack value) A0 = SP1 after pushing old BP0 (is also BP1) 9E 9C = SP2 after adjusting for integers a and x 9A = SP3 after pushing passed parameter a (is also BP2 + 4) 98 = SP4 after call instruction pushes old IP

Hadassah College

Dr. Martin Land

22

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 9 stack value stack value 00 = BP0 location of integer a location of integer x a = 0 old IP = 0014 BP1 = A0

Fall 2004

← ← ← ← ← ← ←

A2 = SP0 (points to the old stack value) A0 = SP1 after pushing old BP0 (is also BP1) 9E 9C = SP2 after adjusting for integers a and x 9A = SP3 after pushing passed parameter a (is also BP2 + 4) 98 = SP4 after call instruction pushes old IP 96 = SP5 after pushing BP1 (is also BP2)

Hadassah College

Dr. Martin Land

23

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 10 stack value stack value 00 = BP0 location of integer a location of integer x a = 0 old IP = 0014 BP1 = A0 location of integer r

Fall 2004

← ← ← ← ← ← ← ← ←

A2 = SP0 (points to the old stack value) A0 = SP1 after pushing old BP0 (is also BP1) 9E 9C = SP2 after adjusting for integers a and x 9A = SP3 after pushing passed parameter a (is also BP2 + 4) 98 = SP4 after call instruction pushes old IP 96 = SP5 after pushing BP1 (is also BP2) 94 = SP6 after adjusting for integer r (is also BP2 – 02) 92 = SP – 02 = first empty stack location usable by function

Hadassah College

Dr. Martin Land

24

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 11 stack value stack value 00 = BP0 location of integer a location of integer x a = 0 old IP = 0014 BP1 = A0

Fall 2004

← ← ← ← ← ← ←

A2 = SP0 (points to the old stack value) A0 = SP1 after pushing old BP0 (is also BP1) 9E 9C = SP2 after adjusting for integers a and x 9A = SP3 after pushing passed parameter a (is also BP2 + 4) 98 = SP4 after call instruction pushes old IP 96 = SP7 = SP5 after copying BP2 into SP

Hadassah College

Dr. Martin Land

25

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 12 stack value stack value 00 = BP0 location of integer a location of integer x a = 0 old IP = 0014

Fall 2004

← ← ← ← ← ←

A2 = SP0 (points to the old stack value) A0 = SP1 after pushing old BP0 (is also BP1) 9E 9C = SP2 after adjusting for integers a and x 9A = SP3 after pushing passed parameter a (is also BP2 + 4) 98 = SP8 = SP4 after popping old BP (BP3 = BP1 = 0A)

Hadassah College

Dr. Martin Land

26

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 13 stack value stack value 00 = BP0 location of integer a location of integer x a = 0

Fall 2004

← ← ← ← ←

A2 A0 9E 9C 9A

= SP0 (points to the old stack value) = SP1 after pushing old BP0 (is also BP1) = SP2 after adjusting for integers a and x = SP9 = SP3 after return instruction pops old IP

Hadassah College

Dr. Martin Land

27

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 14 stack value stack value 00 = BP0 location of integer a location of integer x

Fall 2004

← ← ← ←

A2 = SP0 (points to the old stack value) A0 = SP1 after pushing old BP0 (is also BP1) 9E 9C = SP10 = SP2 after removing passes parameter from stack

Hadassah College

Dr. Martin Land

28

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 15 stack value stack value 00 = BP0

Fall 2004

← A2 = SP0 (points to the old stack value) ← A0 = SP11 = SP1 after restoring SP from BP

Hadassah College

Dr. Martin Land

29

Segmentation in Assembly Language Programming

Microprocessors (A)

Example ⎯ 16 stack value stack value

Fall 2004

← A2 = SP12 = SP0 after popping old BP from stack

Hadassah College

Dr. Martin Land

Suggest Documents