Document

Report
Assembly & Machine Languages
CSCI130
Instructor: Dr. Lynn Ziegler
Layered Architecture
LAYER
Order
High-order P.L.: Visual Basic
1
System SW: O.S.
3
Data Representation
5
Higher-level Programming
Languages

(3GL) High-level languages are in the middle

Use English-like phrases and mathematical notation



x = x+1;
A limited vocabulary with precise meaning
Make us think at a higher-level of abstraction

No attention to technical and hardware details


Like O.S.
Much larger instruction set for programmers

Multiplication
Higher-level Programming
Languages (PLs)

(1GL) Machine language programming


limited set of instructions
binary numbers:



Memory addresses
(2GL) Assembly language





opcodes, absolute memory addresses and data
English mnemonics for binary opcodes
Decimal data
Labels for memory addresses and variables
limited instruction set
Use human languages to program

Large vocabulary (space and time to search) … opcode lookup



500,000 words - 1,000,000 words (including scientific words)
Synonyms
Context
CPU
- Connects to main memory (MM) thru a bus
- Bus = bundle of wires
- Transfers data between CPU and memory
- Width of bus determines speed
- e.g. 32-bit bus (Higher  faster)
- Where is data stored when copied to CPU?
CPU


Divided into CU and ALU
CU (Control Unit)





Orchestra organizer
Oversees transfer of data between MM and CPU
Accesses info in ROM (e.g. during reboot)
Has a bunch of memory registers
ALU (Arithmetic Logic Unit)

Contains circuits to do computations


Built for each basic/hardwired operation (+,-, x, /, =,>,<, NOT)
Includes a special register accumulator AC to hold results
The History of
Computing

ENIAC (February 14, 1946)

weighed over 30 tons, and consumed 200 kilowatts
of electrical power
•
•
An ALU in 1957
This was an arithmetic unit you sit back and admire. It was
part of Honeywell's Datamatic 1000 computer. (Image
courtesy of Honeywell, Inc.)
Modern CPUs
Programs & Algorithms

Characteristics of an algorithm:


List of steps to complete a task
Each step is PRECISELY defined and is suitable for the
machine used




The process terminates in a finite amount of time


Increase the value of X
Jump!
Add 5 to variable X
No infinite loops
Written in an English-like language (Pseudocode)
Programs & Algorithms

Program: A formal representation of a method
for performing some task



Written in a programming language understood by
a computer
Detailed and very well-organized (computers just do
what they are told)
Follows an algorithm … method for fulfilling the task

Plan to do something VS the actual performance
Combining Limited
Instructions

Computing an answer to an equation:



5*3 + 62 – 7
Assume our computer can’t directly multiply, subtract, or
raise to power
Multiplication task:







1:
2:
3:
4:
5:
6:
7:
Get 1st number, Number_1
Get 2nd number, Number_2
Answer = 0
Add Number_1 to Answer
Subtract 1 from Number_2
if Number_2>0, go to step 4
Stop
Assembly Language

Machine code consists of the



binary instructions, data & addresses
can directly be executed by the CPU
We as humans are not good in working with
sequences of 1’s and 0’s


We prefer to use an English-like style (English mnemonics
and decimal numbers)
This is called the assembly language



It has 1-to-1 correspondence to machine code
one instruction for every 3-bit Opcode and decimal numbers for
addresses and data
Slight other changes
Assembly Language

Additional problems addressed:

Most commands like LOAD, STOR, JUMP and ADD
require memory addresses


How do we know which address in memory is free to
use?
We also assumed that PC will contain 0 initially


Our program will be loaded to first memory location…is
this always true?
What if we have multiple executing programs on the
same machine?

Which will get the 0 address then?
Assembly Language


In reality, when we program, we don’t concern
ourselves with such low level details This is
handled by the operating system.
From now on, we use variables/labels instead
of addresses and totally forget about memory

110 10010  ADD Counter which must
initialized
0
clock
16-3000 MHz (~3.0 GHz)
Assembly Language of a Computer
Instruction
HALT
Description
Stop program execution
JUMP loc
Go to location loc to continue program execution (unconditional)
JZER loc
Go to location loc to continue program execution but only if AC=0
(i.e. if zflag=1) (conditional)
JPOS loc
Go to location loc to continue program execution but only if AC>0
(i.e. if pflag=1) (conditional)
LOAD loc
Copy contents of location loc into AC (zflag, pflag might be
affected)
STOR loc
Copy contents of AC into location loc
ADD loc
Add contents of location loc to AC (zflag, pflag might be affected)
SUB loc
Subtract contents of location loc from AC (zflag, pflag might be
affected)
READ
WRITE
Put user’s input inside AC (zflag, pflag might be affected)
Output contents of AC to user – e.g. on screen
Simple VB-like program 1

Private Sub cmdSimple ()




Dim X As Integer
X = inputbox(…)
Msgbox x
End Sub






READ
STOR X
LOAD X
WRITE
HALT
X: 0
Simple VB-like program 2

Private Sub cmdSimple ()



Dim X As Integer
X = inputbox(…)
If X>0 then



Msgbox x
End If
End Sub
Assembly Version for program 2








READ
STOR X
LOAD X
JPOS Disp
HALT
Disp: WRITE
HALT
X: 0
Simple VB-like program 3

Private Sub cmdSimple ()





Dim X As Integer
Dim Y As Integer
Y = 0
X = inputbox(…)
If X>0 then

Msgbox x

Else

Msgbox y
End If


End Sub
Assembly Version for program 3







READ
STOR X
LOAD X
JPOS Disp
LOAD Y
WRITE
HALT




Disp: WRITE
HALT
X: 0
Y: 0
VB-like program 4

Private Sub cmdSimple ()







Dim X As Integer
Dim Y As Integer
Dim Z As Integer
X = inputbox(…)
Y = inputbox(…)
Z = 0
If X>Y then


Msgbox X
ElseIf X=Y then

Msgbox Z

Else

Msgbox Y
End If


End Sub
Assembly Version for program 4











READ
STOR X
READ
STOR Y
LOAD X
SUB Y
JPOS MaxX
JZER Equal
LOAD Y
WRITE
HALT









MaxX: LOAD X
WRITE
HALT
Equal: LOAD Z
WRITE
HALT
X: 0
Y: 0
Z: 0
VB-like program 5

Private Sub cmdSimple ()




Dim Sum As Integer
Dim N As Integer
N = inputbox(…)
Do



Sum = Sum + N
N = N -1

Loop While N>0

Msgbox Sum
End Sub
Assembly Version for program 5









READ
STOR N
Loop: LOAD Sum
ADD N
STOR Sum
LOAD N
SUB One
STOR N
JPos Loop






LOAD Sum
WRITE
HALT
Sum:0
N: 0
One:1
VB-like program 6

Private Sub cmdSimple ()






Dim Sum As Integer
Dim N1 As Integer
Dim N2 As Integer
N1 = inputbox(…)
N2 = inputbox(…)
Do



Sum = Sum + N2
N2 = N2 -1

Loop While N2>=N1

Msgbox Sum
End Sub
Assembly Version for program 6










READ
STOR N1
READ
STOR N2
Loop:
LOAD
ADD
STOR
LOAD
SUB
STOR
Sum
N2
Sum
N2
One
N2
SUB
JPOS
JZER
LOAD
WRITE
HALT
Sum:0
N1: 0
N2: 0
One:1
N1
Loop
Loop
Sum
VB-like program 6

Program to print a 1 if an input number is even (multiple of 2) and 0 otherwise

Try it out in SimHymn: http://www.csbsju.edu/Computer-Science/Useful-Links/Launch-CSApplications.htm
VB-like:
VB-like:
Dim Sum As Integer
If N = 0 Then
Dim N As Integer
MsgBox 1
N = InputBox(…)
Else
Do
MsgBox 0
N=N-2
End If
Loop While N > 0




READ
STOR
LOOP:
Even:

N
LOAD N
SUB Dec
STOR N
JPOS Loop
JZER Even
LOAD Zero
WRITE
HALT
LOAD One
WRITE
HALT



Dec: 2
N: 0
One: 1
Zero:
0
A Simple 8-bit Computer


Use a simplified version of real computers to
understand machine language programs
32 8-bit main memory registers




5 bits (25 registers) to represent an address
00000 to 11111
PC holds memory addresses  5-bit PC
For simplicity, assume only positive and negative
integers (2’s complement notation)

8-bit AC
0
clock
16-3000 MHz (~3.0 GHz)
A Simple 8-bit
Computer

Two 1-bit flags



ALU supports the following basic operations


Addition, subtraction, if AC=0, if AC>0
8 instructions (next slide)






zflag: 1 if AC zero, 0 otherwise
pflag: 1 is AC is positive, 0 otherwise
3 bits (8 instructions)
000 to 111
8 bits per memory location  rest 5 bits of the instruction contain the
address of the input data to the instruction
III AAAAA
8-bit IR
Memory holds 8-bit data or instructions
The Machine Language

Instruction set depending on CPU





Hardwired
Binary code for each instruction (Opcode)
Different CPUs might have different operations for which
circuits exit in the ALU
Programs can only use those opcodes
The set of all opcodes (i.e. instructions) together is
known as the machine language
Assembly Language

Computers can only understand machine code


Programs written in assembly must be translated to
machine code to be executable by CPU
The assembler is responsible for that

Stored in memory and executed before any assembly
program can be executed



(english-like) assembly source code  (binary) machine
code
 Does a lookup for each word (Opcodes)
Other things (find empty memory locations for my
variables)
The result is called object code
Machine Language of a Computer
Instruction
Opcode Address
Description
HALT: 000xxxxx 000 (i.e. 0) NOT USED Stop program execution (Halt does
not require an address)
JUMP: 001xxxxx 001 (i.e. 1)
xxxxx
Place address in PC (unconditional)
JZER: 010xxxxx 010 (i.e. 2)
xxxxx
Place address in PC only of AC=0
(i.e. if zflag=1) (conditional)
JPOS: 011xxxxx 011 (i.e. 3)
xxxxx
Place address in PC only of AC>0
(i.e. if pflag=1) (conditional)
LOAD: 100xxxxx 100 (i.e. 4)
xxxxx
Copy contents of address into AC
(zflag, pflag might be affected)
STOR: 101xxxxx 101 (i.e. 5)
xxxxx
Copy contents of AC into address
A Simple 8-bit Computer

Input/Output (from user perspective)

LOAD 30: 100 11110  register 30 in memory is
designated as an input cell


A LOAD with address 30 causes machine to access an input device
The user is asked to provide an input (input box)



Program waits/pauses meanwhile
The input is saved in address 30 and loaded to AC
STOR 31: 100 11111  register 31 in memory is
designated as an output cell


The contents of AC is saved in address 31
Machine also copies data to output device

User sees the output on his/her screen (message box)
VB-like program 6

Private Sub cmdSimple ()






Dim Sum As Integer
Dim N1 As Integer
Dim N2 As Integer
N1 = inputbox(…)
N2 = inputbox(…)
Do



Sum = Sum + N2
N2 = N2 -1

Loop While N2>=N1

Msgbox Sum
End Sub
Assembly Version for program 6










READ
STOR N1
READ
STOR N2
Loop:
LOAD
ADD
STOR
LOAD
SUB
STOR
Sum
N2
Sum
N2
One
N2
SUB
JPOS
JZER
LOAD
WRITE
HALT
Sum:0
N1: 0
N2: 0
One:1
N1
Loop
Loop
Sum
Machine-like Version for
program 6










0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
LOAD
STOR
LOAD
STOR
LOAD
ADD
STOR
LOAD
SUB
STOR
30
17
30
18
16
18
16
16
19
16
(10011110)
(10101001)
(10011110)
(10101010)
(10001000)
(11001010)
(10101000)
(10001000)
(11101011)
(10101000)
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
SUB 17
JPOS 4
JZER 4
LOAD 16
(11101001)
STOR 31
(10111111)
HALT
0
0
0
1
(00000000)
(01100100)
(01000100)
(10001000)
(00000000)
(00000000)
(00000000)
(00000001)
Von Neumann Machine

Will see how hardwired operations are
accomplished later one


Comparison and addition
Build up other operations/tasks using those
basic/hardwired operations ones

Programmed operations
Fetch-Execute Cycle

Programs are made up of instructions


Fetch-Decode-Execute
CU supervises the fetch-execute cycle




Fetches one instruction from memory
Decodes instruction
Increments PC
Executes instruction
Fetch-Execute Cycle

To supervise the fetch-execute cycle, the CU has two
registers

PC (program counter)




IR (instruction register)


Contains the address of the next instruction in memory to fetch
Initially points to first instruction of a program
After fetching an instruction, it is incremented by one unless there is
a jump to some other instruction
Holds the current instruction
Also has a timing clock


Clock chip that generates a steady stream of electric pulses
At every pulse, the CPU proceeds by one step


Fetch, send data to ALU, etc …
Higher frequency clocks result in faster computers

16-3000 MHz (~3.0 GHz)
Cycle
PC
IR
AC
0
0
-
-
1–F
0
10011110
(LOAD 30)
-
10011110
(LOAD 30)
-
10011110
(LOAD 30)
-
10011110
(LOAD 30)
5 (user input
for N1)
10101000
(STOR 17)
5
1–D
1–I
1–E
2–F
2–D
0
1
1
1
1
10101000
(STOR 17)
5
2–I
2
10101000
(STOR 17)
5
2–E
2
10101000
5
Cycle
PC
IR
AC
5–F
5
11001010
(ADD 18)
0 (sum)
5–D
5
11001010
(ADD 18)
0
5–I
6
11001010
(ADD 18)
0
5–E
6
11001010
(ADD 18)
7 (0+ N2 which
is 7)
…
…
…
…
…
…
...
…
11 – F
11
01100100
(JPOS 4)
1 (N2-N2)
11 – D
11
01100100
(JPOS 4)
1
11 – I
12
01100100
(JPOS 4)
1
11 – E
4
01100100
1

similar documents