Download CS- 203 Assembly Language Programming

Document related concepts
no text concepts found
Transcript
Computer Organization and Assembly
Language
CS-203
Lecture-2
Introduction to Microprocessors,
Assembly Language
& Registers
Lecturer: Syeda Nazia Ashraf
1
Microprocessor
• Microprocessor is an electronic circuit that functions as the
central processing unit (CPU) of a computer, providing
computational control.
• Microprocessors are also used in other advanced electronic
systems, such as computer printers, automobiles, and jet
airliners.
2
Processor Integration
Early computers had many separate chips for the different
portions of a computer system
Registers
Control
ALU
Memory
3
Microprocessors
Data Bus
Address Bus
CPU
(ALU +
Reg +
control)
Memory
I/O
Devices
Control Bus
First microprocessors placed control, registers, arithmetic logic
unit in one integrated circuit (one chip).
4
Modern Processors
Modern microprocessors (general purpose Processors) also integrate memory
on chip for faster access. External memory and I/O components still required.
Memory integrated on the microprocessor is called cache memory.
Data Bus
CPU
Registers,
ALU,
Fetch,
Exe Logic,
Bus logic,
Cache Memory
Address Bus
Memory
I/O
Devices
Control Bus
5
Microcontrollers
Microcontrollers integrate all of the components (control,
memory, I/O) of a computer system into one integrated circuit.
Microcontrollers are intended to be single chip solutions for
systems requiring low to moderate processing power.
Microcontroller
6
Microprocessor vs. Microcontroller
Microprocessor
– CPU is stand-alone,
RAM, ROM, I/O, timer
are separate
– designer can decide on
the amount of ROM,
RAM and I/O ports.
– general-purpose
Microcontroller
• CPU, RAM, ROM, I/O and
timer are all on a single chip
• fix amount of on-chip ROM,
RAM, I/O ports
• single-purpose
7
Assembly language programming
• Learning assembly language programming will
help understanding the operations of the
microprocessor
• To learn:
– Need to know the functions of various registers
– Need to know how external memory is organized and
how it is addressed to obtain instructions and data
(different addressing modes)
– Need to know what operations (or the instruction set)
are supported by the CPU.
8
Why Learn Assembly Language?
•
•
•
•
Learn how a processor works
Explore the internal representation of data and instructions
Allows creation of small and efficient programs
Provides a convenient way to directly access the computers
hardware
• Programmers write subroutine also known as Interface
Subroutine / device drivers in assembly language and call them
from high-level language programs.
9
Advantages of Assembly Language
• Performance:
A well-written Assembly language program produces a faster,
shorter machine language program.
For Some applications speed and size is critical
• Access to hardware:
Some operations, such as reading or writing to specific
memory locations & I/O ports can be done easily in Assembly
but may be impossible by a higher level language.
• Studying ASM language gain a feeling of the way the computer
thinks and the way things happen inside the computer.
10
Assembly Language
• Use instruction mnemonics that have one-to-one
correspondence with machine language.
 An instruction is a symbolic representation of a single
machine instruction
 Consists of:
 label
always optional
 mnemonic
always required
 operand(s)
required by some instructions
 comment
always optional
11
Assembly Language fields
(Label)
A name defined with specific attribute maximum length is 31 characters and end
with;
Characters such as A – Z / 0-9/?, @, _, and $
Mnemonic
Abbreviated spellings of instructions
Contains 2 –to -7 letter acronym for the instruction
[Operand]
Part of instruction that represents a value on which the instruction or directive
acts.
Contain either 1 or 2 operands, separated from the mnemonic by 1 space or tab.
[;comment]
All or part of a statement that is ignored by the Assembler, preceded by a
semicolon ;
12
General Format for an Assembly
language statement
Label
Instruction
Comment
• Start:
Mov AX, BX
; copy BX into AX
• Start is a user defined name and you only put in a
label in your statement when necessary!!!!
• The symbol : is used to indicate that it is a label
• Mov is mnemonic
13
Sample Program Written in Debug
1. mov ax, 5
ax
2. add ax, 10
ax
05
Memory
15
35
3. add ax, 20
ax
35
4. mov [0120], ax
ax
35
5. int 20
011C
011E
0120
0122
0124
0126
(.COM exit interrupt)
14
Essential Tools
• Assembler is a program that converts source-code programs
into a machine language (object file).
• Linker joins together two or more object files and produces a
single executable file.
• Debugger loads an executable program, displays the source
code, and lets the programmer step through the program one
instruction at a time, and display and modify memory.
• Emulator allows you to load and run assembly language
programs, examine and change contents of registers. Example:
EMU8086, DOSbox 0.74 etc.
15
Assembly Program
• The native language is machine language (using
0,1 to represent the operation)
• A single machine instruction can take up one or
more bytes of code
• Assembly language is used to write the program
using alphanumeric symbols (or mnemonic), eg
ADD, MOV, PUSH etc.
• The program will then be assembled (similar to
compiled) and linked into an executable program.
• The executable program could be .com
(command), .exe(executable), or .bin(binary) files
16
Flow of program development
Program
.asm
Assemble
Object file
.obj
link
Executable file
.exe
17
Registers
• In assembly programming, you cannot operate on two
memory locations in the same instruction
• So you usually need to store (move) value of one
location into a register and then perform your
operation
• After the operation, you then put the result back to
the memory location
• Therefore, one form of operation that use very
frequent is the store (move) operation!
• And using registers!
18
Assembly Program
• Each instruction is represented by one assembly
language statement
• The statement must specify which operation
(opcode) is to be performed and the operands
• Eg ADD AX, BX
• ADD is the operation
• AX is called the destination operand
• BX is called the source operand
• The result is AX = AX + BX
• When writing assembly language program, you
need to think in the instruction level
19
Example
• In C/C++, you can do A = (B+C)*100
• In assembly language, only one instruction
per statement
A=B
A = A+C
A = A*100
; only one instruction - MOVE
; only one instruction - ADD
; only one instruction - Multiply
20
Example
• In C/C++ A = B+C ; A, B, C are variables
• In assembly language A,B, C representing
memory locations so you cannot do A =
B+C
– MOV AL, B ; move value of B into AL register
– ADD, AL, C ; Add AL = AL +C
– MOV A, AL ; put AL result to A
21
Data registers
• AX, BX, CX and DX – these are the general purpose
registers but each of the registers also has special
function
Example
– AX is called the accumulator – to store result in
arithmetic operations
• Registers are 16-bit but can be used as 2 8-bit storage
• Each of the 4 data registers can be used as the source or
destination of an operand during an arithmetic, logic,
shift, or rotate operation.
22
8086 Software Model
23
Software model
• In 8086, memory is divided into segments
• Only 4 64K-byte segments are active and these are: code,
stack, data, and extra
• When you write your assembly language program for an
8086, theoretically you should define the different segments!
• To access the active segments, it is via the segment register:
CS (code), SS (stack), DS (data), ES (extra). Two additional
segment registers, FS and GS, with no specific uses defined by
the hardware
• So when writing assembly language program, you must make
use of the proper segment register or index register when
you want to access the memory
24
(16-bit) Segment Registers
•
•
•
•
CS
DS
SS
ES
CS Code Segment
DS Data Segment
SS Stack Segment
ES Extra Segment
25
General-Purpose Registers
32-bit General-Purpose Registers
EAX
EBP
EBX
ESP
ECX
ESI
EDX
EDI
16-bit Segment Registers
EFLAGS
EIP
CS
ES
SS
FS
DS
GS
26
Registers
General Purpose
Function:
Temporary Storage
of
Intermediate Results
27
Registers
Accumulator Register
Function:
Mathematical and Logical Operations
28
16-BIT General Purpose Registers
•
•
•
•
AX
BX
CX
DX
AH
8-bit
AH,AL
BH,BL
CH,CL
DH,DL
A Accumulator Register
B Base Register
C Counter Register
D Destination Register
16-bit
16-bit
AX
BX
AL
8-bit
BH
8-bit
BL
8-bit
29
Data types
• Data can be in three forms: 8-bit, 16-bit, or 32-bit
(double word)
• Integer could be signed or unsigned and in byte-wide
or word-wide
• For signed integer (2’s complement format), the MSB
is used as the sign-bit (0 for positive, 1 for negative)
• Signed 8-bit integer 127 to –128,
• For signed word 32767 to –32768
• Latest microprocessors can also support 64-bit or
even 128-bit data
• In 8086, only integer operations are supported!
30
Data register
• In based addressing mode, base register BX is
used as a pointer to an operand in the current
data segment.
• CX is used as a counter in some instructions, eg.
CL contains the count of the number of bits by
which the contents of the operand must be
rotated or shifted by multiple-bit rotate
• DX, data register, is used in all multiplication and
division, it also contains an input/output port
address for some types of input/output
operations
31
Registers
Flag / Program Status Word
•
•
•
•
Function:
Collection of different Boolean Information.
Each bit has an independent meaning.
Contains the current state of processor.
Its successors, the EFLAGS and RFLAGS registers, are
32 bits and 64 bits wide, respectively.
32
Flag/Status Register
There are 2 types of FLAGS register.
•Status Flags
•Control Flags
This register is 16 bits wide. The Status Flags are located in 0, 2,
4, 6, 7 and 11 bits. Control Flags are located in 8, 9 and 10 Bits
and the System Flags are located in 12,13 and 14 bits and others
left bits are reserved bits.
33
34
Status Flags
There are 6 status flags in 8086 processor.
• Carry Flag
• Parity Flag
• Adjust Flag
• Zero Flag
• Sign Flag
• Overflow Flag
35
NOTE: MSB is high order or left-most bit
36
Carry Flag
111111111111111
1111111111111111
+ 0000000000000001
0000000000000000
16 – bit Accumulator
Carry Flag = CF
37
38
(Adjust flag)
(generated out of the 4 LSBs)
39
40
41
42
43
no
7FFFh + 7FFFh = FFFEh
(in decimal)
44
45
Control Flags
There are 3 Control flags in 8086 processor.
• Trap Flag
• Interrupt Enable Flag
• Direction Flag
46
Note: Direction Flag: Controls the left-to-right or right-to-left direction
of string processing. When it is set to 0 (using the clear-direction-flag instruction
CLD, it means that string is processed beginning from lowest to highest address
and instructions mode is called auto-incrementing mode. The source index and
destination index (like MOVS) will increase both of them. In case it is set to 1
(using the set-direction-flag instruction STD),the string is processed from highest
to lowest address. This is called auto-decrementing mode . This flag is used to
determine the direction (forward or backward) in which several bytes of data will
be copied from one place in the memory, to another. The direction is important
mainly when the original data position in memory and the target data position
overlap.
47
Note: An external interrupt is a computer system interrupt that happens as a
result of outside interference, whether that’s from the user, from peripherals, from
other hardware devices or through a network. These are different than internal
interrupts that happen automatically as the machine reads through program
instructions.
48
Registers
Program Counter
Instruction Pointer
Function:
Address of next instruction to be executed
49
Registers
Pointer / Index / Base
Function:
Holds the Address of Operands
50
Accessing Parts of Registers
• Use 8-bit name, 16-bit name, or 32-bit name
• Applies to EAX, EBX, ECX, and EDX
8
8
AH
AL
AX
EAX
8 bits + 8 bits
16 bits
32 bits
51
Registers
Pointer / Index / Base
•
•
•
•
•
SI
DI
IP
SP
BP
SI Source Index
DI Destination Index
IP Instruction Pointer
SP Stack Pointer
BP Base Pointer
52
Index and Base Registers
• Some registers have only a 16-bit name for
their lower half:
53
Pointer and Index Registers
• Stack – is used as a temporary storage
• Data can be stored by the PUSH instruction and
extracted by the POP instruction
• Stack is accessed via the SP (Stack Pointer) and BP
(Base Pointer)
• The BP contains an offset address in the current
stack segment. This offset address is employed
when using the based addressing mode and is
commonly used by instructions in a subroutine
that reference parameters that were passed by
using the stack
54
Pointer and Index Register
• Source index register (SI) and Destination index
register (DI) are used to hold offset addresses for
use in indexed addressing of operands in memory
• When indexed type of addressing is used, then SI
refers to the current data segment and DI refers
to the current extra segment
• The index registers can also be used as source or
destination registers in arithmetic and logical
operations. But must be used in 16-bit mode
55