SP Week 3-4
SP Week 3-4
Contents
• Machine Considerations for Assemblers
• Modularization and program assembly
• Memory Management
Machine Considerations for Assemblers
• Machine code is a computer program written in
machine language instructions that can be executed
directly by a computer's central processing unit (CPU).
• Each instruction causes the CPU to perform a very
specific task, such as a load, a store, a jump, or an
arithmetic logic unit (ALU) operation on one or more
units of data in central processing unit (CPU) registers or
memory.
Instruction set architecture
An instruction set architecture (ISA) is an
abstract model of a computer.
It is also referred to as architecture or computer
architecture.
Instruction set architecture continued..
• One instruction may have several fields, which identify the
logical operation, and may also include source and destination
addresses and constant values.
• This is the MIPS "Add Immediate" instruction, which allows
selection of source and destination registers and inclusion of a
small constant.
• MIPS (Microprocessor without Interlocked Pipelined
Stages) is a reduced instruction set computer (RISC)
instruction set architecture (ISA) developed by MIPS
Computer Systems
• MIPS I has thirty-two 32-bit general-purpose registers
(GPR).
• Instructions are divided into three types: R, I and J. Every
instruction starts with a 6-bit opcode.
• In addition to the opcode,
• R-type instructions specify three registers, a shift
amount field, and a function field;
• I-type instructions specify two registers and a 16-
bit immediate value;
• J-type instructions follow the opcode with a 26-bit
jump target.
• The following are the three formats used for
the core instruction set:
• Load instructions source the base from the contents of a GPR (rs)
and write the result to another GPR (rt).
• Store instructions source the base from the contents of a GPR (rs)
and the store data from another GPR (rt).
• All load and store instructions compute the memory address by
summing the base with the sign-extended 16-bit immediate.
• MIPS I has instructions to perform addition and subtraction.
These instructions source their operands from two GPRs (rs and
rt), and write the result to a third GPR (rd).
Number of operands
• Instruction sets may be categorized by the maximum number
of operands explicitly specified in instructions.
• (In the examples that follow, a, b, and c are (direct or
calculated) addresses referring to memory cells, while reg1
and so on refer to machine registers.)
• C = A+B
• 0-operand (zero-address machines), so called stack machines:
All arithmetic operations take place using the top one or two
positions on the stack: push a, push b, add, pop c.
– C = A+B needs four instructions.
– For stack machines, the terms "0-operand" and "zero-
address" apply to arithmetic instructions, but not to all
instructions, as 1-operand push and pop instructions are
used to access memory.
• 1-operand (one-address machines), so called
accumulator machines, include early computers and many
small microcontrollers: most instructions specify a single right
operand (that is, constant, a register, or a memory location),
with the implicit accumulator as the left operand (and the
destination if there is one): load a, add b, store c.
• C = A+B needs three instructions.
• 2-operand — many CISC and RISC machines fall under this
category:
• CISC — move A to C; then add B to C.
– C = A+B needs two instructions. This effectively 'stores'
the result without an explicit store instruction.
• CISC — Often machines are limited to one memory operand
per instruction: load a,reg1; add b,reg1; store reg1,c; This
requires a load/store pair for any memory movement
regardless of whether the add result is an augmentation
stored to a different place, as in C = A+B, or the same memory
location: A = A+B.
– C = A+B needs three instructions.
• RISC — Requiring explicit memory loads, the
instructions would be: load a,reg1; load b,reg2; add
reg1,reg2; store reg2,c.
– C = A+B needs four instructions.
• 3-operand, allowing better reuse of data:
• CISC — It becomes either a single instruction: add
a,b,c
– C = A+B needs one instruction.
• more operands—some CISC machines permit a
variety of addressing modes that allow more than 3
operands (registers or memory accesses), such as the
VAX "POLY" polynomial evaluation instruction.
• A computer is a machine that can be
instructed to carry out sequences of
arithmetic or logical operations
automatically v
• MOSFET (MOS transistor), showing gate (G),
body (B), source (S) and drain (D) terminals.
• The gate is separated from the body by an insulating
layer (pink).ia computer programming.
• The metal–oxide–silicon field-effect transistor (MOSFET),
also known as the MOS transistor
• A central processing unit (CPU), also called a central
processor or main processor, is the electronic circuitry
within a computer that executes instructions that make
up a computer program.
• Block diagram of a basic
uniprocessor-CPU
computer.
• Black lines indicate data
flow, whereas red lines
indicate control flow;
arrows indicate flow
directions.
• The arithmetic logic unit (ALU) is a
digital circuit within the processor
that performs integer arithmetic
and bitwise logic operations.
• The inputs to the ALU are the data
words to be operated on (called
operands), status information from
previous operations, and a code
from the control unit indicating
which operation to perform.
• An arithmetic logic unit (ALU) is a combinational
digital electronic circuit that performs arithmetic and
bitwise operations on integer binary numbers.
• Bit shift operations
• ALU shift operations cause operand A (or B) to shift left or right
(depending on the opcode) and the shifted operand appears at Y.
• Simple ALUs typically can shift the operand by only one bit
position, whereas more complex ALUs employ barrel shifters that
allow them to shift the operand by an arbitrary number of bits in
one operation.
• In all single-bit shift operations, the bit shifted out of the
operand appears on carry-out; the value of the bit shifted
into the operand depends on the type of shift.
• Arithmetic shift: the operand is treated as a
two's complement integer, meaning that the most
significant bit is a "sign" bit and is preserved.
• Logical shift: a logic zero is shifted into the operand. This
is used to shift unsigned integers.
• Rotate: the operand is
treated as a circular buffer
of bits so its least and
most significant bits are
effectively adjacent.
• Rotate through carry: the
carry bit and operand are
collectively treated as a
circular buffer of bits.
• Integer range
• Every CPU represents numerical values in a specific way. For
example, some early digital computers represented numbers as
familiar decimal (base 10) numeral system values
• A six-bit word containing the binary encoded representation of
decimal value 40. Most modern CPUs employ word sizes that
are a power of two, for example 8, 16, 32 or 64 bits.
• Parallelism
• The description of the basic operation of a CPU offered in the
previous section describes the simplest form that a CPU can
take. This type of CPU, usually referred to as subscalar, operates
on and executes one instruction on one or two pieces of data at
a time, that is less than one instruction per clock cycle (IPC < 1).
• Model of a subscalar CPU, in which it takes fifteen clock cycles
to complete three instructions
• Instruction-level parallelism
• One of the simplest methods used to accomplish increased
parallelism is to begin the first steps of instruction fetching and
decoding before the prior instruction finishes executing.
• This is the simplest form of a technique known as
instruction pipelining, and is used in almost all modern general-
purpose CPUs.
• Pipelining allows more than one instruction to be executed at
any given time by breaking down the execution pathway into
discrete stages.
• This separation can be compared to an assembly line, in which
an instruction is made more complete at each stage until it
exits the execution pipeline and is retired.
• Basic five-stage pipeline. In the best case scenario, this pipeline
can sustain a completion rate of one instruction per clock cycle.
• A compiler is a computer program that
translates computer code written in one
programming language (the source language)
into another language (the target language).
• (e.g., assembly language, object code, or
machine code) to create an executable
program.
• A diagram of the
operation of a typical
multi-language, multi-
target compiler
• Three-stage compiler structure
Compiler design
• The stages include a front end, a middle end, and a back
end.
• The front end verifies syntax and semantics according to a
specific source language.
• The middle end performs optimizations on the IR that are
independent of the CPU architecture being targeted.
• The back end takes the optimized IR from the middle end.
• Machine code is a strictly numerical language
which is intended to run as fast as possible,
and may be regarded as the lowest-level
representation of a compiled or assembled
computer program or as a primitive and
hardware-dependent programming language.
• The overwhelming majority of practical programs
today are written in higher-level languages or
assembly language.
• The source code is then translated to executable
machine code by utilities such as compilers,
assemblers, and linkers, with the important exception
of interpreted programs, which are not translated into
machine code.
• In computing, a linker or link editor is a computer
System program that takes one or more object files
generated by a compiler or an assembler and
combines them into a single executable file, library file,
or another 'object' file.
• An illustration of the linking process.
• Object files and static libraries are assembled into a
new library or executable
• An interpreter is a computer program that directly executes
instructions written in a programming or scripting language,
without requiring them previously to have been compiled
into a machine language program.
• Machine code is by definition the lowest level of
programming detail visible to the programmer, but internally
many processors use microcode or optimise and transform
machine code instructions into sequences of micro-ops.
• This is not generally considered to be a machine code.
• Microcode is a computer hardware technique that
interposes a layer of organisation between the CPU
hardware and the programmer-visible instruction set
architecture of the computer.
• Micro-operations (also known as a micro-ops or μops)
are detailed low-level instructions used in some designs
to implement complex machine instructions (sometimes
termed macro-instructions in this context)
• A high-level
illustration showing
the decomposition of
machine instructions
into micro-
operations,
performed during
typical
fetch-decode-execute
cycles
• A memory address is a
reference to a specific
memory location used at
various levels by software
and hardware.
• In a computer using
virtual memory,
accessing the location
corresponding to a
memory address may
involve many levels.
• A system call is the programmatic way in which
a computer program requests a service from the
kernel of the operating system it is executed on.
• A high-level overview of the Linux kernel's
system call interface, which handles
communication between its various components
and the userspace
Modularization and Program Assembly
• Modular programming is a software design technique
that emphasizes separating the functionality of a
program into independent, interchangeable modules,
such that each contains everything necessary to execute
only one aspect of the desired functionality.
• A module interface expresses the elements that are
provided and required by the module. The elements
defined in the interface are detectable by other modules.
• A computer
program is a collection
of instructions that
can be executed by a
computer to perform a
specific task.
• A computer program
written in the
imperative
programming style
• The implementation contains the working code that
corresponds to the elements declared in the interface.
• While the historical usage of these terms has been
inconsistent, "modular programming" now refers to high-
level decomposition of the code of an entire program into
pieces: structured programming to the low-level code use of
structured control flow, and object-oriented programming to
the data use of objects, a kind of data structure.
• Control flow (or flow of control) is
the order in which individual
statements, instructions or
function calls of an imperative
program are executed or evaluated.
• A flow chart showing control flow.
• If-then-(else) statements