Sunteți pe pagina 1din 6

.

COMP112/14 - Programming I

01 Introduction
Instructor : Ke Wei

A319

& Ext. 6452

) 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

August 28(29), 2014


Ke Wei 4LCDI/ESAP/MPI

COMP112/14-01 Introduction

2014-08-28(29)

1 / 18

Textbooks and References

Textbooks and References


Y. Daniel Liang.
Introduction to Java Programming, Brief Version, 9th Edition.
Prentice Hall, 2012.
ISBN 978-0132923736
Java8
, 2012.
ISBN 978-7111361220
Textbook.
Cay S. Horstmann and Gary Cornell.
Core Java Volume I Fundamentals, 9th Edition.
Prentice Hall, 2012.
ISBN 978-0137081899
Reference book.
Ke Wei 4LCDI/ESAP/MPI

COMP112/14-01 Introduction

2014-08-28(29)

2 / 18

Textbooks and References

Outline
1

Textbooks and References

Course Overview

Computer Architecture

Instructions and Programs

Values and Variables

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

A Diagram of Computer Architecture


CPU

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

Functional Units (2)

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

Instructions and Programs

Instructions and Programs


Activities in a computer are governed by instructions.
A group of well organized instructions forms a program.
Typical computer instructions perform very simple operations in very high speed.
These simple instructions include:
transfer data between memory and registers,
r0 [100]

[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 ,

usually, theres only one operation in an instruction for simplicity,


control the execution flow of the instructions.
Ke Wei 4LCDI/ESAP/MPI

COMP112/14-01 Introduction

2014-08-28(29)

8 / 18

Instructions and Programs

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

Instructions and Programs

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.

- Try to figure out what the following program intends to compute.


r0 [12]
r1 [16]
r0 r0 r0
r1 r1 r1
r2 r0 r1
r0 r0 r0
r1 r1 r1
r0 r0 + r1
r0 r0 + r2
[20] r0
Ke Wei 4LCDI/ESAP/MPI

Trace the contents of the registers using this table.


r0
r1
r2
[12]2
[16]

COMP112/14-01 Introduction

2014-08-28(29)

10 / 18

Values and Variables

Values and Variables


Instead of using direct addresses, such as [100], [104] and [108], we often use symbolic
notations to denote memory cells, for example a, b, c, sum and item, these symbols are
called variables.
Usually, different variables refer to different memory cells.
It is important to distinguish a memory cell from the value stored in that memory cell.
The process of getting the value stored
in a variable is called evaluation.

x 255

r0 x
x r0

Thus, a variable has two meanings:


When we say put (assign) some value to variable x, we mean by x the memory cell.
When we say add variables y and z, we mean by y and z the values stored in those
memory cells.
So, in this instruction x y + z, x refers to the memory cell, while y and z refer to the values.
Ke Wei 4LCDI/ESAP/MPI

COMP112/14-01 Introduction

2014-08-28(29)

11 / 18

Values and Variables

Computing by Updating Variables

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

ax5 + bx4 + ax3 + (a + b)x2 + (a + 2b)x + b,


and store the final result in register r3 .

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

Register and Memory Transfer Operations (for i, j = 0, 1, 2, 3 and x is a variable):


ri rj

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

Program Sequencing and Flowcharts


Besides memory transfers and arithmetic operations, program sequencing is another type
of operations in a CPU.
Program sequencing operations are related to the program itself, while others are data
processing operations.
The simplest is straight-line sequencing, which means the CPU fetches instructions one by
one and executes them in their natural order. We have seen some programs using
straight-line sequencing.
To represent more complex sequencing schemes, we use flowcharts, which connect
instructions with arrows to indicate the execution flow, often different from the natural
order of the instructions.
r0 100

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

A few conditional branching patterns are common in many structural programming


languages to alter the execution flow (more formally, the control flow).
c

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

S-ar putea să vă placă și