0% found this document useful (0 votes)
2 views34 pages

JavaIntro 11 Chap0

The document serves as an introduction to computer organization and programming concepts, focusing on the roles of input, computation, and output. It covers fundamental topics such as binary and hexadecimal number systems, machine language, and the architecture of a CPU. Additionally, it introduces the concept of stored programs and provides exercises to reinforce understanding of these concepts.

Uploaded by

balaji.pvb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views34 pages

JavaIntro 11 Chap0

The document serves as an introduction to computer organization and programming concepts, focusing on the roles of input, computation, and output. It covers fundamental topics such as binary and hexadecimal number systems, machine language, and the architecture of a CPU. Additionally, it introduces the concept of stored programs and provides exercises to reinforce understanding of these concepts.

Uploaded by

balaji.pvb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Java

Chapter 0

Introduction to Computers

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-1


All Rights Reserved
Introduction to Java

Introduction to Computers

Objectives

After completing this unit you will be able to:


• Describe the basic operations of input, computation
and output.
• Explain the role of the central processing unit (CPU),
memory, and secondary storage in the organization of
computers.
• Work with binary and hex numbers.
• Describe the concept of a stored program.
• Explain machine language and write simple machine
language programs.
• Describe assemblers and write simple assembly
language programs.
• Explain the concept of a high-level language and the
role of compilers.
• Describe interpreters and explain the difference
between interpreters and compilers.

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-2


All Rights Reserved
Introduction to Java

Basic Computer Organization

• At its most basic a computer receives input, performs


computation and produces output

Input Computation Output

− Common input devices include the keyboard, mouse and


scanner
− Common output devices are the console display (screen) and
printer
− Some devices like modems are used for both input and output

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-3


All Rights Reserved
Introduction to Java

CPU and Memory

• Refining our basic diagram, the middle portion of the


computer consists of a central processing unit (CPU)
and memory

CPU

Input Output

Memory

• The CPU performs calculations, such as arithmetic


and logical operations
− Modern CPUs are implemented on a single microprocessor
chip, such as the Intel Pentium
− CPU speed is measured in millions of operations per second
(megahertz or MHz)

• Memory is used to store temporary data and


programs
− Memory is also implemented in computer chips
− Memory is measured in millions of bytes (megabytes or MB)

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-4


All Rights Reserved
Introduction to Java

Secondary Storage

• Refining our basic diagram some more, we can add


secondary storage

CPU

Memory
Input Output

Secondary
Storage

• Secondary storage holds much more data than


memory and is commonly implemented as disks
− Common unit for secondary storage is gigabyte (GB), which
is approximately one billion bytes

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-5


All Rights Reserved
Introduction to Java

Bits and Bytes

• The fundamental unit of information in a computer is


bit (or “binary digit”)
− A bit corresponds to the possible positions of an electrical
switch (off or on)
− A bit is represented by the number 0 or 1

• Computer memory is organized in larger units called


bytes, which consist of 8 bits
− A kilobyte (KB) is approximately one thousand bytes (1024)
− A megabyte (MB) is approximately one million bytes
(1048576)
− Even larger units are now commonly used in the computer
industry, such as gigabyte (approximately one billion bytes)
and terabyte (approximately one trillion bytes)

• Sometimes the term nibble is used to refer to 4 bits

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-6


All Rights Reserved
Introduction to Java

Powers of Two

• Powers of two are ubiquitous in computers

N 2 to power N

0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024 kilo
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536
17 131072
18 262144
19 524288
20 1048576 mega

...

30 1073741824 giga

...

40 1099511627776 tera

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-7


All Rights Reserved
Introduction to Java

Binary Number System

• The binary number system uses 2 as the base and is


the fundamental number system for computers
− Numbers are represented as sums of powers of two
− 0 and 1 is used to indicate absence or presence of a particular
power of 2
− To convert from binary to decimal, start from right of the
binary number (least significant digit) and add the
corresponding power of 2 for each 1
Examples
1 1
10 2
100 4
1000 8
10000 16
100000 32
1000000 64

1001101 = 1 + 4 + 8 + 64 = 77

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-8


All Rights Reserved
Introduction to Java

Hex Numbers

• The hexadecimal number system based on 16 is more


convenient for displaying binary numbers for a
human reader

Binary Hex Decimal

0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 A 10
1011 B 11
1100 C 12
1101 D 13
1110 E 14
1111 F 15

− To represent a binary number in hex, group the binary digits


in groups of 4 and take the hex equivalent for each group

1011001101011100 à 1011 0011 0101 1100


à B35C

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-9


All Rights Reserved
Introduction to Java

Exercises

• Take a few minutes to do the following exercises.


When done you can check your work against answers
on following page
− Refer to table of powers of two on page 0-7 as needed
1. Convert the following binary numbers to decimal:

1011
11100101

2. Convert the following binary numbers to hex

1011100011011111
1100000100110111

3. Convert the following hex numbers to binary

AAFF
10C9

4. Create a power of tables of 16 for n = 0 to 5

5. Convert the following hex numbers to decimal

1A
2AF

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-10


All Rights Reserved
Introduction to Java

Answers

1. Convert the following binary numbers to decimal:

1011 à 1 + 2 + 8 = 11
11100101 à 1 + 4 + 32 + 64 + 128 = 229

2. Convert the following binary numbers to hex

1011 1000 1101 1111 à B8DF


1100 0001 0011 0111 à C137

3. Convert the following hex numbers to binary

AAFF à 1010 1010 1111 1111


10C9 à 0001 0000 1100 1001

4. Create a power of tables of 16 for n = 0 to 5

0 1
1 16
2 256
3 4096
4 65536
5 1048576

5. Convert the following hex numbers to decimal

1A à 10(1) + 1(16) = 26
2AF à 15(1) + 10(16) + 2(256) = 687

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-11


All Rights Reserved
Introduction to Java

Arithmetic, Logic and Branching

• Computers perform both arithmetic and logical


operations
− Arithmetic operations include addition, subtraction,
multiplication and division
− Logical operations include comparisons (less than, greater
than, equal to, etc.) and Boolean operations (and, or, not, etc.)

• Based on the results of a logical operation, a


computer may branch to perform different possible
operations
− Computers thus may make decisions and so have a kind of
“intelligence”
− Branching also allows computers to do operations over and
over again (e.g. add a million numbers)

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-12


All Rights Reserved
Introduction to Java

Programs

• Computers can be instructed to perform specific


operations in a sequence
• The set of instructions to a computer is called a
program
• Early computers had the instructions specified by
electrical circuits
− To change the instructions you had to change the wiring

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-13


All Rights Reserved
Introduction to Java

Stored Programs

• A great advance in the design of computers is the


concept of a stored program
− The program is stored in the memory of the computer
− Executing a program involves loading instructions from the
computer memory to the CPU and carrying out the
instructions
− To change a program all you have to do is to change the
instructions in memory
− Stored programs are sometimes referred to as software in
distinction to the wiring of the computer, which is hardware

• The invention of the concept of a stored program is


generally attributed to the mathematician John von
Neumann
− The basic design of computers that utilize stored programs is
commonly referred to as the “von Neumann architecture”

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-14


All Rights Reserved
Introduction to Java

Machine Language

• Since instructions are stored in memory, they are like


any other kind of information in memory and are
represented by binary numbers
• These binary instructions to the computer are
referred to as machine language
• Machine language programs are very low-level
− They consist of a sequence of binary numbers
− They are specific to a particular computer

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-15


All Rights Reserved
Introduction to Java

CPU Organization

• To better understand machine language let’s look at


the organization of a simple CPU
− The Program Counter holds the address of the instruction to
be executed
− The Instruction Register holds the instruction itself
− The Accumulator holds the data currently being worked on
− The Arithmetic Logic Unit (ALU) performs the arithmetic
and logical operations
− The computer’s memory holds both program instructions and
data

Accumulator

Instruction Register ALU CPU

Program Counter

Program

Memory

Data

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-16


All Rights Reserved
Introduction to Java

Memory Organization

• In most computers memory is organized in bytes


− Recall that a byte is 8 bits

• An individual piece of data may consist of several


bytes
− For example an ordinary integer may be 4 bytes, a short
integer 2 bytes, and a long integer 8 bytes
− Sometimes a computer has a standard word size, where a
word is a certain number of bytes

• A computer instruction will consist of a certain


number of bytes
• Data or instructions in memory is specified by a
memory address
− Memory addresses begin at 0 and extend to size of memory
minus 1
− For example if a computer has memory of size 64KB (tiny by
modern standards but typical for early PCs), memory address
is in the range (see chart of powers of two on page 0-7)
0 First byte of memory
65535 Last byte of memory

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-17


All Rights Reserved
Introduction to Java

Program Execution

1. Load program from secondary storage into memory.

2. Initialize Program Counter to starting address (e.g. 0).

3. Load Instruction Register with instruction at location specified


by Program Counter.

4. Increment Program Counter.

5. Execute instruction (if a branch, the Program Counter will be


changed to be other than the next sequential instruction).

6. Loop back to Step 3.

Some instructions will involve loading data from Memory into


Accumulator or storing data from Accumulator into Memory.
Other instructions will involve performing an arithmetic operation
on the data in the Accumulator. A “halt” instruction will cause the
instruction cycle loop to exit and stop execution of the CPU.

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-18


All Rights Reserved
Introduction to Java

Instruction Set

• All the possible instructions that a computer can


perform are referred to as its instruction set
• Instructions have a specific format
− A portion of the instruction is the opcode (operation code)
that specifies what instruction
− A portion of the instruction may specify a memory address
for loading or storing data, or an address for a branch to
another instruction

Opcode Address

• Instructions have an associated symbolic mnemonic


such as LOAD, STORE, ADD, etc. that is easier to
work with than numerical opcodes
• Instruction sets may be simple or complex
− RISC (“Reduced instruction set computer” chips have a
relatively simple, uniform instruction set (e.g. Sun Sparc)
− CISC (“Complex instruction set computer” chips have a
complicated instruction set with many variations (e.g. Intel
x86 family)

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-19


All Rights Reserved
Introduction to Java

Sample Computer

• Our sample computer is called SICOM (“Simple


Computer”) and has the following features
− 256 bytes of Memory
− One byte is thus adequate to specify an address
− A word is 2 bytes
− All instructions are 2 bytes with the format

Opcode Address

− If an instruction has no address field, use 00


− Instruction Register and Accumulator are 2 bytes
− 16-bit integer arithmetic is performed
− Program Counter is 1 byte

• Instruction set is shown on following page


− Opcodes are shown in hex along with associated mnemonic
− Mem[addr] is contents of memory (one word) at address
addr
− ACC is the Accumulator and PC is the Program Counter

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-20


All Rights Reserved
Introduction to Java

SICOM Instruction Set


Opcode Mnemonic Meaning

00 HALT Halt the CPU

01 LOAD addr ACC ß Mem[addr]


02 STORE addr Mem[addr] à ACC

03 ADD addr ACC ß ACC + Mem[addr]


04 SUB addr ACC ß ACC - Mem[addr]
05 MULT addr ACC ß ACC * Mem[addr]
06 DIV addr ACC ß ACC / Mem[addr]

07 BR addr PC ß addr
08 BEQ addr PC ß addr if ACC equal 0
09 BNE addr PC ß addr if ACC not equal 0
0A BGT addr PC ß addr if ACC > 0
0B BGE addr PC ß addr if ACC >= 0
0C BLT addr PC ß addr if ACC < 0
0D BLE addr PC ß addr if ACC <= 0

0E READ ACC ß data from keyboard


0F WRITE ACC à display on screen

10 NEG ACC ß - ACC

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-21


All Rights Reserved
Introduction to Java

A Machine Language Program

• Consider the following machine language program


− Notice that all instructions are two bytes, as we said
OE00
0206
0E00
0306
0F00
0000

• Assume that this program is loaded in SICOM’s


memory beginning at address 0 and execution begins
at address 0
− What does this program do?

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-22


All Rights Reserved
Introduction to Java

Machine Language Program (Cont’d)

• The program becomes clearer if we translate the


numeric opcodes to mnemonics and show the address
of each instruction
− Note that two hex digits (one byte) are sufficient to specify
an address
Address Machine Language Mnemonic

00 OE00 READ
01 0206 STORE 06
02 0E00 READ
03 0306 ADD 06
04 0F00 WRITE
05 0000 HALT
06

− The program is stored in Memory between 00 and 05. Data


is stored at 06. Following is done:
1. A number is read into Accumulator from keyboard
2. The number is stored in Memory at 06
3. A second number is read into Accumulator from keyboard
4. The first number (stored at 06) is added to the second number
(now in Accumulator) and the result is stored in the Accumulator
5. The answer is written to the display
6. The CPU halts

• The programs computes the sum of two numbers!

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-23


All Rights Reserved
Introduction to Java

Assembly Language

• Programming in machine language is exceedingly


tedious because everything (opcodes and addresses)
are treated as numbers, like the machine does
• Assembly language simplifies programming by
allowing the programmer to specify opcodes by
mnemonics and addresses by symbolic names
• An assembler is a program which translates assembly
language into machine language
• The program that reads two numbers from the
keyboard and writes out the sum to the display could
be expressed in assembly language as follows:
READ
STORE A
READ
ADD A
WRITE
HALT

− The symbol A is used as a symbolic name for a memory


location to store the first number

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-24


All Rights Reserved
Introduction to Java

A Program That Branches

• As a more interesting example we will write an


assembly language program that branches
• Calculate the absolute value of a number that is read
in from the keyboard and display the result
− If a number X is non-negative, absolute value of X is X (for
example, absolute value of 5 is 5)
− If X is negative, absolute value of X is –X (for example,
absolute value of -5 is 5)

READ
BGE DONE
NEG // reverse sign
DONE: WRITE
HALT

• Note the use of the label DONE


− A label is the symbolic name of an address that is the target
of a branch

• Also note the use of a comment beginning with a


double slash
− The comment text is for a human reader and will be ignored
by the assembler

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-25


All Rights Reserved
Introduction to Java

Symbol Table

• To translate an assembly language program into


machine language, you must determine the numerical
address of each symbol that appears
• The symbol table contains the correspondence
between symbolic names of address and the numeric
values of these addresses
• By laying out an assembly language program in
memory you can determine values of symbols
Address Label Mnemonic

00 READ
01 BGE DONE
02 NEG
03 DONE: WRITE
04 HALT

− Hence the symbol table is as follows


Symbol Value

DONE 03

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-26


All Rights Reserved
Introduction to Java

High Level Language

• Although assembly language is easier to write than


machine language, it is still quite low-level
− Assembly language is 1-1 with machine instructions
− Assembly language is specific to a particular machine and
hence is not “portable” to other machines

• A high level language abstracts away details of a


particular machine and enables programs to be
written with simpler language constructs
− A single high level language instruction can correspond to
multiple machine (assembly) language instructions

Example

SUM = A + B // high level language

LOAD A
ADD B
STORE SUM // assembly language

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-27


All Rights Reserved
Introduction to Java

Compilers

• A program that translates a high level language


program to machine language is called a compiler
• The high level language program is created in a
source file
• The translated machine language program is stored
in a binary file (or object file or executable file)
• Once translated the program runs at the native speed
of the machine
• But building the program can be time consuming,
especially for large programs consisting of many
source files
• Typical development cycle is:
− Edit (create source files)
− Compile (create executable file)
− Run the executable
− Go back to Edit

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-28


All Rights Reserved
Introduction to Java

Interpreters

• An alternative approach is to interpret the high-level


language program
− The program is never translated into native machine language
− Instead a special program called an interpreter is used each
time the program is run to invoke appropriate machine
language instructions to carry out the steps specified by the
high level language
− Sometimes an intermediate language is created by a
compiler, and this intermediate language is interpreted

• There are tradeoffs between using compiler and


interpreter approaches
− Compiler takes longer in development, but the final program
runs faster
− Interpreter lends itself to fast development, but the final
program runs more slowly
− The interpreter can perform various run-time checks, making
the behavior of the executing program more robust
− With different interpreters available on different machines for
the same source language, you can run the same program
unchanged on multiple machines

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-29


All Rights Reserved
Introduction to Java

Lab 0

Machine Language and Assembly Language

In this lab you will get additional practice in working with machine
language and assembly language. There are three exercises:

1. Translate an assembly language program into machine


language, given the symbol table

2. Create an assembly language program to solve a problem.

3. Calculate the symbol table for this program and translate to


machine language

Detailed instructions are contained in the Lab 1 write-up at the end


of the chapter.

Suggested time: 45 minutes

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-30


All Rights Reserved
Introduction to Java

Summary

• At its most basic a computer reads input, performs


computations and writes output.
• The central processing unit (CPU) performs the
calculations.
• Memory stores both data and program instructions.
Secondary storage holds a larger amount of data
persistently.
• Binary is the native number system of computers,
and hex numbers are more convenient for humans.
• A stored program holds program instructions in
memory.
• These binary instructions to the computer are
referred to as machine language.
• Assembly language uses mnemonics but is 1-1 with
machine language
• A high level language abstracts away details of a
particular machine and enables programs to be
written with simpler language constructs.
• A compiler translates a high-level program to
machine language prior to execution, and an
interpreter performs this translation at runtime.

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-31


All Rights Reserved
Introduction to Java

Lab 0

Machine Language and Assembly Language

Introduction

In this lab you will get additional practice in working with machine language and
assembly language. There are three exercises:

1. Translate an assembly language program into machine language, given the symbol
table

2. Create an assembly language program to solve a problem.

3. Calculate the symbol table for this program and translate to machine language

Suggested Time: 45 minutes

Exercise 1. Absolute Value Program

Examine the Absolute Value program and its Symbol Table on page 0-25 and
page 0-26. Translate the program into machine language. You will also need the table of
opcodes on page 0-21.

Exercise 2. Maximum of Two Numbers

Write an assembly language program to read two numbers from the keyboard,
determine the maximum value of these numbers, and display the result. You will need to
store both of the original numbers in memory.

Exercise 3. Symbol Table and Machine Language Program

Make a symbol table for the program in Exercise 2 and translate into machine
language.

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-32


All Rights Reserved
Introduction to Java

Lab 0 Answer

Exercise 1. Absolute Value Program

Address Label Mnemonic Machine Language

00 READ 0E00
01 BGE DONE 0B03
02 NEG 1000
03 DONE: WRITE 0500
04 HALT 0000

Exercise 2. Maximum of Two Numbers


READ
STORE A
READ
STORE B
SUB A // B – A
BGE MAXB
LOAD A // A is max
WRITE
HALT
MAXB: LOAD B // B is max
WRITE
HALT

Exercise 3. Symbol Table and Machine Language Program

We first lay out the program in memory to determine the value of the label and
the addresses of the variable A and B (we will place them immediately after the
program).

Address Label Mnemonic

00 READ
01 STORE A
02 READ
03 STORE B
04 SUB A // B – A
05 BGE MAXB
06 LOAD A // A is max
07 WRITE
08 HALT
09 MAXB: LOAD B // B is max
0A WRITE
0B HALT
0C Store A here
0D Store B here

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-33


All Rights Reserved
Introduction to Java

The symbol table can now be easily determined

Symbol Value

MAXB 09
A 0C
B 0D

We can now add the machine language representation

Address Label Mnemonic Machine Language

00 READ 0E00
01 STORE A 020C
02 READ 0E00
03 STORE B 020D
04 SUB A 040C
05 BGE MAXB 0B09
06 LOAD A 010C
07 WRITE 0F00
08 HALT 0000
09 MAXB: LOAD B 010D
0A WRITE 0F00
0B HALT 0000
0C Store A here
0D Store B here

Rev. 1.1 Copyright © 2000 Object Innovations, Inc. 0-34


All Rights Reserved

You might also like