Assembly Language Instruction Addressing and Execution

Motaz K. Saad Spring 2007

Motaz K. Saad, Dept. of CS

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

Motaz K. Saad, Dept. of CS

2

Recalling main concepts

Motaz K. Saad, Dept. of CS

3

Saad. of CS 4 .Recalling main concepts Motaz K. Dept.

Dept.Recalling main concepts • Segment: special areas defined to contain CODE. DATA and STACK • Paragraph boundary: location evenly divisible by 16 or 10H Motaz K. of CS 5 . Saad.

Dept.Recalling main concepts Stack Segment Data Segment Code Segment SS DS CS Segment Registers Motaz K. of CS 6 . Saad.

4C00H INT 21H MAIN ENDP END MAIN Motaz K.MODEL SMALL ..AX MOV AH.MESSAGE INT 21H MOV AX."$" .DATA MESSAGE DB "HELLO EVERYBODY! I AM LEARNING ASSEMBLY LANGUAGE!". Saad.09 LEA DX.CODE MAIN PROC MOV AX. Dept.STACK . of CS 7 . @DATA MOV DS.

of CS 8 . We will focus on EXE program in this course.exe: separate code. data and stack segments – COM is small. Dept.COM and *.COM: consists of one segment containing code.Types of programs • *.EXE files – *. Motaz K. data and stack – *. (small utility program or can be a resident program). Saad.

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. Motaz K. Dept. addresses of system fields etc. Saad. set SP • Transfer control to the program for execution • PSP: Created by the operating system and contains information about some interrupt vectors. of CS 9 .Loading *. set IP • Load address of the stack to SS.exe program • Access *.

PSP Motaz K. Dept. of CS 10 . Saad.

AX MOV AH.DATA MESSAGE DB "HELLO EVERYBODY! I AM LEARNING ASSEMBLY LANGUAGE!". Saad.STACK .4C00H INT 21H MAIN ENDP END MAIN Motaz K.CODE MAIN PROC MOV AX. @DATA MOV DS.."$" .MODEL SMALL. .MESSAGE INT 21H MOV AX. of CS 11 Assembly directive to define memory model to use in the program .09 LEA DX. Dept.

Memory Model • There are several types of memory: Memory model TINY. control segment defaults and determine whether data and code are near or far by default. Motaz K. however. of CS 12 . SMALL. or FLAT. Dept. This field is required. • Determines size of code and data pointers. • The model does. MEDIUM. • The model does not control the type of instructions that we can use. as indicated in the following table. Saad. COMPACT. LARGE. HUGE.

1-1M bytes or 4K-4G bytes yes selector Segment base 20-bit address Segment size (limit) 16-bit. 64K bytes (fixed) no segment base address / 16 Segment protection Segment register Motaz K.Real and Protected mode Real Mode 16-bit Protected Mode 24-bit. of CS 13 . 1-64K bytes yes selector 32-bit Protected Mode 32-bit. from descriptor 16-bit. Dept. from descriptor 20-bit. Saad.

Multitasking: Motaz K. available on Intel 80286 and later -Support: protection: each program is protected from interference from other programs. of CS 14 . extended memory : Enables a single program to access more than 640K of memory.Protected mode -Is a type of memory utilization. Dept. virtual memory : Expands the address space to over 1GB. Saad.

Booting process What is booting? • The process of starting or restarting a computer warm boot cold boot Process of turning on a computer after it has been powered off completely Process of restarting a computer that is already powered on Also called a warm start Motaz K. Dept. Saad. of CS 15 .

Saad.Booting process How does a personal computer boot up? processor (RAM) memory modules BIOS floppy disk drive CD-ROM drive CMOS hard disk Step 6 expansion cards Motaz K. Dept. of CS 16 .

How does a personal computer boot up? • Step 1: The power supply sends a signal to components in the system unit. of CS 17 . Dept. • Step 2: The processor looks for the BIOS (Basic Input/Output system) Motaz K. Saad.

Motaz K. Saad. of CS 18 . and expansion cards. keyboard connectors. (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). which checks components such as the mouse. Dept.How does a personal computer boot up? • Step 3: The BIOS performs the POST.

of CS 19 . Dept. • The operating system in memory takes control of the computer. Saad. • Step 7: The operating system loads configuration information and displays the desktop on the screen. Motaz K.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).

Dept. of CS 20 .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 Motaz K. Saad.How does a personal computer boot up? • The operating system executes programs in the StartUp folder • Registry .

Dept. of CS 21 .BIOS Boot process FFFF0H BIOS routine Check ports Initialize devices Interrupt Vector Table BIOS Data Areas Access the bootstrap loader Motaz K. Saad.

Saad. Dept. of CS 22 . • 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. perform a parity check of a memory. processor enters the reset state. clear all memory location to zero. IP to zero. Motaz K. The first address to execute is FFFF0H (entry point of BIOS in ROM) • Identify & Initialized devices.BIOS Boot process • Internally. set CS to FFFFH.

MESSAGE INT 21H MOV AX.4C00H INT 21H MAIN ENDP END MAIN Motaz K. Saad. @DATA MOV DS. Dept.CODE MAIN PROC MOV AX.STACK .AX MOV AH. of CS 23 Assembly directive to define stack to use in the program .model small .."$" .09 LEA DX.DATA MESSAGE DB "HELLO EVERYBODY! I AM LEARNING ASSEMBLY LANGUAGE!".

Saad. First Out (LIFO) mechanism • STACK in OS has three main functions: – Contains return address – Data – Content of present registers Motaz K. of CS 24 . Dept.STACK • The word is from data structure • Last In.

of CS 25 . Saad. Dept.STACK • PUSH – Decrease SP by 2 and store a value there • POP – Return a value from stack and increase SP by 2 Motaz K.

Lesson plan • Review loading an *. of CS 26 . Dept. Saad.exe file • Concept of execution of instructions • Practice: • Execution of instructions Motaz K.

Saad. Dept. of CS 27 . set SP Transfer control to the program for execution Motaz K.Loading *.exe file Access *. set IP Load address of the stack to SS.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.

of CS 28 . data.Loading *. 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 Motaz K. Dept. Saad.exe file • The sequence of segments (code.

of CS 29 . Saad.Practice 2B360H PSP Stack Segment Data Segment Code Segment Memory Motaz K. Dept.

of CS 30 .Practice 2B360H PSP 2B46H Stack Segment PSP 2B360H PSP size 100H Offset 0H SS 2B460H (stored as 2B46) SS Data Segment Code Segment Memory Motaz K. Saad. Dept.

Practice 2B360H PSP 2B46H Stack Segment PSP 2B360H PSP size 100H Offset 30H 70H CS 2B500H (stored as 2B50) SS Data Segment CS 2B50H Code Segment Memory Motaz K. Dept. Saad. of CS 31 .

Practice 2B360H PSP DS SS 2B46H Stack Segment ES Data Segment SP CS 2B50H Code Segment 0030H 2B36H 2B36H Memory Motaz K. Saad. of CS 32 . Dept.

Instruction Execution and Addressing • Executing an instruction include – Fetch the next instruction. Dept. Saad. STACK LIFO) – Decode the instruction – Execute the instruction Motaz K. of CS 33 . put to a queue (QUEUE: FIFO vs.

Example CS 4AF0 IP 0013 DS 04B1 + CS segment address: 4AF00H IP offset: 0013H ________________________ Instruction address: 4AF13H Motaz K. of CS 34 . Dept. Saad.

Saad. of CS 35 . Dept.Example CS 4AF0 IP 0013 DS 04B1 Decode instruction: AO: MOV [0012] to AL 4AF13H A01200 Memory Motaz K.

Example CS 4AF0 IP 0013 DS 04B1 DS segment address:04B10H 0012H + IP offset: ________________________ 04B03H A01200 Data address: 04B22H Memory Motaz K. Dept. Saad. of CS 36 .

Dept.Example CS 4AF0 IP 0013 DS Data address: AX 04B03H 04B1 04B22 A01200 AH | 1B AL 04B22H 1B Memory Motaz K. Saad. of CS 37 .