Segmentation in Assembly Language Programming

Similar documents
Summer 2003 Lecture 14 07/02/03

Computer Organization & Assembly Language Programming. CSE 2312 Lecture 15 Addressing and Subroutine

Parameter Passing. Procedure line. Calling procedure line. Most subroutines require parameters Can sometimes pass parameters via registers

Copyright 2000 by Barry B. Brey The CPU Scheduling Processes

Summer 2003 Lecture 15 07/03/03

Computer Architecture and System Software Lecture 07: Assembly Language Programming

EE 332 Real Time Systems Midterm Examination Solution Friday February 13, :30 pm to 4:30 pm

EE 314 Spring 2003 Microprocessor Systems

Procedures and the Stack. Chapter 4 S. Dandamudi

CSC 2400: Computer Systems. Using the Stack for Function Calls

Assembler Programming. Lecture 10

Dr. D.M. Akbar Hussain

CSC 8400: Computer Systems. Using the Stack for Function Calls

16.317: Microprocessor Systems Design I Fall 2013

Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015.

THE UNIVERSITY OF TRINIDAD & TOBAGO

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

UNIVERSITY OF CALIFORNIA, RIVERSIDE

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB3 FÖYÜ

Lecture 16: Passing Parameters on the Stack. Push Examples. Pop Examples. CALL and RET

Subprograms: Local Variables

Microprocessors (A) DOS Services

CALL instruction and RET instruction :

16-Bit Intel Processor Architecture

Lesson 4. Stack, Procedures and Macros

Summary: Direct Code Generation

1: /********************************************************** 2: * A

EC-333 Microprocessor and Interfacing Techniques

Code segment Stack segment

CSC 2400: Computer Systems. Using the Stack for Function Calls

EEM336 Microprocessors I. Data Movement Instructions

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC

Author: Steve Gorman Title: Programming with the Intel architecture in the flat memory model

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

x86 Assembly Tutorial COS 318: Fall 2017

Intel 8086: Instruction Set

Computer Architecture and System Software Lecture 06: Assembly Language Programming

Microprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Mr. Sapan Naik 1. Babu Madhav Institute of Information Technology, UTU

Tutorial Letter 103/3/2012 Computer Organization COS2621 Semesters 1 & 2

CC411: Introduction To Microprocessors

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

Microcomputer Architecture..Second Year (Sem.2).Lecture(2) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

ORG ; TWO. Assembly Language Programming

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Intel assembly language using gcc

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

Assembler Programming. Lecture 6

9/25/ Software & Hardware Architecture

Computer Systems Lecture 9

3: 4: 5: 9: ',0 10: ',0 11: ',0 12: 13:.CODE 14: INCLUDE

2 Sadeghi, Davi TU Darmstadt 2012 Secure, Trusted, and Trustworthy Computing Chapter 6: Runtime Attacks

Lab 8 The Stack (LIFO) Structure

CS401 - Computer Architecture and Assembly Language Programming Glossary By

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

Summer 2003 Lecture 4 06/14/03

UNIT 4. Modular Programming

SHEET-2 ANSWERS. [1] Rewrite Program 2-3 to transfer one word at a time instead of one byte.

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

CSE351 Spring 2018, Midterm Exam April 27, 2018

Chapter 3: Addressing Modes

Stack, subprograms. procedures and modular programming role of stack while using procedures stack implementation (Pentium)

LABORATORY WORK NO. 8 WORKING WITH MACROS AND LIBRARIES

Real instruction set architectures. Part 2: a representative sample

An Introduction to x86 ASM

System Software Assignment 1 Runtime Support for Procedures

Assembly Language: Function Calls

E CpE185 Laboratory Fall Introduction to Debug

Stacks and Function Calls

Procedures: Advanced Topics Chapter 12

Assembly Language. Dr. Esam Al_Qaralleh CE Department Princess Sumaya University for Technology. Overview of Assembly Language

CA Compiler Construction

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth

The CSx600 micro-computer

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

Systems Architecture The Stack and Subroutines

Project 3 Due October 21, 2015, 11:59:59pm

ADVANCE MICROPROCESSOR & INTERFACING

BASIC INTERRUPT PROCESSING

INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI

IA32 Intel 32-bit Architecture

Introduction to IA-32. Jo, Heeseung

Chapter 02: Computer Organization. Lesson 02: Functional units and components in a computer organization- Part 1: Processor

INTRODUCTION TO IA-32. Jo, Heeseung

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

Northern India Engineering College, Delhi (GGSIP University) PAPER I

Computer Architecture 1 ح 303

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

CS 2150 Exam 2, Spring 2018 Page 1 of 6 UVa userid:

Transcription:

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