Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row...

16
Programming

Transcript of Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row...

Page 1: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Programming

Page 2: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Memory Address Machine Code (hex) Assembler Instructions (Poorly coded) C program

0x100000e9b0x100000ea2

0x100000eb90x100000ebc0x100000ebf0x100000ec50x100000ec90x100000ecd0x100000ed00x100000ed30x100000ed60x100000ed90x100000ede0x100000ee1

0x100000ee60x100000ee90x100000eea

c7 45 f0 00 00 00 00c7 45 ec 00 00 00 00

8b 45 ec3b 45 f40f 8d 21 00 00 0048 63 45 ec48 8b 4d f88b 14 8103 55 f089 55 f08b 45 ec05 01 00 00 0089 45 ece9 d3 ff ff ff

8b 45 f099f7 7d f4

movl $0x0, -0x10(%rbp)movl $0x0, -0x14(%rbp)

movl -0x14(%rbp), %eaxcmpl -0xc(%rbp), %eaxjge 0x100000ee6movslq -0x14(%rbp), %raxmovq -0x8(%rbp), %rcxmovl (%rcx,%rax,4), %edxaddl -0x10(%rbp), %edxmovl %edx, -0x10(%rbp)movl -0x14(%rbp), %eaxaddl $0x1, %eaxmovl %eax, -0x14(%rbp)jmp 0x100000eb9

movl -0x10(%rbp), %eaxcltdidivl -0xc(%rbp)

int sum = 0;int i = 0;

loop:if (i >= length(values))

goto done;sum += values[i];

i = i + 1;

goto loop;done:

mean = sum / i;

Page 3: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Machine level programming

Not portable: depends on specific CPU model Error proneHard to handle complex problems Machine dictates the language in which the programmer thinksFull controlNeed more problem-oriented languages.Need more abstract and safer languages.Abstraction versus control is a tradeoff!

Page 4: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Programming paradigms

Computation: sequential/concurrent/parallel

Control flow: imperative/declarative

.. to name a few…

Page 5: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Imperative Paradigm

1. Log in to StReaMS2. Click “Register” in the row of comp11003. Choose the lab L that you prefer among all labs available4. For every week i from 1 to 12

1. Come to lab L in week i2. Solve all exercises of lab i3. Submit your solution before lab (i+1)

Goal: Get 20 points for labs of comp1100

Page 6: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Declarative (functional is a subcategory of declarative)

• StReaMS is a system that is used to enroll in lab groups• A student has access to StReaMS• Enrolment means a lab is associated with a student • A student can enroll to one lab per week• Each lab is specified by

• location• weekday • time • tutor

• A tutor can work in at most one lab at a time• A student obtains 1 point for attending 1 lab per week• A student obtains 1 point for completing a before the next lab• There is an ordered sequence of 12 labs• …

Page 7: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Early programming languages

Inspired by machine code - macro assemblers (imperative)Fortran (‘57), Cobol (‘59), Basic (‘64), C(‘71)

Based on formal model of computation - lambda calculus (functional)Lisp (‘58), ML (‘73)

Structured and strongly typed (imperative)Algol (58), Algol 60, Algol 68, Pascal (‘70)

Based on message-passing simulation models (object-oriented)Simula (‘67), Smalltalk (‘69)

Based on first-order logic (declarative)Prolog (‘72)

Page 8: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Influential languages (≠popularity!)

Conceptual foundationsλ-calculus/Lisp, Simula/Smalltalk, Algol, Prolog

Also influentialML/Haskell, Pascal, Eiffel, Ada, Modula-3, C, Java

Check out http://exploringdata.github.io/vis/programming-languages-influence-network/

e.g., PHP widely used, but not particularly influential…

Page 9: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every
Page 10: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

How is software written

The source code is written in

• Text Editor - E.g., Notepad, Vim, Emacs, …• Integrated Development Environment (IDE) – E.g. IntelliJ

IDEs allow editing, compiling, executing, and debugging from one environment ⇒ rapid development cycle

Page 11: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

How is executedCompiler: program that translates source program into machine instructions

E.g.: Haskell

Interpreter: program that interprets (executes) a source program

E.g.: Smalltalk, PerlMany languages have been implemented using both compilers and interpreters, including BASIC, C, Lisp, Pascal, Python.

Page 12: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Programming: craft and correctness

A human activity (“the art of programming”)• A program can be well-crafted and aesthetically pleasing• A program can be unmaintainable or unreadable

Programs are formal artefacts• An expression in (temporal) logic• Can be correct/incorrect (against a specification)

Page 13: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Obfuscated codeThe International Obfuscated C Code Contest

http://www.ioccc.org/2014/endoh2/prog.c

#include <stdio.h>#define TA q=/*XYXY*/#define/*X YXY*/CG r=void p(int n,int c){;for(;n--;) putchar(c)#define Y( z)d;d=c++\%2<1?x=x*4 +z,c%8>5?\x=x?p(1,x), 0:x:0:0;d=#define/*X YX*/C Y(1)#define/*X YX*/G Y(2);}int(*f)( void),d,c,#define/*X YX*/A Y(0)#define/*XY*/AT int\

m(void/**/){d=#define/*XYX*/T Y(3)

#define GC d; return\0;}int(*f) (void )=m;

x,q,r; int main(){if(f)f();else {for(puts("#include" "\40\"pro\g.c\"\n\n \101T"+0);d=!d?x=(x= getchar())<0?0:x,8*8 :d,TA++c%8,TA(1+7*q- q*q)/3,r=c*15-c*c-36 ,p(r<0?!q+4:r/6+!q+4 ,32),q||x;c%=16)q?p( 1,"ACGT"[x/d&3]),p(q ,126),p(1,"TGCA"[x/d &3]),d/=4,p(001,10): puts(c%8?\"CG":"TA") ;puts("GC");}return 0;}/**/

Page 14: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Programming myths

It’s easy, everybody can do it with just a few tutorialsEspecially sales people for the latest trendy language

It’s intrinsically hardWhat old programmers say to keep their jobs

A weird way to spend your dayHollyword nerd stereotypes

In realityA specialized, historically recent, technological, professional activity, which requires care and focus (just like any advanced profession)

Page 15: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

Decision Problems

Is a program syntactically correct? Is x a prime number? Will a program stop for a given input?

The last one is the halting problem, which is representative of a class of inherently undecidableproblems

Alan Turing (’37): “no program can decide whether another arbitrary program will terminate on a given input”

Page 16: Programming - GitLab · Imperative Paradigm 1.Log in to StReaMS 2.Click “Register” in the row of comp1100 3.Choose the lab Lthat you prefer among all labs available 4.For every

What makes a professional programmer?“Fluent” in all essential programming constructs and paradigmsCan choose and use the right tools for the jobConsiders capabilities of available hardwareUnderstands translation into executable machine code and how to control that translationUnderstands testing and verification and uses them adequatelyFinds best suited abstractions and modularisations (experience!)Knows how to analyse unexpected problems (debugging)