Ch3 Programing Language Level

Download Report

Transcript Ch3 Programing Language Level

CS 147
June 13, 2001
Levels of Programming
Languages
Svetlana Velyutina
Overview
Levels of programming languages.
Their relationship to each other.
How they’re converted into executable form.
How Java applets are converted to run on a computer.
Attributes of assembly language instructions.
LANGUAGE CATEGORIES
• High- Level Languages
• Assembly Languages
• Machine Languages
High- Level Languages
• platform independent
• highest level of abstraction
• Java, C++, FORTRAN
Assembly Languages
• lower level of abstraction
• specific to each microprocessor
• usually backward compatible
• can directly manipulate data stored in a
microprocessors internal components
• platform-specific
Machine Languages
• contain binary values
• platform specific
• each microprocessor has own language
• may be backward compatible
High-level Language Programs are
Compiled
High level-language
sourse code
Compiler for Pentium
Windows PC
Compiler for G4
Power Mac Computer
Compiler for SPARC
UNIX workstation
Pentium object code
G4 object code
SPARC object code
Other Pentium
object files
Other G4
object files
Other SPARC
object files
Pentium linker
G4 linker
SPARC linker
Pentuim
executable file
G4
executable file
SPARC
executable file
Windows
Pentium PC
G4
Power Mac
SPARC
UNIX workstation
Assembly Language Programs are
Assembled
Assembly language
program for processor X
Assembler for
processor X
Processor X object code
Other processor X object files
Processor X linker
Processor X
executable file
Computer with processor X
Java Applets are Compiled
into Bytecode
Java applet
source code
Java compiler
Bytecode
Java VM for
Windows Pentium PC
Java VM for
G4 Power Mac
Java VM for
SPARC UNIX workstation
Windows
Pentium PC
G4
Power Mac
SPARC
UNIX workstation
Applet is a Java program running inside a browser.
Bytecode
• Bytecode is generated by Java compiler to be
later interpreted by Java Virtual Machine and
translated into machine code at run-time.
• JVM interprets the bytecode for its specific
platform and executes it.
• Therefore, bytecode is platform independent.
• JVM can be a hardware chip but usually it is a
program, often part of Web browser.
• Most browsers are Java-enabled and most
operating systems have JVM ported on them.
Bytecode
• Bytecode can not be directly executed by
microprocessor - code runs much slower
• Therefore it would not be efficient to do
CPU-intensive tasks with JVM interpreter
• Still Java can easily keep up with the data rate
of network connection.
• Many platforms have just-in-time bytecode
compilers that compile code once, cach the
results and call it again if needed.
• Just-in-time compilers will speed up the
program 10 - 20 times compare to interpreter.
Attributes of Assembly
Language Instructions
• instruction types
• data types
• addressing modes
• instruction formats
Assembly Language
Instruction Types
• data transfer instructions
• data operation instructions
• program control instructions
• interrupts
• halt instruction
Data Transfer Instructions
• most common microprocessor operation
• copy the value to its destination
• perform following transfers:
– Load data from memory into the
microprocessor
– Store data from microprocessor into memory
– Move data within the microprocessor
– Input data to the microprocessor
– Output data from the microprocessor
Data Operation Instructions
• Arithmetic, Logic and Shift instructions
• modify their data values
• perform operation on one or two data
values and store result
Program Control Instructions
• jump or branch instructions used to go to
another part of program
• absolute jump is always taken:
jr $ra
• conditional jump is taken if condition is met:
bne $t0, $t1, endloop
• instructions to call and return from subroutines:
jal calcsqrt
Interrupts
• tells microprocessor to stop and execute
another instruction
• software interrupts are generated by assembly
language instructions
• hardware interrupts are triggered by devices
outside of microprocessor
Halt instruction
• causes microprocessor to stop executing
instructions
• used at the end of program
Most Common Data Types
• Integers
• Floating point numbers
• Boolean values
• Characters
Integers
• range from 0 to 2^n - 1 for unsigned
• from -2^n to 2^(n-1)-1 for signed
Floating Point Numbers
• include fractional portion of the value
• may be assigned special registers
and instructions by microprocessor
Boolean Type
• true or false values
• zero is false, non-zero is true
• can be used to perform logical
operations
• Ex.: result of 0010 AND 0001 is true
Characters
• stored as binary values
• encoded using ASCII, EBCDIC,
UNICODE or other encoding standard
Addressing Modes
Assume LDAC loads data from memory into microprocessor’s AC register.
• Direct Mode: LDAC 5
(loads value into accumulator from memory location 5)
• Indirect Mode: LDAC @5
(loads value from the memory address stored at memory location 5)
• Register Direct Mode: LDAC R
(copies value stored in register R)
• Register Indirect Mode: LDAC @R
(copies value from the memory address stored in register R)
Addressing Modes
• Immediate Mode: LDAC #5
(moves value 5 into accumulator)
• Implicit Mode: LDAC
(gets an operand from stack, don’t have to specify an operand)
• Relative Mode: LDAC $5
(the operand supplies offset from the program counter to generate address)
• Index Mode: LDAC 5(X)
(loads the data from X+5 memory location)
• Address Mode: LDAC 5(X)
(loads the data from memory address(X+5))
Instruction Formats
Instructions code - binary value
representing an assembly language
instruction after it is converted into
machine code.
Different instructions may have different
formats.
Groups of bits in a format correspond to
the opcode and the operands.
Example for the operation A=B+C
This instruction has one operation, two source
operands and one destination operand.
Microprocessor performing 16 different
operations will need 4 bits to specify one
operation.
Assume bit pattern 1010 corresponds to
addition.
Assume that there are only 4 possible operands
for this operation A, B, C and D.
Corresponding bit patterns for the operands: 00
for A, 01 for B, 10 for C and 11 for D.
Instruction Formats
4 bits
2 bits
2 bits
2 bits
opcode operand #1 operand #2 operand #3
assembly code
ADD A,B,C (A=B+C)
machine code
1010 00 01 10
Format for three-operand instruction.
4 bits
2 bits
opcode operand #1
2 bits
operand #2
MOVE A,B (A=B)
ADD A,C (A=A+C)
Format for two-operand instruction.
1000 00 01
1010 00 10
Instruction Formats
4 bits
2 bits
opcode operand
assembly code
machine code
LOAD B (Acc=B)
0000 01
ADD C
(Acc=Acc+C) 1010 10
STORE A (A=Acc)
0001 00
One-operand instruction. The accumulator register is
always used as destination and one of source registers.
4 bits
opcode
PUSH B
PUSH C
ADD
POP A
(Stack=B)
(Stack=C,B)
(Stack=B+C)
(A=Stack)
0101
0110
1010
1100
Zero-operand instruction. All operands are drawn from
the stack.
Instruction Formats
Microprocessor may be designed to work with
instructions that specify 3, 2, 1 or 0 operands.
Microprocessor that uses two-operand
instructions is more limited than one using
three-operand instructions.
Microprocessors with Fewer
Operand Instructions
Drawbacks:
• more instructions needed for the same task
Benefits:
• instruction codes use fewer bits
• hardware to implement microprocessor is less complex
• microprocessors whose instructions specify fewer
operands can usually execute instructions more quickly