asm1-1220345581297677-8

Upload: ike-mag-away-gaamil

Post on 02-Mar-2016

8 views

Category:

Documents


0 download

DESCRIPTION

assembly language

TRANSCRIPT

  • Assembly LanguageInstruction Addressing and ExecutionMotaz K. SaadSpring 2007*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Lesson planReview some concepts in the first weekFirst assembly program with EMU8086Related concepts with the first program:Loading program Boot processHandling the stack*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Recalling main concepts*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Recalling main concepts*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Recalling main conceptsSegment: special areas defined to contain CODE, DATA and STACKParagraph boundary: location evenly divisible by 16 or 10H

    *Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Recalling main concepts

    Stack SegmentData SegmentCode SegmentSSDSCSSegment Registers*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • .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

    *Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Types of programs*.COM and *.EXE files*.COM: consists of one segment containing code, data and stack*.exe: separate code, data and stack segmentsCOM is small, (small utility program or can be a resident program). We will focus on EXE program in this course.

    *Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Loading *.exe programAccess *.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 executionPSP: Created by the operating system and contains information about some interrupt vectors, addresses of system fields etc.

    *Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • PSP*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • 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

    *Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Memory ModelThere 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.

    *Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Real and Protected mode*Motaz K. Saad, Dept. of CS

    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

    Motaz K. Saad, Dept. of CS

  • Protected modeIs 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:*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Booting processWhat is booting?The process of starting or restarting a computer*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Booting processprocessorBIOShard diskCD-ROM drive(RAM) memory modulesCMOSfloppy disk driveStep 6expansion cardsHow does a personal computer boot up?*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • 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)*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • 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).*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • 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.*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • How does a personal computer boot up?The operating system executes programs in the StartUp folderRegistry - Several files that contain the system configuration informationRegistry is constantly accessed during the computer's operationStartUp folder - Contains a list of programs that open automatically when you boot the computer*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • BIOS Boot processBIOS routineFFFF0HInterrupt VectorTableBIOS Data AreasAccess the bootstraploaderCheck portsInitialize devices*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • BIOS Boot processInternally, 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.

    *Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • 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

    *Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • STACKThe word is from data structureLast In, First Out (LIFO) mechanismSTACK in OS has three main functions:Contains return address DataContent of present registers

    *Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • STACKPUSHDecrease SP by 2 and store a value therePOPReturn a value from stack and increase SP by 2*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Lesson planReview loading an *.exe fileConcept of execution of instructionsPractice:Execution of instructions*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • 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 executionLoading *.exe file*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Loading *.exe fileThe sequence of segments (code, data, and stack) is givenSS: contains the address of the beginning of the stackCS: contains the address of the beginning of the code segmentDS: contains the address of the beginning of the data segmentSP: contains the size of stack

    *Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Practice2B360HPSPStack SegmentData SegmentCode SegmentMemory*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Practice2B360HPSPStack SegmentData SegmentCode SegmentMemoryPSP 2B360HPSP size 100HOffset 0HSS 2B460H (stored as 2B46)2B46HSS*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Practice2B360HPSPStack SegmentData SegmentCode SegmentMemory2B46HSSPSP 2B360HPSP size 100HOffset 30H 70HCS 2B500H (stored as 2B50)CS2B50H*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Practice2B360HPSPStack SegmentData SegmentCode SegmentMemory2B46HSSCS2B50H2B36H2B36HDSESSP0030H*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Instruction Execution and AddressingExecuting an instruction includeFetch the next instruction, put to a queue (QUEUE: FIFO vs. STACK LIFO)Decode the instructionExecute the instruction

    *Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Example4AF0CS0013IP 4AF13H 04B1DS*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Example4AF0CS0013IP 4AF13H

    A01200

    Memory04B1DSDecode instruction:AO: MOV [0012] to AL*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Example4AF0CS0013IP 04B03H

    A01200

    Memory04B1DS04B22H*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

  • Example4AF0CS0013IP 04B03H

    A01200

    1B

    Memory04B1DS04B22Data address:04B22H*Motaz K. Saad, Dept. of CS

    Motaz K. Saad, Dept. of CS

    *COM is small, (small utility program or can be a resident program). We will focus on EXE program in this course. *PSP: Created by the operating system and contains information about some interrupt vectors, addresses of system fields etc.

    PSP address is loaded into both DS and ES. But DS is supposed to contain the address of the data segment. So in our program, we need to initialize the DS register*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.*Interest part: father of Ctrl-Alt-Del: david Bradley, long time employee of IBM, who also has a Ph.D in EE from Purdue in Indiana

    *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)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).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.The operating system executes programs in the StartUp folderRegistry - Several files that contain the system configuration informationRegistry is constantly accessed during the computer's operationStartUp folder - Contains a list of programs that open automatically when you boot the computer

    *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.

    *Queue