39
Low Level Programming and Computer Architecture Lecture 5 Carl Henrik Ek {chek}@csc.kth.se Royal Institute of Technology March 10, 2013 Ek KTH Low Level Programming and Computer Architecture

Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

  • Upload
    vocong

  • View
    225

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Low Level Programming and Computer ArchitectureLecture 5

Carl Henrik Ek{chek}@csc.kth.se

Royal Institute of Technology

March 10, 2013

Ek KTH

Low Level Programming and Computer Architecture

Page 2: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Last Lecture• Abstraction• File formats• Structured Data

Ek KTH

Low Level Programming and Computer Architecture

Page 3: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Abstraction of Data

Ek KTH

Low Level Programming and Computer Architecture

Page 4: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Abstraction of Data

Representing Images• Indexed Images• Truecolour• HAM - Hold And Modify

• Image sizeI 640× 480× 3 =

921600bytes = 900KB

• PBM, PGM, PPM, PNM, BMP• executable

Ek KTH

Low Level Programming and Computer Architecture

Page 5: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Compression

Defintion

f : X → Yg : Y → X

• RAW - uncompressed data• Lossless

I x = g (f (x))• Lossy

I d(x,g (f (x))) 6= 0I What is d(·, ·)

Ek KTH

Low Level Programming and Computer Architecture

Page 6: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Compression: Lossless

Lossless• Run-length encoding• Frequency dependency

I Huffman encoding

• LHA, LZX, ZIP, TAR,GZIP, . . .

Ek KTH

Low Level Programming and Computer Architecture

Page 7: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Compression: Lossless

Huffman encoding• David A. Huffman, 1952

MIT• Entropy encoding• Claude Shannon

I Father of Information theory

Ek KTH

Low Level Programming and Computer Architecture

Page 8: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Compression: Lossless

Dictionary Coding• Dictionary• LZW• Original Unix compress

Ek KTH

Low Level Programming and Computer Architecture

Page 9: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Compression: Lossy

Images• Lots of different formats• Lossless

I TIFF, GIF, BMP

• LossyI JPEG

• Loss function

Ek KTH

Low Level Programming and Computer Architecture

Page 10: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Compression: Lossy

JPEGa

aWikipedia

• Change-of-Basis

f (t) =∫ ∞−∞

f (x)e−2πixtdx

• perception• what is the loss function?

Ek KTH

Low Level Programming and Computer Architecture

Page 11: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

This Lecture• Abstraction

I Left-overs from last time

• Programming• Structured Data

Ek KTH

Low Level Programming and Computer Architecture

Page 12: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Communication

Parity• Check Bit• Even/Odd

I number of set bits needs to beEven/Odd

I Parity bit compensates

• Checksum: CRC• ASCII, TCP/IP traffic

Ek KTH

Low Level Programming and Computer Architecture

Page 13: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Communication

Parity• Check Bit• Even/Odd

I number of set bits needs to beEven/Odd

I Parity bit compensates

• Checksum: CRC• ASCII, TCP/IP traffic

Ek KTH

Low Level Programming and Computer Architecture

Page 14: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Communication

Parity• Check Bit• Even/Odd

I number of set bits needs to beEven/Odd

I Parity bit compensates

• Checksum: CRC• ASCII, TCP/IP traffic

Ek KTH

Low Level Programming and Computer Architecture

Page 15: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Communication

Error Correcting Codes• Detect error• Correct• Distance between codes

Copyright © 2012 Pearson Education, Inc. 0-0

Figure 1.29 An error-correcting code

Ek KTH

Low Level Programming and Computer Architecture

Page 16: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Communication

Copyright © 2012 Pearson Education, Inc. 0-0

Figure 1.30 Decoding the pattern 010100using the code in Figure 1.30

Ek KTH

Low Level Programming and Computer Architecture

Page 17: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Recapitulation

Program Execution• Code is just data• OS Loads executable

I Lecture 7 May 3rd

Ek KTH

Low Level Programming and Computer Architecture

Page 18: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Recapitulation

Ek KTH

Low Level Programming and Computer Architecture

Page 19: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Recapitulation

Ek KTH

Low Level Programming and Computer Architecture

Page 20: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Recapitulation

Ek KTH

Low Level Programming and Computer Architecture

Page 21: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Disassembly• Machine Code and Assembler• Recover Code from

executable• Intel

• Motorla 68k

• AMD Reference Manual

Ek KTH

Low Level Programming and Computer Architecture

Page 22: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Executable

Executable• Mac

I Mach-OI machine.h

• PE - Windows, ELF - Linux• Extended Hunk Format -

AmigaOS• Library

I Static, Dynamic

Ek KTH

Low Level Programming and Computer Architecture

Page 23: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Abstraction II

Programming Languages• Generate Instructions• Levels of abstraction• “Generations”

Ek KTH

Low Level Programming and Computer Architecture

Page 24: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Abstraction II

Second Generation• Assembly• Mnemonics• Injective mapping

(one-to-one)• Assembler

19/3/13 10:16 Parallel byte-wise maximum

Page 1 of 3http://ada.untergrund.net/forum/index.php?action=vthread&forum=4&topic=870

- Forums - Reply - Search - Statistics -

Amiga Demoscene Archive Forum / Coding / Parallel byte-wise maximum

Author Message

BlueberryForums Member

#1 - Posted: 25 Feb 2013 10:04 - Edited by: BlueberryReply Quote

Hi!

It has been discussed in here a couple of times how to do byte-wise saturated add one longword at a time.

But how about byte-wise maximum? If we have byte values up to 127 in all four bytes of D0 and D1, we can perform abyte-wise maximum like this:

move.l #$80808080,d2 sub.l d1,d0 ; byte-wise differences with overflows add.l d2,d0 ; overflows negated, msb of each byte is 1 iff difference is positive and.l d0,d2 ; 128 for bytes where difference is positive, 0 for the others move.l d2,d3 lsr.l #7,d3 sub.l d3,d2 ; 127 for bytes where difference is positive, 0 for the others and.l d2,d0 ; only positive differences add.l d1,d0 ; original + positive differences = maximum

With appropriate pairing, these 9 instructions result in an overhead (compared to simply using one of the sources) of1.125 cycles per byte. One of the sources (D1) can be in memory for no extra cost.

Can it be done better? Can we get down to 1 cycle? :)

Suppose we need, say, 6 bits of precision on our byte values, but those 6 bits have to be placed in the upper bits of eachbyte (in the memory operand and the destination). We can do this by a move (from memory) and an lsr #1 at thebeginning and an lsl #1 at the end, resulting in 3 more instructions (1.5 cycles per byte total). Is there perhaps somebetter way to do it?

jamie2010Forums Member

#2 - Posted: 25 Feb 2013 14:14Reply Quote

Hey,

Do you need to do this sort of operation several times by pixel?

jamie2010Forums Member

#3 - Posted: 25 Feb 2013 14:17Reply Quote

By Pixels i mean for 4 pixels.

daltonForums Member

#4 - Posted: 26 Feb 2013 08:43Reply Quote

I can't think of a quicker way to perform this operation. What bothers me is that so many cycles are spent on creating abit mask from the sign bits (x-x>>7). Another way of doing it would be (x>>7)*127. It saves one line of code and oneregister. But I don't think it's any faster =(

move.l #$80808080,d2 sub.l d1,d0 ; byte-wise differences with overflows add.l d2,d0 ; overflows negated, msb of each byte is 1 iff difference is positive and.l d0,d2 ; 128 for bytes where difference is positive, 0 for the others

Ek KTH

Low Level Programming and Computer Architecture

Page 25: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Abstraction II

Third Generation• Higher-level

I not injective

• Machine independent• Compiled/Interpreted

Ek KTH

Low Level Programming and Computer Architecture

Page 26: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Paradigms

Imperative• Traditional algorithm• Define procedure• State changes• Machine Language, Pascal,C, C++, Python

Ek KTH

Low Level Programming and Computer Architecture

Page 27: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Paradigms

Functional• Express as functional evaluation• Recursion• Avoid state changes• Haskell, Lisp

• Beautiful but slowI academic

Ek KTH

Low Level Programming and Computer Architecture

Page 28: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Paradigms

Declarative• Defines logic• Does not define flow• SQL, Prolog

Ek KTH

Low Level Programming and Computer Architecture

Page 29: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Copyright © 2012 Pearson Education, Inc. 0-0

Figure 6.2 The evolution ofprogramming paradigms

Ek KTH

Low Level Programming and Computer Architecture

Page 30: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Types

Compiled• Translates whole program• Executes program• Intermediate stages

I ParserI Compile timeI Run time

Ek KTH

Low Level Programming and Computer Architecture

Page 31: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Types

Interpreted• Translates and executes• Matlab

• Fast development• Hard to Optimise

Ek KTH

Low Level Programming and Computer Architecture

Page 32: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Types

Typing• Metadata• Strongly Typed

I JAVA, Python, C++

• Weakly TypedI C

• UntypedI Assembler

• Static, Dynamic

Ek KTH

Low Level Programming and Computer Architecture

Page 33: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Virtual Machine• Abstraction of Hardware• Hard to make efficient• Convinient distribution• API

Ek KTH

Low Level Programming and Computer Architecture

Page 34: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Feynman

“You can know the name of abird in all the languages of theworld, but when you’re finished,you’ll know absolutely nothingwhatever about the bird... Solet’s look at the bird and seewhat it’s doing– that’s whatcounts. I learned very early thedifference between knowing thename of something and knowingsomething.”

Ek KTH

Low Level Programming and Computer Architecture

Page 35: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Summary• Imperative• Compiled/Interpreted• Virtual Machine

Ek KTH

Low Level Programming and Computer Architecture

Page 36: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Misc

Turing Completness• Simluate Turing Machine

I States, memory, aritmetic

• All Imperative languages• SQL, HTML

Ek KTH

Low Level Programming and Computer Architecture

Page 37: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Misc

Esoteric Languages• Brainfuck

I Only uses > < + { . , []

I code

• MalbolgeI Eight circle of HellI code

• ShakespeareI code

• Esolang

• IOCCC

Ek KTH

Low Level Programming and Computer Architecture

Page 38: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

Next Lecture• April 23th• C Programming• Practical Session April 17th

Ek KTH

Low Level Programming and Computer Architecture

Page 39: Low Level Programming and Computer Architecture - Lecture 5 · Low Level Programming and Computer Architecture Lecture 5 ... difference between knowing the ... Low Level Programming

e.o.f.

Ek KTH

Low Level Programming and Computer Architecture