You are on page 1of 47

CSC 232 Computer Organization

and Architecture

Dr. Charles Nche


Structure of von Neumann machine
3.0 Assembly Language Programming

• On completing this chapter, the students should be


able to;
• Explain a Simple Machine and Assembly language.
• Discuss Instruction Mnemonics and Syntax
• Differentiate Assembler Directives and
Commands
• Assembly and Execute Programs
• Explain Assemblers, Data Structures, Linkers and
Loaders.
• Know the main features of the Intel X86
1.8 microprocessor family.
3.0 Assembly Language Programming

• Introduce basic concepts and principles involved in


the design of an instruction set of a machine.
• Consider the issues related to assembly language
programming.
• Programming in assembly can result in machine
code that is much smaller and much faster than that
generated by a compiler of a high-level language.
• Small and fast code could be critical in some
embedded and portable applications, where
resources are very limited
1.9
3.0 Assembly Language Programming

• A computer program can be represented at different


levels of abstraction.
• Programs written at any level higher than the
machine language must be translated to the binary
representation before a computer can execute them.
• An assembly language program is a symbolic
representation of the machine language program.
• Machine language is pure binary code, whereas
assembly language is a direct mapping of the binary
code onto a symbolic form that is easier for humans
1.10to understand and manage.
3.0 Assembly Language Programming

• Converting the symbolic representation into machine


language is performed by a special program called the
assembler.
• An assembler is a program that accepts a symbolic
language program (source) and produces its machine
language equivalent (target).
• Translating a program into binary code, the assembler will
replace symbolic addresses by numeric addresses, replace
symbolic operation codes by machine operation codes,
reserve storage for instructions and data, and translate
constants into machine representation.

1.11
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
1.12 Data Register (DR).
3.1 A Simple Machine

• The PC contains the address of the next instruction


to be executed.
• The IR contains the operation code portion of the
instruction being executed.
• The AR contains the address portion (if any) of the
instruction being executed.
• The AC serves as the implicit source and
destination of data.
• The DR is used to hold data. The memory unit is
made up of 4096 words of storage.
•1.13The word size is 16 bits.
3.1 A Simple Machine

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

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

1.16
3.1 A Simple Machine

1.17
3.1 A Simple Machine

1.18
3.1 A Simple Machine

1.19
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
1.20manageable.
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.

1.21
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.
• The opcode field contains the symbolic abbreviation of a
given operation. The operand field consists of additional
information or data that the opcode requires.
• The operand field may be used to specify constant, label,
immediate data, register, or an address.
• The comments field provides a space for documentation to
explain what has been done for the purpose of debugging
and maintenance.

1.22
3.2 Instruction Mnemonics and Syntax

• START LD X \ copy the contents of location X into AC.

• BRA START \ go to the statement with label START


• In addition to program instructions, an assembly
program may also include pseudo instructions or
assembler directives.
• Assembler directives are commands that are
understood by the assembler and do not correspond
to actual machine instructions.
• For example, the assembler can be asked to allocate
memory storage or initialized a memory location.

1.23
3.2 Instruction Mnemonics and Syntax

1.24
3.2 Instruction Mnemonics and Syntax

Example 1 Let us write a machine language program that adds


the contents of memory location 12 (00C-hex), initialized to
350 and memory location 14 (00E-hex), initialized to 96, and
store the result in location 16 (010-hex), initialized to 0.

1.25
3.2 Instruction Mnemonics and Syntax

Example 2 In this example, we will write an assembly


program to perform the multiplication operation: Z XY, where
X, Y, and Z are memory locations..

1.26
3.3 Assembler Directives and Commands

• In writing assembly language programs for a


specific architecture, a number of practical
issues need to be considered:
• Assembler directives (pseudo-operations)
• Use of symbols
• Use of synthetic operations
• Assembler syntax
• Interaction with the operating system.

1.27
3.3 Assembler Directives and Commands

• Assembler directives are commands that are understood by


the assembler and do not correspond to actual machine
instructions.
• It affect the way the assembler performs the conversion of
assembly code to machine code.
• Special assembler directives can be used to instruct the
assembler to place data items such that they have proper
alignment.
• For proper alignment of data, data of n-bytes width must be
stored at an address that is divisible by n, for example, a
word that has a two-byte width has to be stored at locations
having addresses divisible by two.
1.28
3.3 Assembler Directives and Commands

• 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.
• 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.
1.29
3.3 Assembler Directives and Commands

• Assemblers usually impose some conventions in


referring to hardware components such as registers
and memory locations.

• One such convention is prefixing of immediate


values with the special characters (#) or a register
name with the character (%).

1.30
3.3 Assembler Directives and Commands

• The underlying hardware in some machines cannot


be accessed directly by a program. The OS plays
the role of mediating access to resources such as
memory and I/O facilities.

• Interactions with OS can take place in the form of a


code that causes the execution of a function that is
part of the OS. These functions are called system
calls.

1.31
3.4 Assembly and Execution of Programs

• A program written in assembly language needs to be


translated into binary machine language before 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
1.32begins execution.
3.4 Assembly and Execution of Programs

• .

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

1.34
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.
1.35
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..
1.36
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.

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

1.38
3.4 Assembly and Execution of Programs

• Simplified pass one in a two-pass assembler

1.39
3.4 Assembly and Execution of Programs

• Simplified pass two in a two-pass assembler

1.40
3.4 Assembly and Execution of Programs

• Main features of the Intel X86 microprocessor family

1.41
3.4 Assembly and Execution of Programs

• Base register sets of the X86 programming model

1.42
3.4 Assembly and Execution of Programs

• The X86 Flag Register

• X86 Status Flags

1.43
3.4 Assembly and Execution of Programs

• Direct Addressing in the X86 Family

1.44
3.4 Assembly and Execution of Programs

• Indirect Addressing using BPR in the X86 Family

1.45
3.4 Assembly and Execution of Programs

• A Sample of the X86 Data Movement Instructions

1.46
3.4 Assembly and Execution of Programs

• A Sample of the X86 Arithmetic Instructions

1.47
3.4 Assembly and Execution of Programs

• A Sample of the X86 Shift and Rotate Instructions

1.48
3.4 Assembly and Execution of Programs

• A Sample of the X86 Control Transfer Instructions

1.49
3.4 Assembly and Execution of Programs

• A Sample of the X86 Processor Control Instructions

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

1.51
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..
1.52

You might also like