IBM PC Assembly Language

bloatdecorumSoftware and s/w Development

Oct 30, 2013 (4 years and 9 days ago)

79 views

IBM PC Assembly Language

____________________________________________________________________________________

Chapter 2

Instruction Addressing and Execution


Objective: To explain the general requirements for
loading and executing programs on the PC.

-
Description of PC software environment

-
Functions of the operating system

-
Boot process

-
Loading of programs

-
Use of the stack

-
How instructions address data


IBM PC Assembly Language

____________________________________________________________________________________

Features of an Operating System

-
Device
-
independent access to resources

-
File management


directories of files and location of
files

-
Input/Output


requests for input or delivering of output
via interrupts

-
Program handling


request


load into memory


initialize for execution

-
Memory management


allocation of main memory for
programs

-
Interrupt handling


provides means for programs to
access external devices

IBM PC Assembly Language

____________________________________________________________________________________

The BIOS Boot Process

1.
Turn on computer

2.
Processor enters reset state


clears
memory, performs parity check of
memory

3.
Sets the CS register to FFFF0H and IP
to zero. CS:IP is the start of the BIOS
program in ROM

4.
BIOS starts to run continued….

IBM PC Assembly Language

____________________________________________________________________________________

5. Checks various ports to identify and
initialize devices

6. Establishes Interrupt Vector Table


to be
used later when interrupts occur

7. Established BIOS Data Areas


to contain
status of attached devices

8. Determines if there is a disk that contains
system files (usually the case)


IBM PC Assembly Language

____________________________________________________________________________________

9. Accesses the Bootstrap Loader from disk

10. Boot Loader loads system files into
memory and transfers control to them

11. Initialization of device
-
drivers, etc.

12. Initialization of internal system tables
and the system’s portion of the Interrupt
Vector Table.

IBM PC Assembly Language

____________________________________________________________________________________

Accessing Devices

User Program

Oper Sys

BIOS

Hardware / Devices

IBM PC Assembly Language

____________________________________________________________________________________

The System Program Loader

BIOS hands control to the OS. Then you can
request execution of programs.

Two types of programs:


.COM = one segment that contains code,
data and stack (usually small utilities)


.EXE = separate segments for code, data
and stack (more serious programs)

IBM PC Assembly Language

____________________________________________________________________________________

Loading an .EXE program

1.
Accesses program from disk.

2.
Constructs 256
-
byte program segment prefix (PSP)

3.
Stores program in memory immediately following the
PSP

4.
Loads the address of the PSP into the DS and ES
registers

5.
Loads the address of the code segment into CS and
sets IP to the offset to the first instruction

6.
Loads address of stack into SS and sets SP to the size
of the stack

7.
Transfers control to program for execution

(Do you see any problems with this?)

IBM PC Assembly Language

____________________________________________________________________________________

The Stack

Programs require a stack. The stack has three
main uses:

1.
If a program calls a subroutine for special
processing, the return address is saved on the
stack.

2.
The program that calls a subroutine may also
pass data via the stack.

3.
If the program has to do many calculations, it
can save the contents of the registers on the
stack and retrieve those contents later.

IBM PC Assembly Language

____________________________________________________________________________________

How the Stack works

1.
SS register contains the address of the
beginning of the stack. SP register contains
the size.

2.
When data is stored, it is stored at the high
end of the stack in memory, and then other
data are stored working backwards (or down)
in memory.

3.
There are operations, PUSH and POP, that
are used to put data on the stack or take it off.

IBM PC Assembly Language

____________________________________________________________________________________

Example:


INITIAL STACK

OFFSET

CONTENTS

SP = 36

34

0000

32

0000

30

0000

2E

0000

IBM PC Assembly Language

____________________________________________________________________________________

AFTER PUSH AX, when AX has 026B

OFFSET

CONTENTS

SP = 34

34

6B02

32

0000

30

0000

2E

0000

IBM PC Assembly Language

____________________________________________________________________________________

AFTER PUSH BX, when BX has 04E3

OFFSET

CONTENTS

SP = 32

34

6B02

32

E304

30

0000

2E

0000

IBM PC Assembly Language

____________________________________________________________________________________

AFTER POP BX, when AX has 026B

OFFSET

CONTENTS

SP = 34

34

6B02

32

E304

30

0000

2E

0000

IBM PC Assembly Language

____________________________________________________________________________________

Notes:

1.
You have to be careful how you Push and Pop
data. Try to do your Pops in exactly the
reverse order of your Pushs.

2.
PUSHF and POPF: Save and Restore the
Flags Register

3.
PUSHA and POPA: For AX, BX, CX, DX, SP,
BP, SI and DI

4.
PUSHAD and POPAD for the extended
registers.

IBM PC Assembly Language

____________________________________________________________________________________

Instruction Execution and Addressing

Basic steps in Executing an Instruction

1.
Fetch the next instruction and place it in the
instruction queue.

2.
Decode the instruction; calculate addresses
that reference memory; deliver data to the
ALU; increment the IP.

3.
Execute the instruction; perform requested
operation; store results; set flags

IBM PC Assembly Language

____________________________________________________________________________________

Pipelining

Overlapping some of the operations:


FETCH

DECODE


EXECUTE



FETCH


DECODE EXECUTE




FETCH DECODE EXECUTE




IBM PC Assembly Language

____________________________________________________________________________________

Steps in executing a program (example)

1.
Program loader decides to load a .exe
program at location 05BE0H.

2.
Loader initializes CS to 05BE[0]H.

3.
Loader initializes IP to 0.

4.
CS:IP is now the address of the first
instruction, and it is loaded and executed.

5.
If the 1
st

instruction is 2 bytes long, then IP is
incremented by 2. So now CS:IP points to the
2
nd

instruction.


IBM PC Assembly Language

____________________________________________________________________________________

Example continued:

6. Assume some instructions have been executed,
and now IP = 23H. So the next instruction is at
address 5C03H.

7. Perhaps the instruction is a MOV instruction.



A01600

MOV AL, [0016]


This moves the data from offset 16H in the data
segment into the AL register. NOTE THE
DIRECTION!


8. In this example, A0 = MOV and 1600 (in
memory) is actually 0016H, the offset address.

IBM PC Assembly Language

____________________________________________________________________________________

9. Suppose the program has also initialized the DS
register to 5D10H (05D1[0]H)

10. The processor adds 5D10H to 16H to obtain
05D26H. This is the address of the data byte to
be moved.

11. Assume the data byte has the value 4AH

12. The processor then copies 4AH into the AL
register.

13. At this point in time, what would be the
contents of the IP register?

IBM PC Assembly Language

____________________________________________________________________________________

Slightly different:

1.
An instruction wants to move the contents of
the AX register into two adjacent bytes of the
data segment starting at 0016H offset.

2.
The AX register has a 0248H in it.

3.
The instruction is MOV [0016],AX

4.
After this instruction is executed, relative to
DS, 0016H has a 48H and 0017H has a 02H.


IBM PC Assembly Language

____________________________________________________________________________________

Instruction Operands

An instruction may have 0, 1, 2 or 3
operands.

You don’t have to specify data addresses by
numbers. You can use names.

There are different ways of doing things:
names, numbers, names in square
brackets, etc.

See next page for examples:

IBM PC Assembly Language

____________________________________________________________________________________

WORDX

DW


0






MOV CX, WORDX ; MOVE CONTENTS OF WORDX INTO CX


MOV CX, 25 ; MOVE THE VALUE 25 INTO CX


MOV CX, BX ; MOVE THE CONTENTS OF THE BX





; REGISTER INTO THE CX REGISTER


MOV CX,[BX] ; MOVE THE CONTENTS OF THE LOCATION





; ADDRESSED BY BX INTO CX


The [ ] is called the index operator. It means to take the
contents of the referenced item and use those contents
as an offset address into the data segment. In the last
line above, the address that will be DS:BX.

IBM PC Assembly Language

____________________________________________________________________________________

Protected Mode

1.
If only one program is in memory, things are easy. But
what if there are more?

2.
In protected mode, the processor can switch from one
task to another. Each program will have its own area of
memory, but the processor must protect each area and
preserve the status of the programs.

3.
In “real mode”, segment registers contain the actual
segment addresses, but are limited to addressing one
megabyte.

Cont….

IBM PC Assembly Language

____________________________________________________________________________________

4. In protected mode, the processor must keep
several additional tables:

a.
Local Descriptor Tables: one table per task.

b.
Interrupt Descriptor Table: contains data
regarding interrupt operations

c.
Global Descriptor Table: contains the
addresses of the various local descriptor
tables.

d.
The processor has additional registers: LDT,
IDT and GDT.