PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted...

23
PROGRAMMING THE COMPUTER

Transcript of PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted...

Page 1: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

PROGRAMMING THE COMPUTER

Page 2: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

• programming languages• syntax and semantics• compiler/interpreter• assembler• linker• object file (*.o)• executable file (a.out)• loader• process• process address space

Topics

Page 3: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate
Page 4: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

a programming language is a formal language that specifies a set of instructions that can be used to create

programs that implement specific algorithms.

Page 5: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

Calculate max(a,b,c)

Page 6: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

PROGRAMMING LANGUAGES

Page 7: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate
Page 8: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate
Page 9: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

Important Aspects of a Program

syntax: is the program written according to the programming language grammar?

e.g., int if = 2;

semantics: is the program well formed?

e.g., int x = 2, y = 5; float z = x + y;

Page 10: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

• compiler takes entire program as input

• full program is checked for errors

• requires more memory

• program is compiled once

• e.g., C, C++, Java

Compiled Languages

Page 11: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

• interpreter takes each instruction as input

• instruction (only) is checked for errors

• instruction is executed at once

• less memory required

• program is interpreted everytime

• e.g., Bash, Python, Ruby

Interpreted Languages

Page 12: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

Ca compiled language

case study #1

Page 13: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

original (source) code in C

Page 14: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

intermediate code (assembly

representation)MIPS R2000

Page 15: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

native code (binary)

MIPS R2000

Page 16: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

compilers transform source code into valid/executable binary code

instructions and data translated into binary sequences, the only language

understood by microprocessors

only some binary sequences are valid/executable

the same source program is compiled into different binary sequences for different microprocessors, e.g., Intel x86 or ARM

binary code is hardware dependent, source code is not

Page 17: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

Compiling a C program

Page 18: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

Basic compilation steps

Page 19: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

Linking object file with libraries

Page 20: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

• checks the object file to identify required missing binary code

• searches other object files and operating system libraries for those pieces of code

• determines in which position they must be inserted in the object file and adjusts references

• produces executable file (or fails)

The Program Linker

Page 21: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

a program becomes a process

Page 22: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

The Program Loader(part of the operating system)

• creates an address space in main memory large enough to hold the program, a heap and a stack

• copies the code and data of the program to the address space, and the command line arguments to the stack

• initialises register values including the stack pointer and the program counter

• the original program is now ready to run and is called a process

Page 23: PROGRAMMING THE COMPUTER - DCClblopes/aulas/ic/T9.pdf · PROGRAMMING THE COMPUTER ... Interpreted Languages. C a compiled language case study #1. original (source) code in C. intermediate

• this happens when you click on an application icon in the Desktop

• or, when you execute a program directly from the command shell as in:

$./a.out