Sie sind auf Seite 1von 35

Chapter 3

Assembly Language Programming

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.1 A Simple Machine


Machine language is the native language of a given processor. Assembly language is the symbolic form of machine language. An accumulator-based processor has:
five 16-bit registers:
Program Counter (PC), Instruction Register (IR), Address Register (AR), Accumulator (AC), and Data Register (DR).

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.1 A Simple Machine


This simple processor supports three types of instructions:
data transfer, data processing, and program control.

The data transfer operations are:


load, store, and move data between the registers AC and DR.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.1 A Simple Machine


The data processing instructions are:
add, subtract, and, and not.

The program control instructions are:


jump and conditional jump.

The instruction size is 16 bits:


4 bits for the operation code and 12 bits for the address.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.1 A Simple Machine


0 1 2

Address PC ALU IR AR Data Control Unit Instructions AC DR

Memory

4095

CPU

A Simple Machine

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.1 A Simple Machine


Instruction Set of the Simple Processor
Operation Code 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 adr adr adr adr Operand Stop execution Load operand from memory (location adr) into AC Store contents of AC in memory (location adr) Copy the contents AC to DR Copy the contents of DR to AC Add DR to AC Subtract DR from AC And bitwise DR to AC Complement contents of AC Jump to instruction with address adr Jump to instruction adr if AC = 0 Mostafa Abd-El-Barr & Hesham El-Rewini Meaning of Instruction

Fundamentals of Computer Organization and Architecture

3.1 A Simple Machine


Simple Machine Language Program in Binary
Memory Location (bytes) Binary instruction Description

0000 0000 0000 0000 0000 0010 0000 0000 0100 0000 0000 0110 0000 0000 1000 0000 0000 1010 0000 0000 1100 0000 0000 1110 0000 0001 0000

0001 0000 0000 1100 0011 0000 0000 0000 0001 0000 0000 1110 0101 0000 0000 0000 0010 0000 0001 0000 0000 0000 0000 0000 0000 0001 0101 1110 0000 0000 0110 0000 0000 0000 0000 0000

Load the contents of location 12 in AC Move contents of AC to DR Load the contents of location 14 into AC Add DR to AC Store contents of AC in location 16 Stop Data value 350 Data value is 96 Data value is 0

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.1 A Simple Machine


Simple Machine Language Program in Hexadecimal
Memory Location (bytes) Hex instruction

000 002 004 006 008 00A 00C 00E 010

100C 3000 100E 5000 2010 0000 015E 0060 0000

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.2 Instruction Mnemonics and Syntax


Assembly programs are written with short abbreviations called mnemonics. A mnemonic is an abbreviation that represents the actual machine instruction. Assembly language programming is the writing of machine instructions in mnemonic form, where each machine instruction is replaced by a mnemonic. Clearly the use of mnemonics would make programming at this low level easier and more manageable.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.2 Instruction Mnemonics and Syntax


An assembly program consists of a sequence of assembly statements. Each line of an assembly program is split into the following four fields:
label, operation code (opcode), operand, and comments.
Label (Optional) Operation Code (Required) Operand (Required in some instructions) Comment (Optional)

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.2 Instruction Mnemonics and Syntax


A label is an identifier that can be used on a program line so that the line can be branched to. It can also be used to access data using symbolic names.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.2 Instruction Mnemonics and Syntax


Mnemonic STOP LD ST MOVAC MOV ADD SUB AND NOT BRA BZ adr adr x x Operand Stop execution Load operand from memory (location x) into AC Store contents of AC in memory (location x) Copy the contents AC to DR Copy the contents of DR to AC Add DR to AC Subtract DR from AC And bitwise DR to AC Complement contents of AC Jump to instruction with address adr Jump to instruction adr if AC = 0 Mostafa Abd-El-Barr & Hesham El-Rewini Meaning of Instruction

Fundamentals of Computer Organization and Architecture

3.3 Assembler Directives and Commands


Assembler directives are commands that are understood by the assembler and do not correspond to actual machine instructions. Assembler directives are commands that are understood by the assembler and do not correspond to actual machine instructions. In assembly language programs symbols are used to represent numbers in order to make the code easier to read, understand, and debug. Symbols are translated to their corresponding numerical values by the assembler.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.3 Assembler Directives and Commands


The use of synthetic operations helps assembly programmers to use instructions that are not directly supported by the architecture. These are then translated by the assembler to a set of instructions defined by the architecture.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.4 Assembly and Execution of Programs


A program written in assembly language needs to be translated into binary machine language before it can be executed. Three steps exist in the assembly and execution process.
The assembler reads the source program in assembly language and generates the object program in binary form. The object program is passed to the linker. The linker will check the object file for calls to procedures in the link library. The linker will combine the required procedures from the link library with the object program and produce the executable program. The loader loads the executable program into memory and branches the CPU to the starting address. The program begins execution.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.4 Assembly and Execution of Programs

Assembly and Execution Process

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.4 Assembly and Execution of Programs


Assemblers
Assemblers are programs that generate machine code instructions from a source code program written in assembly language. The assembler will replace symbolic addresses by numeric addresses, Will replace symbolic operation codes by machine operation codes, Will reserve storage for instructions and data, and translate constants into machine representation.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.4 Assembly and Execution of Programs


Assemblers
A simple assembler scans the entire assembly program twice, where each scan is called a pass. During the first pass, it generates a table that includes all symbols and their binary values. This table is called the symbol table. During the second pass, the assembler will use the symbol table and other tables to generate the object program, and output some information that will be needed by the linker.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.4 Assembly and Execution of Programs


Data Structures
The assembler uses at least three tables to perform its functions: symbol table: is generated in pass one, has an entry for every symbol in the program. opcode table: provides information about the operation codes. Associated with each symbolic opcode in the table are its numerical value and other information about its type, its instruction length, and its operands. pseudo instruction table: Each entry refers the assembler to a procedure that processes the pseudo instruction when encountered in the program.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.4 Assembly and Execution of Programs


Data Structures
In order to keep track of the instruction locations, the assembler maintains a variable called Instruction Location Counter (ILC). The ILC contains the value of memory location assigned to the instruction or operand being processed. The ILC is initialized to 0 and is incremented after processing each instruction.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.4 Assembly and Execution of Programs


Linker and Loader
The linker is the entity that can combine object modules that may have resulted from assembling multiple assembly modules separately. The loader is the operating system utility that reads the executable into memory and start execution.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.4 Assembly and Execution of Programs


Simplified pass one in a two-pass assembler

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.4 Assembly and Execution of Programs


Simplified pass two in a two-pass assembler

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.5 Example: the X86 Family


Main features of the Intel X86 microprocessor family
Feature Date Introduced Data bus Address bus Operating speed Instruction Cache size Data Cache size Physical memory Data word size 8086 1978 8 bits 20 bits 5,8,10 MHz NA NA 1 Mbytes 16 bits 286 1982 16 bits 24 bits 6,8,10, 12.5, 16, 20 MHz NA NA 16 Mbytes 16 bits 386 1985 32 bits 32 bits 16, 20,25, 33, 40, 50 MHz 16 bytes 256 bytes 4 Gbytes 16 bits 486 1991 32 bits 32 bits 25, 33, 50 MHz 32 bytes 8 Kbytes 4 Gbytes 32 bits Pentium 1993 64 bits 32 bits 50, 60, 66, 100 MHz 8 Kbytes 8 Kbytes 4 Gbytes 32 bits

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.5 Example: the X86 Family


The base register sets of the X86 programming model
31
EAX EBX ECX EDX ESI EDI EBP ESP

16

15
AH BH CH DH SI DI

7
AL

0 BL CL DL

SP BP CS SS DS ES FS GS
IP

31
E Flags

16

15
Flags H Flags L

0 Mostafa Abd-El-Barr & Hesham El-Rewini

Fundamentals of Computer Organization and Architecture

3.5 Example: the X86 Family


The X86 Flag Register

X86 Status Flags

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.5 Example: the X86 Family


Direct Addressing in the X86 Family

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.5 Example: the X86 Family


Indirect Addressing using BPR in the X86 Family

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.5 Example: the X86 Family


A Sample of the X86 Data Movement Instructions

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.5 Example: the X86 Family


A Sample of the X86 Arithmetic Instructions

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.5 Example: the X86 Family


A Sample of the X86 Shift and Rotate Instructions

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.5 Example: the X86 Family


A Sample of the X86 Control Transfer Instructions

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.5 Example: the X86 Family


A Sample of the X86 Processor Control Instructions

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.6 Summary
A machine language is a collection of the machine instructions represented in 0s and 1s. Assembly language provides easier to use symbolic representation, in which an alphanumeric equivalence to machine language is used. An assembler is a program that accepts a symbolic language program (source program) and produces its machine language equivalent (target program). Programming in assembly can result in machine code that is smaller and faster than that generated by a compiler of a high level language.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

3.6 Summary
Assembly programmers have access to all the hardware features of the target machine that might not be accessible to high-level language programmers. In addition, learning assembly languages can be of great help in understanding the low level details of computer organization and architecture. In this chapter a general overview of assembly language and its programming was provided. The programmer view of the X86 Intel microprocessor family of processors was also introduced as a real world example.

Fundamentals of Computer Organization and Architecture

Mostafa Abd-El-Barr & Hesham El-Rewini

Das könnte Ihnen auch gefallen