Computer Systems
Assembly Language Programming
University of Akron
Dr. Tim Margush
Fetch Execute Cycle
Fetch Next Instruction
Execute Instruction Update PC
Decode Instruction
11/23/2011 Dr. Tim Margush - Assembly 2
Language Programming
EDVAC & EDSAC
• First examples of the stored-program computer,
also called von Neumann Architecture
• Electronic Delay Storage Automatic Calculator
› Wilkes, University of Cambridge Mathematical
Laboratory, 1949
› Concept inspired by the 1945 report by John von
Neumann on the EDVAC
• Electronic Discrete Variable Automatic Computer
› Mauchley & Eckert, University of Pennsylvania, Moore
School of Electrical Engineering, 1952
11/23/2011 Dr. Tim Margush - Assembly 3
Language Programming
Computer System Components
Central Processing Unit
Control Unit Main Memory
Arithmetic Busses
Logical Unit
Secondary
Storage
Input & Output
Devices
11/23/2011 Dr. Tim Margush - Assembly 4
Language Programming
Control Unit
• Fetch, decode, and execute instructions
• Control all aspects of the computer system
• Contains a limited number registers
› Register = fast access storage location located
in the control unit that serves as an operand or
result location for many operations
• Keeps track of the address of the next
instruction to be fetched
11/23/2011 Dr. Tim Margush - Assembly 5
Language Programming
Arithmetic and Logical Unit
• Combines signals from one or more sources
such as registers to produce an arithmetic or
logical result that is stored, typically in a
register
› Performs arithmetic operations such as add,
subtract, multiply, etc
› Performs logical operations such as and, or,
not, shift, etc
11/23/2011 Dr. Tim Margush - Assembly 6
Language Programming
Memory
Addresses
are usually
• A computer system shown in
0000 3F hexadecimal
component that allows
the storage and 0001 2C Data values
are usually
retrieval of data 0002 41 bytes
• Main memory is 0003 FF
Addresses
usually called RAM 0004 00 typically start at
0, and continue
• Memory is usually 0005 1E to some upper
organized as a table of limit (often a
power of 2)
bytes or words
11/23/2011 Dr. Tim Margush - Assembly 7
Language Programming
RAM and ROM
• Random Access Memory
› Access time does not vary by address
› Typically volatile
• Read Only Memory
› Also a random access memory
› Retains data even when power is removed
11/23/2011 Dr. Tim Margush - Assembly 8
Language Programming
Secondary Storage
• External devices used for long-term storage
of data
› Disk drives, Flash storage, Optical media, etc.
› May be random or sequential access
› Typically much slower access than Main
Memory
11/23/2011 Dr. Tim Margush - Assembly 9
Language Programming
Input Output
• Devices vary widely by need
• Input devices
› Mouse, microphone, touch-pad, sensors
• Output devices
› Video display, speaker, servos, etc
• At the processor level, we may treat I/O as
primitively as the ability to sense or assert a logic
signal on an external connection to the processor
chip
11/23/2011 Dr. Tim Margush - Assembly 10
Language Programming
CISC vs RISC
• Complex Instruction • Reduced Instruction
Set Computer Set Computer
› Typically has a large › Typically has a smaller
instruction set instruction set
› Instructions range from › All instructions are
very simple to very simple and efficient to
complex execute
› Time to execute › Most instructions
instructions may vary execute in a single
widely cycle
11/23/2011 Dr. Tim Margush - Assembly 11
Language Programming
Microprocessors
• Integrated ALU and CU on a single chip
› Became possible in the 1970's due to the
development of large-scale integrated circuits
(LSI)
› Replaced systems that were spread across
multiple components
11/23/2011 Dr. Tim Margush - Assembly 12
Language Programming
Microcontrollers
• Integrates CPU, memory, secondary storage
(ROM), and I/O capabilities on a single
chip
› Sometimes called a "computer on a chip"
› Typically used in embedded applications
11/23/2011 Dr. Tim Margush - Assembly 13
Language Programming
Harvard vs. von Neumann
• von Neumann • Harvard
› Program instructions › Program instructions
and data reside in the are stored in a separate
same physical memory memory from data
› Instruction fetching › Instructions and data
uses the same can be accessed in
pathways as data parallel
access › Named after Mark I
› Also called Princeton relay computers
Architecture developed at Harvard
11/23/2011 Dr. Tim Margush - Assembly 14
Language Programming
Numeration Systems
• Binary - Base 2 • Raw Binary format
› 0, 1 › All information is
• Octal - Base 8 encoded and internally
is in the form of binary
› 0, 1, 2, … 7 signals
• Decimal - Base 10 › Externally, we may
› 0, 1, 2, …, 9 choose to express the
information in any
• Hexadecimal (Hex) numeration system, or
› 0, 1, …, 9, A, B, …, F in a decoded form
using other symbols
11/23/2011 Dr. Tim Margush - Assembly 15
Language Programming
Storage Containers
• Standard fixed-size containers
› Bit: 0 or 1
› Nybble (4 bits): 0000 through 1111
› Byte (8 bits): 0x00 through 0xFF
› Word (16 bits): 0x0000 through 0xFFFF
› Doubleword (32 bits): 8 hex digits
› Quadword (64 bits): 16 hex digits
• Sometimes information is encoded in non-
standard sizes
11/23/2011 Dr. Tim Margush - Assembly 16
Language Programming
Positional Notation
• Numeration scheme representing numbers as
polynomials in a shorthand positional format
› The polynomial is in powers of b, called the bse or
radix of the particular notation
› The coefficients are numbers from 0 to b-1
› There must be b distinct symbols that represent this
initial set of numbers
• The numeral 235 is shorthand for the polynomial
2*102+3*101+5*100 (assuming base ten)
11/23/2011 Dr. Tim Margush - Assembly 17
Language Programming
Specifying Notational Details
• Each assembly language establishes a
convention for writing numerals
› Normally formatted numerals, such as 28, or
983, will be assumed to be in decimal (base ten)
notation
› Binary data will be prefixed by 0b
› Octal data will be prefixed by a leading 0
› Hexadecimal data will be prefixed by 0x or $
11/23/2011 Dr. Tim Margush - Assembly 18
Language Programming
Implying Container Sizes
• Although not strictly required in assembly
language, numerals written in binary or
hexadecimal generally imply the number of bits
intended
› 0b0110 implies a 4 bit value
› $13FC implies a 16 bit value, even though only 13 bits
are required
› 0377 is ambiguous; 3 octal digits would imply 9 bits;
but this numeral typically represents a byte (the first
digit would never be greater than 3 in this usage)
11/23/2011 Dr. Tim Margush - Assembly 19
Language Programming
Visualizing Memory
• Memory is visualized as an array
(sequence) of bytes
› Left to right; low addresses to high addresses
› Top to bottom; both address orders are used
› As a printed page; each line represents
consecutive addresses, subsequent lines
continue where previous line ends
11/23/2011 Dr. Tim Margush - Assembly 20
Language Programming
Bit and Byte Order
• Visualizing a byte as 8-bits
› Express the bits left to right by decreasing place
value
• Number them 7, 6, …, 0
• Electronically, there is no position convention
• Visualize a word as 16 bits or 2 bytes
› Write the bits/bytes left to right decreasing
place value
11/23/2011 Dr. Tim Margush - Assembly 21
Language Programming
Endian?
• When multibyte data is stored in memory,
there is a variation as to how the bytes are
arranged
› Multibyte data is stored as an array of bytes
• Big-endian order means the most significant
byte is stored at the (first) lowest address.
• Little-endian order means the least
significant byte is stored first.
11/23/2011 Dr. Tim Margush - Assembly 22
Language Programming
Conversions
• Converting from base b to base ten requires
evaluating the positional polynomial
representation of the base b numeral at x = b
• 05712 = 5*83 + 7*82 + 1*81 + 2*80
= 3018
• 0b1101 = 1*23 + 1*22 + 0*21 + 1*20
= 13
11/23/2011 Dr. Tim Margush - Assembly 23
Language Programming
Binary-Octal-Hexadecimal
• There are shortcut conversions between these
bases
› Octal digits represent 3 bits
• 037621 = 011 111 110 010 001
› Hexadecimal digits represent 4 bits
• $1F3C = 0001 1111 0011 1100
› Converting from binary: group by 3's or 4's starting
from the right and add sufficient leading 0's
• 0b1001110011 = 001 001 110 011 = 01163
= 0010 0111 0011 = $273
11/23/2011 Dr. Tim Margush - Assembly 24
Language Programming
Converting from Base Ten
• Usually accomplished using a repetitive
division algorithm working in base ten
//determine base b numeral r
//equivalent to t convert 244 to octal
i=0; Successive
244 / 8 = 30 r 4
values of t
do{ 30 / 8 = 3 r 6
r[i++] = t % b; 3/8=0r3
t = t / b; 0
}while (t != 0) r = 0364
11/23/2011 Dr. Tim Margush - Assembly 25
Language Programming
Horner's Algorithm
William George Horner 1786-1837
• An efficient algorithm for evaluating
polynomials
› Based on a clever factorization
› Applied to the task of converting a base b1
numeral to base b2 by evaluating the positional
notation polynomial
• The trick is to do all of the work in base b2
• This is what we did earlier to convert to base ten –
now we apply it to convert to other bases
11/23/2011 Dr. Tim Margush - Assembly 26
Language Programming
Horner's Algorithm
• Evaluate a positional polynomial with
coefficients r[] and base b1
//Given r[] in base b1 evaluate
//the positional polynomial
n = 0; Convert 0231 to base ten
for (i=r.length-1; i>=0; i--){ (working in base ten)
n = n * b1 + r[i]; n=0
} n = 0*8+2 = 2
n = 2*8+3 = 19
//n is the result n = 19*8+1 = 153
11/23/2011 Dr. Tim Margush - Assembly 27
Language Programming
Horner's Rule
• Processors do Convert 0231 to base two
computations in (working in base two)
n=0
binary, so when they n = 0*1000+10 = 10
execute this algorithm, n = 10*1000+11 = 10011
all work is in base two n = 10011*1000+1 = 10011001
• Note that each digit and notice 10 011 001 is 0231
must be encoded in
This is the basis of numeric input
binary as well as the routines that convert a string of
base (8=1000) digits to the native integer format
11/23/2011 Dr. Tim Margush - Assembly 28
Language Programming
Boolean Logic
• An algebraic system for Boolean data
› Set: {false, true)
› Operations: and, or , not
• The operations obey certain axioms such as
associativity, commutativity, identity
• Named after George Boole (1816-1864)
• Implemented via digital logic circuits
fundamental to the function of a computer
11/23/2011 Dr. Tim Margush - Assembly 29
Language Programming
Boolean Data
• Digital circuits represent two distinct
voltage levels
• We visualize (interpret) these as
› 0 or 1 (binary)
› false or true (Boolean or logical)
11/23/2011 Dr. Tim Margush - Assembly 30
Language Programming
Boolean Operations
• Act on Boolean values
› NOT a a NOT a
› a AND b
0 1
› a OR b
1 0
› a XOR b
11/23/2011 Dr. Tim Margush - Assembly 31
Language Programming
Boolean Operations
• Act on Boolean values
› NOT a a b a AND b
› a AND b
0 0 0
› a OR b
0 1 0
› a XOR b
1 0 0
1 1 1
11/23/2011 Dr. Tim Margush - Assembly 32
Language Programming
Boolean Operations
• Act on Boolean values
› NOT a a b a OR b
› a AND b
0 0 0
› a OR b
0 1 1
› a XOR b
1 0 1
1 1 1
11/23/2011 Dr. Tim Margush - Assembly 33
Language Programming
Boolean Operations
• Act on Boolean values
› NOT a a b a XOR b
› a AND b
0 0 0
› a OR b
0 1 1
› a XOR b
1 0 1
1 1 0
11/23/2011 Dr. Tim Margush - Assembly 34
Language Programming
ALU and Boolean Operations
• An n-bit ALU performs Boolean operations
on all n bits simultaneously
• Each column is an independent calculation
› 0b01100101
› AND 0b11000100
› 0b01000100
11/23/2011 Dr. Tim Margush - Assembly 35
Language Programming
Applying Boolean Operations
• Force a bit or bits to zero
› Sometimes called "masking out bits"
• Use a "mask" byte with
› 0's in positions to be cleared (forced to 0)
› 1's in positions to be unchanged
• Perform an AND with the mask
› x AND 0 = 0 (domination)
› x AND 1 = x (identity)
11/23/2011 Dr. Tim Margush - Assembly 36
Language Programming
Applying Boolean Operations
• Force a bit or bits to one
› Sometimes called "setting bits"
• Use a "mask" byte with
› 1's in positions to be set (forced to 1)
› 0's in positions to be unchanged
• Perform an OR with the mask
› x OR 0 = x (identity)
› x OR 1 = 1 (domination)
11/23/2011 Dr. Tim Margush - Assembly 37
Language Programming
Applying Boolean Operations
• Toggle a bit or bits
› Sometimes called "flipping or complementing bits"
• Use a "mask" byte with
› 1's in positions to be toggled (complemented)
› 0's in positions to be unchanged
• Perform an XOR with the mask
› x XOR 0 = x (identity)
› x XOR 1 = NOT x
11/23/2011 Dr. Tim Margush - Assembly 38
Language Programming
Combining Bits in Separate Bytes
• Bit patterns from two bytes are to be combined
› The important bits of each byte must be aligned with 0
(and unimportant) bits in the other byte
• The OR of the two bytes, combines them into one
› The bold bits are the important ones:
› 0b00110000
› OR 0b00001010
› 0b00111010
11/23/2011 Dr. Tim Margush - Assembly 39
Language Programming
Shifting
• Move bits in a byte to the left or right
› 0b11011100 shifted left is 0b10111000
› 0b11011100 shifted right is 0b01101110
• In these examples, a zero (0) was brought in
to fill the vacated position
› Variations on the basic shift fill this position
differently
11/23/2011 Dr. Tim Margush - Assembly 40
Language Programming
Rotating
• Move bits in a byte to the left or right in a
circular pattern
› 0b11011100 rotated left is 0b10111001
› 0b11011100 rotated right is 0b01101110
• In these examples, the bit shifted out is used
to fill the vacated position
› There are some variations of this behavior as
well
11/23/2011 Dr. Tim Margush - Assembly 41
Language Programming
Application of Shifts
• When a byte (or word) is interpreted
numerically (as a binary representation of a
number)…
› Left shift is equivalent to multiplication by 2
› Right shift is equivalent to division by 2
› $5C shifted left becomes $B8
• 92 * 2 = 184
› $5C shifted right is $2E
• 92 / 2 = 46 (remainder if any is thrown away)
11/23/2011 Dr. Tim Margush - Assembly 42
Language Programming
Multiplication Tricks
• Every multiplication can be accomplished
by shifting and adding
› Just regroup using only multiplications by
powers of 2 and additions
• 10 * n = (8 + 2) * n
• =8*n+2*n
› or
• 10 * n = (2 * (4 + 1)) n
• = 2 * (4 * n + n)
11/23/2011 Dr. Tim Margush - Assembly 43
Language Programming