Principles of Programming Languages Kristopher Micinski This class - - PowerPoint PPT Presentation

principles of programming languages
SMART_READER_LITE
LIVE PREVIEW

Principles of Programming Languages Kristopher Micinski This class - - PowerPoint PPT Presentation

Principles of Programming Languages Kristopher Micinski This class is about understanding how programs work To do this, were going to have to learn how a computer works Heres a program in a new language, C++ C++ is a compiled language


slide-1
SLIDE 1

Principles of Programming Languages

Kristopher Micinski

slide-2
SLIDE 2

This class is about understanding how programs work

slide-3
SLIDE 3

To do this, we’re going to have to learn how a computer works

slide-4
SLIDE 4

Here’s a program in a new language, C++ C++ is a compiled language A translator (compiler) turns C++ into binary code

slide-5
SLIDE 5
slide-6
SLIDE 6

Main procedure Program starts here

slide-7
SLIDE 7

sum function (calculates sum(0 to number))

slide-8
SLIDE 8

Here’s a program in a new language, C++ C++ is a compiled language A translator (compiler) turns C++ into binary code

slide-9
SLIDE 9

Binary: The native language of the processor

  • Modern processors are very fast
  • (m/b)illions of instructions per sec

Processors execute a small number

  • f very basic instructions

MOV r1, r2 ADD r1, r2,r3 IFZERO r1, +20 These instructions written in a binary encoding (Why?)

slide-10
SLIDE 10

Binary: The native language of the processor

  • Modern processors are very fast
  • (m/b)illions of instructions per sec

Processors execute a small number

  • f very basic instructions

MOV r1, r2 ADD r1, r2,r3 IFZERO r1, +20 These instructions written in a binary encoding (Why?) Compact representation Quick to decode and execute

slide-11
SLIDE 11

Thousands of different processors Each speaks a different language Called its architecture Different versions of architecture add features, etc..

slide-12
SLIDE 12

So I need to turn this into something my i7 speaks…

slide-13
SLIDE 13

To do that, I use a compiler

slide-14
SLIDE 14

g++ sumnums.cpp -o sumnums

“Compile a file named sumnums.cpp, and output an executable file named sumnums”

slide-15
SLIDE 15

g++ sumnums.cpp -o sumnums

“Compile a file named sumnums.cpp, and output an executable file named sumnums”

(Ton of options here, especially for large projects with complex configs / multifiles)

slide-16
SLIDE 16

Compiler

slide-17
SLIDE 17

So, the compiler turns C++ into a giant list of these instructions…

slide-18
SLIDE 18

So, the compiler turns C++ into a giant list of these instructions…

These are written in assembly (Human-readable binary)

slide-19
SLIDE 19

Let’s see what assembly the compiler generates…

slide-20
SLIDE 20

g++ -S sumnums.cpp

(Note I really used: g++ -S sumnums -fno-asynchronous-unwind-tables This is because otherwise extra debugging overhead is inserted.)

slide-21
SLIDE 21

Divided up by function

slide-22
SLIDE 22

Divided up by function Implementation of sum

slide-23
SLIDE 23

Divided up by function Implementation of main Don’t worry that this code is hard to understand for now (It also confuses me..)

slide-24
SLIDE 24

I can manually transform the assembly to the binary…

as sumnums.s

slide-25
SLIDE 25

Crud…

slide-26
SLIDE 26

Insight: my program needs a lot of other stuff to run…

For example: code to print to the screen This is kept in a library

(But keep in mind, that’s also just code. Nothing particularly magical)

slide-27
SLIDE 27

Your code lstdc++ lm etc…

+ =

Executable file

slide-28
SLIDE 28

Can I run a program compiled for

  • ne architecture and use it on

another?

Question (for next time):

slide-29
SLIDE 29

Now: C++ coding

slide-30
SLIDE 30

Next time:

  • More C++ programming nitty-gritty
  • Representing HOFs in C++
  • What’s your computer doing when you call a function
  • How is stack laid out, what is a StackOverflow?
  • How can we avoid them

C++ is a huge language, don’t feel embarrassed if you think you know nothing. I can’t think of a single smart person I know who even claims to know “most” of C++ But I do know some people who admit it’s a useful and powerful tool when you use the right features