Documente Academic
Documente Profesional
Documente Cultură
COMP112/14 - Programming I
01 Introduction
Instructor : Ke Wei
A319
) wke@ipm.edu.mo
http://a319-101.ipm.edu.mo/~wke/COMP112/14/
Bachelor of Science in Computing, School of Public Administration, Macao Polytechnic Institute
COMP112/14-01 Introduction
2014-08-28(29)
1 / 18
COMP112/14-01 Introduction
2014-08-28(29)
2 / 18
Outline
1
Course Overview
Computer Architecture
Instruction Summary
Program Sequencing
Reading Homework
Ke Wei 4LCDI/ESAP/MPI
COMP112/14-01 Introduction
2014-08-28(29)
3 / 18
Course Overview
Course Overview
This course provides an introduction to computer programming and the Java language
fundamentals, including the concept and history of programming, compiling and running
programs, writing sequential programs, using variables and expressions, writing loops and
methods, and using arrays.
The course is divided into the following sections:
architecture of computers and introduction to programming languages,
compiling and running Java programs (with and without an IDE),
language syntax and program structure,
variables and data types,
expressions and statements,
simple graphics,
control flow, conditionals and loops,
grouping variables in arrays and using arrays with loops,
grouping variables in classes and using instances of classes, and
writing functions and procedures (static methods), passing parameters and returning results.
Ke Wei 4LCDI/ESAP/MPI
COMP112/14-01 Introduction
2014-08-28(29)
4 / 18
Computer Architecture
Arithmetic
Logic
Unit
IO Requests
Memory
Registers
Data
Instructions
Control
Unit
Data
Interrupts
DMA
(Direct Memory Access)
Input and
Output Devices
Ke Wei 4LCDI/ESAP/MPI
COMP112/14-01 Introduction
2014-08-28(29)
5 / 18
Computer Architecture
Functional Units
From a programming point of view, a computer can be divided into a couple of functional units
abstractly.
Arithmetic and logic unit (ALU)
Executes arithmetic and logical operations, such as addition and logical conjunction.
Contains a number of very high speed storage elements, called registers, for holding
operands and results.
Memory
Stores programs and data for later reference.
Consists of semiconductor units, each of which stores one bit, either 0 or 1.
These bits are processed together in groups of fixed size, called words. In modern
computers, the word length is usually 32 or 64.
The size of memory is measured in bytes, a group of eight bits, independent to the word
length of a particular computer.
Ke Wei 4LCDI/ESAP/MPI
COMP112/14-01 Introduction
2014-08-28(29)
6 / 18
Computer Architecture
Input Devices
Translates information obtained from the outside world to digital signals and provides
them to computers.
Output Devices
Translates digital signals generated by computers into desired formats: video, audio, text,
radio, ..., and provides them to the outside world.
Control Unit
Coordinates and transfers data among the other functional units.
Ke Wei 4LCDI/ESAP/MPI
COMP112/14-01 Introduction
2014-08-28(29)
7 / 18
[104] r1
r0 r2 ,
where a memory cell is denoted by its index a number indicating the location, commonly
called its address,
perform arithmetic and logical operations on the operands stored in registers and put the
results back to registers,
r0 r1 + r2
r1 r1 r0
r2 r2 & r1 ,
COMP112/14-01 Introduction
2014-08-28(29)
8 / 18
A Program Example
The following program computes a simple formula: [100] [104]2 + [108]2 , using only the
instructions given in Slide 8. Since we cannot perform arithmetic operations directly upon
memory cells, we need to transfer the numbers stored in the memory cells to registers for the
computation, and transfer the result back.
r0
r1
r2
r3
registers
Ke Wei 4LCDI/ESAP/MPI
r0 [104]
r1 [108]
r0 r0 r0
r1 r1 r1
r0 r0 + r1
[100] r0
program counter
12
5
100
104
108
memory
COMP112/14-01 Introduction
2014-08-28(29)
9 / 18
Reverse Engineering
While translating a formula to a set of instructions is rather mechanical, the reverse process
guessing the original formula from a given set of instructions is quite difficult.
COMP112/14-01 Introduction
2014-08-28(29)
10 / 18
x 255
r0 x
x r0
COMP112/14-01 Introduction
2014-08-28(29)
11 / 18
As we have seen, a major part of a program is about updating variables and registers using
instructions.
We call this imperative programming. This type of programming is about how to change
the values in variables smartly.
Suppose we have three variables a, b and x holding some values, and four registers r0 , r1 , r2
and r3 .
Write a program using the instructions in Slide 13 to compute
Ke Wei 4LCDI/ESAP/MPI
COMP112/14-01 Introduction
2014-08-28(29)
12 / 18
Instruction Summary
Instruction Summary
We summarize the instruction set with variables.
Registers: r0 , r1 , r2 , r3 .
Variables: a, b, c, . . . , x, y, z.
Arithmetic Operations (for i, j, k = 0, 1, 2, 3):
ri rj + rk
ri rj rk
ri rj rk
ri rj rk
ri x
x ri
Immediate Number Loading (for i = 0, 1, 2, 3 and ### is a number such as 255 or 123):
ri ###
The number mentioned here is not stored in a register or a memory cell, but directly
embedded in the instruction, so it is immediately available to the instruction.
Ke Wei 4LCDI/ESAP/MPI
COMP112/14-01 Introduction
2014-08-28(29)
13 / 18
Program Sequencing
r1 150
Ke Wei 4LCDI/ESAP/MPI
r0 r0 + r1
r0 r0 r1
COMP112/14-01 Introduction
r1 r0 r1
2014-08-28(29)
14 / 18
Program Sequencing
Unconditional Branching
A branch instruction breaks the normal fetching order, it lets the CPU go to a specified
location to continue fetching and executing instructions.
Branching is done by setting the program counter (PC) to the desired address. Normally, PC
automatically increments to the address of the next instruction after having fetched the
current instruction.
r0 100
r1 150
r0 r0 + r1
r0 r0 r1
r2 200
r3 60
r2 r2 r3
r1 r0 r1
If we have only unconditional branch instructions, we can only make dead-loops or skip
some instructions.
Ke Wei 4LCDI/ESAP/MPI
COMP112/14-01 Introduction
2014-08-28(29)
15 / 18
Program Sequencing
Conditional Branching
A branch instruction branches according to some condition is called conditional
branching. Conditions are usually shown in diamond boxes.
r2 =?
no
r2 200
r3 60
r2 r3
yes
r2 r2 r3
yes
B1
B2
if-then-else
yes
while-do
Ke Wei 4LCDI/ESAP/MPI
no
do-while
COMP112/14-01 Introduction
2014-08-28(29)
16 / 18
Program Sequencing
Practice
Trace the values of r0 , r1 and r2 when the execution reaches point (1).
What is the value in r0 at the END? What do you think the program does?
What do you think the instruction sequence in block (2) does?
r0 210
r2 r0
r0 r1
r1 r2
(2)
no
(1)
r1 120
r0 r1
yes
r1 6= 0
no
END
yes
r0 r0 r1
Ke Wei 4LCDI/ESAP/MPI
COMP112/14-01 Introduction
2014-08-28(29)
17 / 18
Reading Homework
Reading Homework
Textbook
For both the 8th and 9th edition
Section 1.1 1.4.
Internet
Flowchart (http://en.wikipedia.org/wiki/Flowchart).
Instruction set (http://en.wikipedia.org/wiki/Instruction_(computer_science)).
Ke Wei 4LCDI/ESAP/MPI
COMP112/14-01 Introduction
2014-08-28(29)
18 / 18