assembly language lecture 1

37
Assembly Language Instruction Addressing and Execution Motaz K. Saad Spring 2007 1 Motaz K. Saad, Dept. of CS

Upload: motaz-saad

Post on 17-Jan-2015

22.800 views

Category:

Technology


2 download

DESCRIPTION

Assembly Language Lecture 1

TRANSCRIPT

Page 1: Assembly Language Lecture 1

Assembly LanguageInstruction Addressing and

Execution

Motaz K. SaadSpring 2007

1Motaz K. Saad, Dept. of CS

Page 2: Assembly Language Lecture 1

Lesson plan

• Review some concepts in the first week• First assembly program with EMU8086• Related concepts with the first program:

– Loading program – Boot process– Handling the stack

2Motaz K. Saad, Dept. of CS

Page 3: Assembly Language Lecture 1

Recalling main concepts

3Motaz K. Saad, Dept. of CS

Page 4: Assembly Language Lecture 1

Recalling main concepts

4Motaz K. Saad, Dept. of CS

Page 5: Assembly Language Lecture 1

Recalling main concepts

• Segment: special areas defined to contain CODE, DATA and STACK

• Paragraph boundary: location evenly divisible by 16 or 10H

5Motaz K. Saad, Dept. of CS

Page 6: Assembly Language Lecture 1

Recalling main concepts

Stack Segment

Data Segment

Code Segment

SSDSCS

Segment Registers

6Motaz K. Saad, Dept. of CS

Page 7: Assembly Language Lecture 1

.MODEL SMALL

.STACK

.DATAMESSAGE DB "HELLO EVERYBODY! I AM LEARNING ASSEMBLY LANGUAGE!","$"

.CODE

MAIN PROC MOV AX, @DATA MOV DS,AX MOV AH,09 LEA DX,MESSAGE INT 21H MOV AX,4C00H INT 21HMAIN ENDPEND MAIN

7Motaz K. Saad, Dept. of CS

Page 8: Assembly Language Lecture 1

Types of programs

• *.COM and *.EXE files– *.COM: consists of one segment containing code,

data and stack– *.exe: separate code, data and stack segments– COM is small, (small utility program or can be a

resident program). We will focus on EXE program in this course.

8Motaz K. Saad, Dept. of CS

Page 9: Assembly Language Lecture 1

Loading *.exe program• Access *.exe from disk• 256-byte Program Segment Prefix (PSP) on a

paragraph boundary• Store the program immediately following the PSP• Load address of PSP in the DS & ES• Load code segment in CS, set IP • Load address of the stack to SS, set SP• Transfer control to the program for execution• PSP: Created by the operating system and

contains information about some interrupt vectors, addresses of system fields etc.

9Motaz K. Saad, Dept. of CS

Page 10: Assembly Language Lecture 1

PSP

10Motaz K. Saad, Dept. of CS

Page 11: Assembly Language Lecture 1

Assembly directive to

define memory model to use in

the program

.MODEL SMALL,

.STACK

.DATAMESSAGE DB "HELLO EVERYBODY! I AM LEARNING ASSEMBLY

LANGUAGE!","$"

.CODE

MAIN PROC MOV AX, @DATA MOV DS,AX MOV AH,09 LEA DX,MESSAGE INT 21H MOV AX,4C00H INT 21HMAIN ENDPEND MAIN

11Motaz K. Saad, Dept. of CS

Page 12: Assembly Language Lecture 1

Memory Model

• There are several types of memory: Memory model TINY, SMALL, COMPACT, MEDIUM, LARGE, HUGE, or FLAT.

• Determines size of code and data pointers. This field is required.

• The model does, however, control segment defaults and determine whether data and code are near or far by default, as indicated in the following table.

• The model does not control the type of instructions that we can use.

12Motaz K. Saad, Dept. of CS

Page 13: Assembly Language Lecture 1

Real and Protected mode

Real Mode 16-bit Protected

Mode

32-bit Protected Mode

Segment base address

20-bit24-bit,

from descriptor

32-bit,from descriptor

Segment size (limit)

16-bit, 64K bytes (fixed)

16-bit, 1-64K bytes

20-bit, 1-1M bytes or 4K-4G bytes

Segment protection

no yes yes

Segmentregister

segment base address / 16

selector selector

13Motaz K. Saad, Dept. of CS

Page 14: Assembly Language Lecture 1

Protected mode-Is a type of memory utilization, available on Intel 80286 and later

-Support:protection: each program is protected from

interference from other programs. extended memory : Enables a single program

to access more than 640K of memory. virtual memory : Expands the address space

to over 1GB. Multitasking:

14Motaz K. Saad, Dept. of CS

Page 15: Assembly Language Lecture 1

Booting processWhat is booting?• The process of starting or restarting a computer

cold boot

Process of turning on a computer after it

has been powered off completely

warm boot

Process of restarting a

computer that is already powered on

Also called a warm start

15Motaz K. Saad, Dept. of CS

Page 16: Assembly Language Lecture 1

Booting process

processor

BIO

S

hard disk

CD-ROM drive

(RAM) memory modules

CMOS

floppy disk drive

Step 6

expansion cards

How does a personal computer boot up?

16Motaz K. Saad, Dept. of CS

Page 17: Assembly Language Lecture 1

How does a personal computer boot up?

• Step 1: The power supply sends a signal to components in the system unit.

• Step 2: The processor looks for the BIOS (Basic Input/Output system)

17Motaz K. Saad, Dept. of CS

Page 18: Assembly Language Lecture 1

How does a personal computer boot up?

• Step 3: The BIOS performs the POST, which checks components such as the mouse, keyboard connectors, and expansion cards. (Power On Self Test)

• Step 4: The results of the POST are compared with data in the CMOS chip. (battery power)

• Step 5: The BIOS looks for the system files in drive A (floppy disk drive) and then drive C (hard disk).

18Motaz K. Saad, Dept. of CS

Page 19: Assembly Language Lecture 1

How does a personal computer boot up?

• Step 6: The boot program loads the kernel of the operating system into RAM from storage (hard disk).• The operating system in memory takes control of

the computer.

• Step 7: The operating system loads configuration information and displays the desktop on the screen.

19Motaz K. Saad, Dept. of CS

Page 20: Assembly Language Lecture 1

How does a personal computer boot up?

• The operating system executes programs in the StartUp folder• Registry - Several files that contain the system

configuration information• Registry is constantly accessed during the computer's

operation

• StartUp folder - Contains a list of programs that open automatically when you boot the computer

20Motaz K. Saad, Dept. of CS

Page 21: Assembly Language Lecture 1

BIOS Boot process

BIOS routineFFFF0H

Interrupt VectorTable

BIOS Data Areas

Access the bootstrap

loader

Check portsInitialize devices

21Motaz K. Saad, Dept. of CS

Page 22: Assembly Language Lecture 1

BIOS Boot process

• Internally, processor enters the reset state, clear all memory location to zero, perform a parity check of a memory, set CS to FFFFH, IP to zero. The first address to execute is FFFF0H (entry point of BIOS in ROM)

• Identify & Initialized devices.• Established the tables: IVT (Interrupt Vector

Table) and BIOS data area (status of attached devices)

• Determine if the disk containing the system file is available.

22Motaz K. Saad, Dept. of CS

Page 23: Assembly Language Lecture 1

Assembly directive to

define stack to use in the program

.model small

.STACK

.DATAMESSAGE DB "HELLO EVERYBODY! I AM LEARNING ASSEMBLY

LANGUAGE!","$"

.CODE

MAIN PROC MOV AX, @DATA MOV DS,AX MOV AH,09 LEA DX,MESSAGE INT 21H MOV AX,4C00H INT 21HMAIN ENDPEND MAIN

23Motaz K. Saad, Dept. of CS

Page 24: Assembly Language Lecture 1

STACK

• The word is from data structure• Last In, First Out (LIFO) mechanism• STACK in OS has three main functions:

– Contains return address – Data– Content of present registers

24Motaz K. Saad, Dept. of CS

Page 25: Assembly Language Lecture 1

STACK

• PUSH– Decrease SP by 2 and store a value there

• POP– Return a value from stack and increase SP by 2

25Motaz K. Saad, Dept. of CS

Page 26: Assembly Language Lecture 1

Lesson plan

• Review loading an *.exe file• Concept of execution of instructions• Practice:

• Execution of instructions

26Motaz K. Saad, Dept. of CS

Page 27: Assembly Language Lecture 1

Access *.exe from disk256-byte Program Segment Prefix (PSP) on a paragraph boundaryStore the program immediately following the PSPLoad address of PSP in the DS & ESLoad code segment in CS, set IP Load address of the stack to SS, set SPTransfer control to the program for execution

Loading *.exe file

27Motaz K. Saad, Dept. of CS

Page 28: Assembly Language Lecture 1

Loading *.exe file• The sequence of segments (code, data, and stack)

is given• SS: contains the address of the beginning of the

stack• CS: contains the address of the beginning of the

code segment• DS: contains the address of the beginning of the

data segment• SP: contains the size of stack

28Motaz K. Saad, Dept. of CS

Page 29: Assembly Language Lecture 1

Practice2B360H

PSP

Stack Segment

Data Segment

Code Segment

Memory

29Motaz K. Saad, Dept. of CS

Page 30: Assembly Language Lecture 1

Practice2B360H

PSP

Stack Segment

Data Segment

Code Segment

Memory

PSP 2B360HPSP size 100HOffset 0HSS 2B460H (stored as 2B46)

2B46HSS

30Motaz K. Saad, Dept. of CS

Page 31: Assembly Language Lecture 1

Practice

2B360H

PSP

Stack Segment

Data Segment

Code Segment

Memory

2B46HSS

PSP 2B360HPSP size 100HOffset 30H

70HCS 2B500H (stored as 2B50)

CS 2B50H

31Motaz K. Saad, Dept. of CS

Page 32: Assembly Language Lecture 1

Practice

2B360H

PSP

Stack Segment

Data Segment

Code Segment

Memory

2B46HSS

CS 2B50H

2B36H

2B36H

DS

ES

SP 0030H

32Motaz K. Saad, Dept. of CS

Page 33: Assembly Language Lecture 1

Instruction Execution and Addressing

• Executing an instruction include– Fetch the next instruction, put to a queue

(QUEUE: FIFO vs. STACK LIFO)– Decode the instruction– Execute the instruction

33Motaz K. Saad, Dept. of CS

Page 34: Assembly Language Lecture 1

Example

4AF0CS 0013IP

4AF13H

CS segment address: 4AF00HIP offset: 0013H________________________

+

Instruction address:

04B1DS

34Motaz K. Saad, Dept. of CS

Page 35: Assembly Language Lecture 1

Example

4AF0CS 0013IP

4AF13H

A01200

Memory

04B1DS

Decode instruction:

AO: MOV [0012] to AL

35Motaz K. Saad, Dept. of CS

Page 36: Assembly Language Lecture 1

Example

4AF0CS 0013IP

04B03H

A01200

Memory

04B1DS

DS segment address:04B10HIP offset: 0012H________________________

+

Data address: 04B22H

36Motaz K. Saad, Dept. of CS

Page 37: Assembly Language Lecture 1

Example

4AF0CS 0013IP

04B03H

A01200

1B

Memory

04B1DS

04B22Data address:

04B22H

| 1B

AXAX

AH AL

37Motaz K. Saad, Dept. of CS