Computer Systems A Programmers Perspec8ve Have a - - PowerPoint PPT Presentation

computer systems a programmer s perspec8ve have a tour of
SMART_READER_LITE
LIVE PREVIEW

Computer Systems A Programmers Perspec8ve Have a - - PowerPoint PPT Presentation

Whats next Computer Systems A Programmers Perspec8ve Have a tour of computer system at first... Chapter 1 1 Computer System Runs


slide-1
SLIDE 1

What’s ¡next… ¡

¡

Computer ¡Systems… ¡ ¡ A ¡Programmer’s ¡Perspec8ve ¡ ¡ Have ¡a ¡tour ¡of ¡computer ¡system ¡at ¡first... ¡ Chapter ¡1 ¡

1 ¡

slide-2
SLIDE 2

Computer ¡System ¡

2 ¡

SOFTWARE ¡

HARDWARE ¡

Opera8ng ¡ System ¡

} ¡

RISC ¡vs ¡CISC ¡ LOAD/STORE ¡ ADDRESS ¡ BUS ¡ DATA ¡ BUS ¡ ADDRESSIBILITY ¡ ALIGNMENT ¡ ISA ¡ BIG/LITTLE ¡ ENDIAN ¡ ETC ¡ PIPELINING ¡

Runs ¡the ¡soKware ¡and ¡ manages ¡the ¡hardware ¡

slide-3
SLIDE 3

Opera8ng ¡System ¡

3 ¡

slide-4
SLIDE 4

The ¡role ¡of ¡the ¡opera8ng ¡system ¡

  • Protect ¡the ¡computer ¡from ¡misuse ¡
  • Provide ¡an ¡abstrac<on ¡for ¡using ¡the ¡hardware ¡

so ¡that ¡programs ¡can ¡be ¡wri?en ¡for ¡a ¡variety ¡

  • f ¡different ¡hardware ¡
  • Manage ¡the ¡resources ¡to ¡allow ¡for ¡reasonable ¡

use ¡by ¡all ¡users ¡and ¡programs ¡on ¡a ¡computer ¡

4 ¡

slide-5
SLIDE 5

The ¡UNIX ¡Opera8ng ¡System ¡

  • Developed ¡in ¡1970s ¡at ¡Bell ¡Labs ¡
  • Kernel ¡wri?en ¡in ¡C, ¡also ¡developed ¡at ¡the ¡

same ¡<me ¡

– C ¡was ¡developed ¡for ¡the ¡purpose ¡of ¡wri<ng ¡UNIX ¡ and ¡systems ¡programming ¡

  • We ¡are ¡using ¡a ¡variant ¡of ¡UNIX ¡named ¡Linux ¡

– Other ¡UNIX ¡variants ¡exist, ¡such ¡as ¡Solaris, ¡and ¡the ¡ various ¡BSDs ¡(OpenBSD, ¡NetBSD, ¡FreeBSD, ¡OSX) ¡

5 ¡

slide-6
SLIDE 6

Linux ¡-­‑ ¡OS ¡

6 ¡

h?ps://www.explainxkcd.com/wiki/index.php/456:_Cau<onary ¡

slide-7
SLIDE 7

SoKware ¡

7 ¡

slide-8
SLIDE 8

Text/Ascii ¡

  • A ¡file ¡is ¡a ¡sequence ¡of ¡bytes ¡-­‑ ¡not ¡a ¡magical ¡

container ¡holding ¡the ¡bytes, ¡but ¡the ¡bytes ¡ themselves ¡

  • How ¡this ¡informa<on ¡is ¡treated ¡depends ¡on ¡the ¡

context ¡

– the ¡same ¡sequence ¡of ¡bits ¡can ¡be ¡used ¡to ¡represent ¡a ¡ character, ¡or ¡an ¡integer, ¡or ¡a ¡floa<ng-­‑point ¡number, ¡

  • r ¡an ¡instruc<on, ¡or... ¡
  • It's ¡all ¡a ¡ma?er ¡of ¡interpreta<on ¡
  • % ¡emacs ¡hellob.c ¡& ¡

8 ¡

slide-9
SLIDE 9

Why ¡assembly ¡language? ¡

  • Instruc<on ¡based ¡execu<on ¡

– Each ¡program ¡on ¡a ¡computer ¡is ¡a ¡sequence ¡of ¡ instruc<ons ¡wri?en ¡in ¡machine ¡language ¡ – Processor ¡executes ¡one ¡instruc<on ¡at ¡a ¡<me ¡in ¡a ¡ program, ¡then ¡executes ¡the ¡next ¡one ¡in ¡turn ¡ ¡ – To ¡study ¡code ¡in ¡this ¡form, ¡it's ¡helpful ¡to ¡use ¡ assembly ¡language ¡rather ¡than ¡machine ¡language ¡ code ¡

  • gcc ¡–S ¡hellob.c ¡

9 ¡

slide-10
SLIDE 10

Assembly ¡language… ¡really?! ¡

  • Chances ¡are, ¡you’ll ¡never ¡write ¡programs ¡in ¡assembly ¡

– Compilers ¡are ¡much ¡be?er ¡& ¡more ¡pa<ent ¡than ¡you ¡are ¡

  • But: ¡Understanding ¡assembly ¡is ¡key ¡to ¡machine-­‑level ¡

execu<on ¡model ¡

– Behavior ¡of ¡programs ¡in ¡presence ¡of ¡bugs ¡

  • High-­‑level ¡language ¡models ¡break ¡down ¡

– Tuning ¡program ¡performance ¡

  • Understand ¡op<miza<ons ¡done/not-­‑done ¡by ¡the ¡compiler ¡
  • Understanding ¡sources ¡of ¡program ¡inefficiency ¡

– Implemen<ng ¡system ¡sokware ¡

  • Compiler ¡has ¡machine ¡code ¡as ¡target ¡
  • Opera<ng ¡systems ¡must ¡manage ¡process ¡state ¡

– Crea<ng ¡/ ¡figh<ng ¡malware ¡

  • x86 ¡assembly ¡is ¡the ¡language ¡of ¡choice! ¡

10 ¡

slide-11
SLIDE 11

The ¡compila8on ¡system… ¡revisited ¡

11 ¡

Type ¡in ¡program ¡using ¡an ¡editor ¡of ¡ your ¡choice ¡(file.c); ¡plain ¡text ¡ .c ¡+ ¡.h ¡= ¡.i ¡which ¡is ¡the ¡“ul<mate ¡source ¡ code”? ¡i.e. ¡# ¡includes ¡expanded ¡and ¡ #defines ¡replaced ¡ .i ¡à ¡.s ¡ ¡which ¡is ¡assembler ¡source ¡code ¡ .s ¡à ¡.o ¡which ¡is ¡an ¡object ¡file; ¡fragments ¡of ¡ machine ¡code ¡with ¡unresolved ¡symbols ¡i.e. ¡ some ¡addresses ¡not ¡yet ¡known ¡(vars/subrs). ¡ ¡ .o ¡+ ¡library ¡links ¡à ¡a.out ¡(default ¡name); ¡ resolves ¡symbols, ¡generates ¡an ¡

  • executable. ¡

hello.c ¡ hello ¡

%gcc ¡ ¡-­‑o ¡ ¡hello ¡ ¡hello.c ¡

%hello ¡

slide-12
SLIDE 12

Declara8ons ¡and ¡the ¡Preprocessor ¡

  • #include ¡

– provides ¡ability ¡to ¡include ¡declara<ons ¡from ¡other ¡files ¡

  • usually ¡have ¡the ¡file ¡name ¡extension ¡.h ¡

– generally ¡only ¡include ¡func<on ¡prototypes, ¡and ¡type ¡and ¡ variable ¡declara<ons ¡ – actual ¡code ¡is ¡kept ¡in ¡a ¡different ¡file, ¡usually ¡with ¡the ¡ extension ¡.c ¡

  • files ¡with ¡code ¡are ¡compiled ¡individually ¡to ¡object ¡code ¡and ¡then ¡

linked ¡together ¡to ¡create ¡a ¡file ¡with ¡executable ¡code ¡

  • Two ¡ways ¡to ¡use ¡#include ¡

– #include ¡<stdio.h> ¡for ¡standard ¡system ¡files ¡ – #include ¡"swap.h“ ¡for ¡user-­‑wri?en ¡files ¡

  • Very ¡rarely/never ¡#include ¡a ¡.c ¡file ¡(link ¡.c ¡files) ¡

12 ¡

slide-13
SLIDE 13

Compila8on ¡stages ¡

  • Preprocessor ¡

– used ¡to ¡make ¡sure ¡the ¡program ¡parts ¡see ¡declara<ons ¡they ¡need ¡(and ¡other ¡ purposes ¡too, ¡e.g., ¡macros) ¡ – direc<ves ¡begin ¡with ¡a ¡# ¡(pound ¡sign) ¡ – do ¡not ¡end ¡in ¡a ¡; ¡like ¡C ¡statements ¡do ¡

  • Transla<on ¡

– makes ¡sure ¡individual ¡parts ¡are ¡consistent ¡within ¡themselves ¡ – creates ¡an ¡object ¡(.o) ¡file ¡

  • Linking ¡

– joins ¡one ¡or ¡more ¡compiled ¡object ¡files ¡together ¡

  • includes ¡matching ¡func<on ¡call ¡to ¡callee ¡across ¡separate ¡files ¡
  • and ¡matching ¡global ¡variable ¡use ¡to ¡where ¡it’s ¡defined ¡

– makes ¡sure ¡caller/callee ¡consistent ¡with ¡each ¡other ¡ – creates ¡an ¡executable ¡file ¡(the ¡conven<on ¡in ¡Unix ¡is ¡no ¡filename ¡extension ¡is ¡ used ¡for ¡executable ¡files, ¡although ¡some<mes ¡.x ¡or ¡.exe ¡are ¡used) ¡

13 ¡

slide-14
SLIDE 14

Compiling ¡a ¡C ¡program ¡

  • C ¡programs ¡must ¡be ¡compiled ¡to ¡be ¡executed ¡
  • Use ¡the ¡gcc ¡program ¡to ¡build ¡your ¡programs ¡

– invoca<on: ¡gcc ¡op<ons ¡source-­‑files ¡ – common ¡op<ons ¡

  • -­‑ansi ¡enforces ¡the ¡ansi ¡standards ¡
  • -­‑g ¡enable ¡debugging ¡
  • -­‑Wall ¡warn ¡about ¡common ¡things ¡that ¡may ¡be ¡problems ¡
  • -­‑Werror ¡treat ¡warnings ¡as ¡errors ¡
  • -­‑o ¡filename ¡place ¡output ¡in ¡filename ¡
  • -­‑c ¡only ¡compile ¡to ¡object ¡file, ¡don't ¡link ¡(.o ¡file ¡created) ¡
  • -­‑S ¡generate ¡assembly ¡code, ¡don’t ¡link ¡(.s ¡file ¡created) ¡

– a ¡very ¡simple ¡compila<on ¡command ¡

  • gcc ¡file.c ¡

– a ¡simple ¡compila<on ¡command ¡

  • gcc ¡-­‑g ¡-­‑Wall ¡–Werror ¡–ansi ¡-­‑o ¡prog1 ¡prog1.c ¡tools.c ¡

– executable ¡is ¡run ¡in ¡UNIX ¡by ¡just ¡typing ¡its ¡name ¡(some<mes ¡preceded ¡by ¡"./", ¡ like ¡this: ¡./prog1) ¡

14 ¡

slide-15
SLIDE 15

A ¡look ¡ahead ¡to ¡Chp. ¡3 ¡ ¡

  • Disassembler ¡

– A ¡tool ¡that ¡determines ¡the ¡instruc<on ¡sequence ¡represented ¡by ¡an ¡ executable ¡program ¡file ¡ – Unix ¡command ¡

  • gcc ¡–o ¡hellob ¡hellob.c ¡
  • objdump ¡–D ¡–t ¡–s ¡hellob ¡

– -­‑d, ¡-­‑-­‑disassemble ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  • Display ¡assembler ¡contents ¡of ¡executable ¡sec<ons ¡

– -­‑D, ¡-­‑-­‑disassemble-­‑all ¡ ¡ ¡ ¡ ¡

  • Display ¡assembler ¡contents ¡of ¡all ¡sec<ons ¡

– -­‑S, ¡-­‑-­‑source ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  • Intermix ¡source ¡code ¡with ¡disassembly ¡

– -­‑s, ¡-­‑-­‑full-­‑contents ¡ ¡ ¡ ¡ ¡ ¡ ¡

  • Display ¡the ¡full ¡contents ¡of ¡all ¡sec<ons ¡requested ¡

– -­‑t, ¡-­‑-­‑syms ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  • Display ¡the ¡contents ¡of ¡the ¡symbol ¡table(s) ¡ ¡ ¡

– -­‑T, ¡-­‑-­‑dynamic-­‑syms ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡

  • Display ¡the ¡contents ¡of ¡the ¡dynamic ¡symbol ¡table ¡

15 ¡

slide-16
SLIDE 16

Hardware ¡

16 ¡

slide-17
SLIDE 17

Hardware ¡Organiza8on ¡(big ¡picture) ¡

17 ¡

PC ¡ ALU ¡ Bus ¡Interface ¡ I/O ¡ ¡ bridge ¡ Main ¡ memory ¡ Disk ¡ Controller ¡ USB ¡ controller ¡ Graphics ¡ adapter ¡ Disk* ¡

Display ¡ Keyboard ¡

Expansion ¡slots ¡for ¡

  • ther ¡devices ¡such ¡as ¡

network ¡adapters, ¡ video ¡cards, ¡etc. ¡ Memory ¡ bus ¡ System ¡ bus ¡

Register ¡File ¡

I/O ¡bus ¡

hellob ¡executable ¡ stored ¡on ¡disk* ¡

Mouse ¡

slide-18
SLIDE 18

HW ¡organiza8on ¡details ¡

  • Processor ¡(CPU) ¡

– Interprets/executes ¡instruc<ons ¡stored ¡in ¡main ¡memory ¡ – Updates ¡the ¡PC ¡to ¡point ¡to ¡the ¡next ¡instruc<on ¡ – PC ¡ ¡

  • points ¡at ¡(contains ¡the ¡address ¡of) ¡some ¡machine-­‑language ¡instruc<on ¡in ¡main ¡

memory ¡

– ALU ¡

  • Computes ¡new ¡data ¡and ¡address ¡values ¡

– Register ¡file ¡

  • Small ¡storage ¡device ¡that ¡consists ¡of ¡a ¡collec<on ¡of ¡word-­‑sized ¡registers, ¡each ¡

with ¡their ¡own ¡name ¡

– ISA ¡– ¡instruc<on ¡set ¡architecture ¡defines ¡

  • The ¡processor ¡state ¡
  • The ¡format ¡of ¡the ¡instruc<ons ¡
  • The ¡effect ¡each ¡instruc<on ¡will ¡have ¡on ¡the ¡state ¡
  • Instruc<ons: ¡

– h?p://www.c-­‑jump.com/CIS77/reference/Instruc<ons_by_Mnemonic.html ¡

18 ¡

slide-19
SLIDE 19

HW ¡organiza8on ¡details ¡(cont.) ¡

  • I/O ¡Devices ¡

– System’s ¡connec<on ¡to ¡the ¡external ¡world ¡ – Transfers ¡informa<on ¡back ¡and ¡forth ¡between ¡the ¡I/O ¡bus ¡and ¡an ¡I/O ¡ device ¡

  • Main ¡Memory ¡

– Temporary ¡storage ¡ – Holds ¡both ¡the ¡program ¡and ¡the ¡data ¡it ¡manipulates ¡ ¡

  • Von ¡Neumann ¡architecture ¡

– Is ¡organized ¡as ¡a ¡linear ¡array ¡of ¡bytes ¡each ¡with ¡its ¡own ¡unique ¡ address ¡star<ng ¡at ¡zero ¡

  • Bus ¡

– Transfers ¡one ¡“word” ¡at ¡a ¡<me ¡

  • Fundamental ¡system ¡parameter ¡
  • Amount ¡can ¡fetch ¡from ¡memory ¡at ¡one ¡<me ¡
  • Tends ¡to ¡be ¡the ¡size ¡of ¡the ¡data ¡bus ¡

19 ¡

slide-20
SLIDE 20

Memory ¡Hierarchy ¡(chp. ¡6) ¡

20 ¡ 2/14/17 ¡

slide-21
SLIDE 21

Target ¡of ¡Memory ¡Hierarchy ¡Op8miza8ons ¡

21 ¡ 2/14/17 ¡

  • Reduce ¡memory ¡latency ¡

– The ¡latency ¡of ¡a ¡memory ¡access ¡is ¡the ¡<me ¡(usually ¡ in ¡cycles) ¡between ¡a ¡memory ¡request ¡and ¡its ¡ comple<on ¡

  • Maximize ¡memory ¡bandwidth ¡

– Bandwidth ¡is ¡the ¡amount ¡of ¡useful ¡data ¡that ¡can ¡be ¡ retrieved ¡over ¡a ¡<me ¡interval ¡

  • Manage ¡overhead ¡

– Cost ¡of ¡performing ¡op<miza<on ¡(e.g., ¡copying) ¡ should ¡be ¡less ¡than ¡an<cipated ¡gain ¡

slide-22
SLIDE 22

Abstrac8on ¡

  • Provided ¡by ¡the ¡OS ¡

– Process ¡(chp. ¡8) ¡

  • The ¡running ¡of ¡a ¡program ¡done ¡by ¡the ¡processor ¡
  • Threads ¡= ¡mul<ple ¡execu<on ¡units ¡
  • Includes ¡memory ¡and ¡I/O ¡device ¡(i.e. ¡files ¡abstrac<on) ¡ ¡

– Virtual ¡Memory ¡(chp. ¡9) ¡

  • Provides ¡each ¡process ¡with ¡the ¡illusion ¡that ¡is ¡has ¡exclusive ¡use ¡of ¡

the ¡main ¡memory ¡

  • Program ¡code ¡and ¡data ¡ ¡

– Includes ¡files ¡ – Begins ¡at ¡same ¡fixed ¡address ¡for ¡all ¡processes ¡ – Address ¡space ¡(chp. ¡7) ¡

– Files ¡(chp. ¡10) ¡

  • Sequence ¡of ¡bytes ¡

22 ¡

slide-23
SLIDE 23

Address ¡Space… ¡a ¡quick ¡look ¡

23 ¡

ADDRESS ¡SPACE ¡ Decrip8on/info ¡ Kernel ¡virtual ¡memory ¡ Memory ¡invisible ¡to ¡user ¡code ¡ User ¡stack ¡(created ¡at ¡run ¡<me) ¡ Implements ¡func<on ¡calls ¡ Memory ¡mapped ¡region ¡for ¡ shared ¡libraries ¡

  • Ex. ¡prinz ¡func<on ¡

Run-­‑<me ¡heap ¡ (created ¡at ¡run ¡<me ¡by ¡malloc/ calloc) ¡ Dynamic ¡in ¡size ¡ Read/write ¡data ¡ Program ¡(executable ¡file) ¡ Read-­‑only ¡code ¡and ¡data ¡ Fixed ¡size ¡

} ¡

32/64 ¡bit ¡ star8ng ¡ address ¡

Address ¡0 ¡ No<ce ¡symbolically ¡drawn ¡with ¡memory ¡“star<ng” ¡at ¡the ¡bo?om ¡

  • An ¡array ¡of ¡

8-­‑bit ¡bytes ¡

  • A ¡pointer ¡is ¡

just ¡an ¡ index ¡into ¡ this ¡array ¡ ¡

¡

slide-24
SLIDE 24

What ¡is ¡a ¡system? ¡

“A ¡collec8on ¡of ¡intertwined ¡hardware ¡and ¡ systems ¡soKware ¡that ¡must ¡cooperate ¡in ¡order ¡ to ¡achieve ¡the ¡ul8mate ¡goal ¡of ¡running ¡ applica8on ¡programs” ¡

24 ¡

slide-25
SLIDE 25

Informa8on ¡Representa8on ¡and ¡ Interpreta8on ¡ Chapter ¡2 ¡

25 ¡

slide-26
SLIDE 26

Are ¡you ¡sure? ¡

26 ¡

Let’s ¡ check… ¡

* ¡see ¡sizeck.c ¡ * ¡try ¡–m32 ¡op8on ¡ ANSI ¡rules ¡ Variables ¡of ¡type ¡char ¡are ¡guaranteed ¡to ¡always ¡be ¡one ¡byte. ¡ There ¡is ¡no ¡maximum ¡size ¡for ¡a ¡type, ¡but ¡the ¡following ¡ rela<onships ¡must ¡hold: ¡ § sizeof(short) ¡<= ¡sizeof(int) ¡<= ¡sizeof(long) ¡ § sizeof(float) ¡<= ¡sizeof(double) ¡<= ¡sizeof(long ¡double) ¡

8 ¡

slide-27
SLIDE 27

Number ¡of ¡values ¡(vs ¡range ¡of ¡values) ¡ ¡

  • Every ¡computer ¡has ¡a ¡

“word ¡size” ¡

– Nominal ¡size ¡of ¡integer ¡ and ¡pointer ¡data ¡

  • Address ¡space ¡depends ¡
  • n ¡word ¡size ¡à ¡2word-­‑size-­‑

in-­‑#bits ¡

– Is ¡it ¡big ¡enough? ¡ ¡

  • 64-­‑bit ¡high-­‑end ¡machines ¡

becoming ¡more ¡prevalent ¡

  • Portability ¡issues ¡– ¡

insensi<ve ¡to ¡sizes ¡of ¡ different ¡data ¡types ¡

27 ¡

# ¡bytes ¡ # ¡bits ¡ # ¡of ¡values ¡ (2#bits) ¡

low ¡ high ¡

1 ¡ 8 ¡ 256 ¡

¡ ¡ ¡ ¡

2 ¡ 16 ¡ 65536 ¡

¡ ¡ ¡ ¡

3 ¡ 24 ¡ 16777216 ¡

¡ ¡ ¡ ¡

4 ¡ 32 ¡ 4294967296 ¡

¡ ¡ ¡ ¡

5 ¡ 40 ¡ 1.09951E+12 ¡

¡ ¡ ¡ ¡

6 ¡ 48 ¡ 2.81475E+14 ¡

¡ ¡ ¡ ¡

7 ¡ 56 ¡ 7.20576E+16 ¡

¡ ¡ ¡ ¡

8 ¡ 64 ¡ 1.84467E+19 ¡

¡ ¡ ¡ ¡

9 ¡ 72 ¡ 4.72237E+21 ¡

¡ ¡ ¡ ¡

10 ¡ 80 ¡ 1.20893E+24 ¡

¡ ¡ ¡ ¡

11 ¡ 88 ¡ 3.09485E+26 ¡

¡ ¡ ¡ ¡

12 ¡ 96 ¡ 7.92282E+28 ¡

¡ ¡ ¡ ¡

13 ¡ 104 ¡ 2.02824E+31 ¡

¡ ¡ ¡ ¡

14 ¡ 112 ¡ 5.1923E+33 ¡

¡ ¡ ¡ ¡

15 ¡ 120 ¡ 1.32923E+36 ¡

¡ ¡ ¡ ¡

16 ¡ 128 ¡ 3.40282E+38 ¡

¡ ¡ ¡ ¡

slide-28
SLIDE 28

Outline ¡

  • General ¡introduc<on ¡
  • Hexadecimal ¡and ¡other ¡nota<ons ¡
  • Addressing ¡and ¡byte ¡order ¡ ¡

28 ¡

slide-29
SLIDE 29

Interpreta8on ¡& ¡Representa8on ¡

  • What ¡does ¡this ¡mean? ¡

– abc ¡ – 123 ¡ – 3.14 ¡ – 0x61 ¡

  • Representa<on ¡(encode?) ¡

– ASCII ¡ – Simple ¡Binary* ¡ – One’s ¡complement ¡ – Two’s ¡complement* ¡ – Binary ¡Coded ¡Decimal ¡ – Floa<ng-­‑point* ¡

29 ¡

Limited ¡number ¡of ¡bits ¡to ¡ encode ¡a ¡value ¡

¡

Will ¡there ¡be ¡a ¡8me ¡that ¡ the ¡value ¡we ¡want ¡to ¡ encode ¡does ¡not ¡fit? ¡Yes! ¡ OVERFLOW ¡

¡

Need ¡to ¡be ¡aware ¡of ¡the ¡ range ¡of ¡values ¡that ¡each ¡ limited ¡number ¡of ¡bits ¡will ¡ hold ¡

¡

Inaccuracies ¡exist… ¡ ¡

(see ¡overflw.c) ¡

slide-30
SLIDE 30

Floa8ng ¡point ¡

  • Google ¡ ¡

– “what ¡every ¡computer ¡scien<st ¡should ¡know ¡about ¡ floa<ng ¡point” ¡

  • Squeezing ¡infinitely ¡many ¡real ¡numbers ¡into ¡a ¡finite ¡number ¡
  • f ¡bits ¡requires ¡an ¡approximate ¡representa<on ¡(rounding) ¡
  • Overflow ¡à ¡+ ¡
  • Not ¡associa<ve ¡

– Due ¡to ¡finite ¡precision ¡of ¡the ¡representa<on ¡

  • A ¡float ¡has ¡roughly ¡seven ¡decimal ¡digits ¡of ¡precision ¡
  • see ¡floatpt.c ¡

30 ¡

slide-31
SLIDE 31

Informa8on ¡Storage ¡(general) ¡

  • The ¡machine ¡level ¡program ¡generated ¡has ¡no ¡informa<on ¡

about ¡data ¡types ¡

– It’s ¡the ¡C ¡compiler ¡that ¡maintains ¡this ¡type ¡of ¡informa<on ¡

  • The ¡different ¡mathema<cal ¡proper<es ¡of ¡integers ¡vs ¡

floa<ng-­‑point ¡arithme<c ¡stem ¡from ¡the ¡difference ¡in ¡how ¡ they ¡handle ¡the ¡finite-­‑ness ¡of ¡their ¡representa<ons ¡

– Integers ¡– ¡smaller ¡values ¡but ¡more ¡precise ¡ – Real ¡– ¡wide ¡range ¡of ¡values, ¡but ¡only ¡approximately ¡

  • Defines ¡ranges ¡of ¡values ¡
  • Computer ¡security ¡vulnerabili<es ¡
  • Need ¡to ¡know/understand ¡before ¡progress ¡to ¡machine ¡

level ¡programming ¡(chp.3) ¡

31 ¡

slide-32
SLIDE 32

Informa8on ¡Storage ¡(details) ¡

  • Byte ¡= ¡smallest ¡addressable ¡unit ¡of ¡memory ¡
  • Virtual ¡memory ¡= ¡very ¡large ¡array ¡of ¡bytes ¡
  • Address ¡= ¡how ¡byte ¡of ¡memory ¡is ¡uniquely ¡

iden<fied ¡

  • Virtual ¡address ¡space ¡= ¡the ¡set ¡of ¡all ¡possible ¡

addresses ¡ Reminder: ¡no ¡data ¡typing ¡at ¡this ¡level ¡

32 ¡

slide-33
SLIDE 33

Hexadecimal ¡Nota8on ¡(Hex) ¡

  • Base ¡16 ¡
  • Useful ¡in ¡describing ¡bit ¡pa?erns ¡
  • Digits ¡0-­‑9 ¡and ¡A-­‑F ¡ ¡
  • In ¡C ¡

– 0x ¡or ¡0X ¡prefix ¡interpreted ¡as ¡hex ¡ value ¡ – Not ¡case ¡sensi<ve ¡ – Example ¡

  • FA1D37B16 ¡-­‑-­‑> ¡0Xfa1d37b, ¡

0xFA1D37B, ¡0xfA1d37B ¡

  • Easy ¡to ¡convert ¡to/from ¡hex, ¡
  • ctal ¡and ¡binary ¡

33 ¡

DEC HEX Notes 1 1 2^0 2 2 2^1 3 3 4 4 2^2 5 5 6 6 7 7 8 8 2^3 9 9 10 A 11 B 12 C 13 D 14 E 15 F

slide-34
SLIDE 34

Binary ¡to ¡Hex ¡to ¡Octal ¡

  • FYI: ¡“bit” ¡stands ¡for ¡“binary ¡

digit” ¡

  • Fact: ¡24 ¡= ¡16 ¡and ¡23 ¡= ¡8 ¡

– The ¡power ¡is ¡the ¡# ¡of ¡bits ¡per ¡ hex/octal ¡digit ¡

  • Binary ¡to ¡Hex ¡

– Every ¡4 ¡bits ¡= ¡1 ¡hex ¡digit ¡

  • Octal ¡– ¡base ¡8 ¡

– Digits ¡0-­‑7 ¡

  • Binary ¡to ¡Octal ¡

– Every ¡3 ¡bits ¡= ¡1 ¡octal ¡digit ¡

  • Example: ¡FA1D37B16 ¡

34 ¡

DEC OCT HEX BIN Notes

  • 1

1 1 1 20 2 2 2 10 21 3 3 3 11 4 4 4 100 22 5 5 5 101 6 6 6 110 7 7 7 111 8 10 8 1000 23 9 11 9 1001 10 12 A 1010 11 13 B 1011 12 14 C 1100 13 15 D 1101 14 16 E 1110 15 17 F 1111

slide-35
SLIDE 35

When ¡value ¡x10 ¡is ¡a ¡power ¡of ¡2 ¡

  • x ¡= ¡2n ¡for ¡some ¡non-­‑nega<ve ¡integer ¡n, ¡then ¡if ¡

– x ¡= ¡1610 ¡= ¡24 ¡= ¡100002 ¡= ¡1016 ¡

  • Binary ¡rep ¡of ¡x ¡à ¡1 ¡followed ¡by ¡n ¡zeroes ¡
  • Hex ¡rep ¡of ¡x ¡is ¡à ¡n ¡= ¡i ¡+ ¡4j ¡

– Reminder: ¡hex ¡digit ¡0 ¡in ¡binary ¡is ¡0000 ¡ – Leading ¡hex ¡digit ¡where ¡0 ¡<= ¡i ¡<=3 ¡

  • 1 ¡(i=0) ¡
  • 2 ¡(i=1) ¡
  • 4 ¡(i=2) ¡
  • 8 ¡(i=3) ¡

– Followed ¡by ¡j ¡hex ¡0s ¡ – Examples: ¡ ¡

  • n=9 ¡à ¡so ¡i ¡= ¡1 ¡and ¡j ¡= ¡2 ¡à ¡x ¡= ¡(2)(00) ¡hex ¡i.e. ¡x ¡= ¡20016 ¡
  • n=6 ¡à ¡so ¡i ¡= ¡2 ¡and ¡j ¡= ¡1 ¡à ¡x ¡= ¡(4)(0) ¡hex ¡i.e. ¡x ¡= ¡4016 ¡

35 ¡

slide-36
SLIDE 36

Convert ¡to ¡Decimal ¡(why?) ¡

  • Base ¡ten ¡(decimal): ¡digits ¡0-­‑9 ¡

– E.g., ¡31610 =

  • Base ¡eight ¡(octal): ¡digits ¡0-­‑7 ¡

– E.g., ¡4748 =

  • Base ¡16 ¡(hexadecimal): ¡digits ¡0-­‑9 ¡and ¡A-­‑F. ¡

– 13C16 =

¡

  • Base ¡2 ¡(binary): ¡digits ¡0, ¡1 ¡ ¡

– 1001102 =

  • In ¡general, ¡radix ¡r ¡representa<ons ¡use ¡the ¡first ¡r ¡chars ¡in ¡{0…9, ¡A...Z} ¡and ¡

have ¡the ¡form ¡dn-­‑1dn-­‑2…d1d0. ¡ ¡

– Summing ¡dn-­‑1 ¡×rn-­‑1 ¡+ ¡dn-­‑2×rn-­‑2 ¡+ ¡… ¡+ ¡d0×r0 ¡converts ¡to ¡base ¡10. ¡ ¡ ¡

36 ¡

slide-37
SLIDE 37

Every ¡base ¡is ¡base ¡10 ¡

37 ¡

http://cowbirdsinlove.com/43

EXPLANATION ¡ In ¡general, ¡10X ¡= ¡X10 ¡ ¡ ¡ ¡ ¡ ¡102 ¡= ¡2 ¡ ¡ ¡ ¡ ¡ ¡103 ¡= ¡3 ¡ ¡ ¡ ¡ ¡ ¡104 ¡= ¡4 ¡ ¡ ¡ ¡ ¡ ¡105 ¡= ¡5 ¡ ¡ ¡ ¡ ¡ ¡106 ¡= ¡6 ¡ ¡ ¡ ¡ ¡ ¡107 ¡= ¡7 ¡ ¡ ¡ ¡ ¡ ¡108 ¡= ¡8 ¡ ¡ ¡ ¡ ¡ ¡109 ¡= ¡9 ¡ ¡ ¡ ¡ ¡ ¡1010 ¡= ¡10 ¡

slide-38
SLIDE 38

Base ¡Conversions ¡

  • Base ¡Conversions ¡

– Convert ¡to ¡base ¡10 ¡by ¡mul1plica1on ¡of ¡powers ¡

  • 100125 = ( )10

¡

– Convert ¡from ¡base ¡10 ¡by ¡repeated ¡division ¡

  • 63210 = ( )8

¡ ¡

– Conver<ng ¡base ¡x ¡to ¡base ¡y: ¡convert ¡base ¡x ¡to ¡base ¡10 ¡ then ¡convert ¡base ¡10 ¡to ¡base ¡y ¡

38 ¡

slide-39
SLIDE 39

More ¡prac8ce ¡

  • Convert ¡from ¡base ¡10 ¡

– 12310 ¡= ¡( ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡)3 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡and ¡check ¡ – 123410 ¡= ¡( ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡)16 ¡ ¡ ¡ ¡ ¡ ¡ ¡and ¡check ¡ – Another ¡way ¡from ¡decimal ¡to ¡base ¡n ¡

  • From ¡LEFT ¡TO ¡RIGHT, ¡ask ¡“how ¡many” ¡and ¡subtract ¡
  • (219)10 ¡= ¡( ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡)2 ¡= ¡( ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡)16 ¡

39 ¡

n8 ¡ n7 ¡ n6 ¡ n5 ¡ n4 ¡ n3 ¡ n2 ¡ n1 ¡ n0 ¡ 256 ¡ 128 ¡ 64 ¡ 32 ¡ 16 ¡ 8 ¡ 4 ¡ 2 ¡ 1 ¡

for ¡n ¡= ¡2 ¡

slide-40
SLIDE 40

Hex ¡and ¡Binary ¡addi8on/subtrac8on ¡

  • Hex ¡add ¡first, ¡then ¡convert ¡hex ¡to ¡binary ¡and ¡add ¡
¡

– A ¡+ ¡8 ¡= ¡ ¡ – 13 ¡+ ¡F ¡= ¡ ¡ – BEAD ¡+ ¡4321 ¡= ¡ ¡

  • Subtract ¡in ¡hex ¡first, ¡then ¡convert ¡each ¡value ¡to ¡binary ¡and ¡subtract ¡

– 5CD2 ¡– ¡2A0 ¡= ¡ ¡ – 3145 ¡– ¡1976 ¡= ¡ ¡ – A8D2 ¡– ¡3DAC ¡= ¡ ¡ à carry/borrow ¡16 ¡each ¡<me, ¡since ¡the ¡next ¡place ¡is ¡16 ¡<mes ¡as ¡large ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡(see ¡prac<ce ¡problems) ¡

40 ¡

slide-41
SLIDE 41

Addressing ¡and ¡byte ¡ordering ¡

  • Two ¡conven<ons ¡(for ¡mul<-­‑byte ¡objects)* ¡

– What ¡is ¡the ¡address ¡of ¡the ¡object? ¡ – What ¡is ¡the ¡order ¡of ¡the ¡bytes ¡in ¡memory? ¡

  • Typically ¡

– Mul<-­‑byte ¡objects ¡are ¡stored ¡con<guously ¡ – The ¡address ¡of ¡the ¡object ¡is ¡given ¡as ¡the ¡smallest ¡address ¡of ¡the ¡ bytes ¡used ¡

¡

41 ¡

xx ¡ xx ¡ xx ¡ xx ¡ Ø 4-­‑byte ¡integer ¡stored ¡as ¡hex ¡value ¡at ¡address ¡0x100 ¡ Ø So ¡&x ¡= ¡0x100, ¡and ¡ Ø The ¡4 ¡bytes ¡of ¡x ¡would ¡be ¡stored ¡at ¡memory ¡loca<ons ¡0x100, ¡0x101, ¡0x102, ¡0x103 ¡ * ¡Does ¡not ¡apply ¡to ¡characters ¡because ¡they ¡are ¡single ¡byte ¡values ¡

slide-42
SLIDE 42

Addressing ¡and ¡byte ¡ordering ¡(cont) ¡

  • Big ¡Endian ¡

– “The ¡big ¡end ¡goes ¡at ¡byte ¡zero” ¡ – “big ¡end” ¡means ¡the ¡most ¡significant ¡byte ¡of ¡the ¡given ¡value ¡

  • Li?le ¡Endian ¡

– “The ¡li?le ¡end ¡goes ¡at ¡byte ¡zero” ¡ – “li?le ¡end” ¡means ¡the ¡least ¡significant ¡byte ¡of ¡the ¡given ¡value ¡

  • “Byte ¡zero” ¡means ¡the ¡smallest ¡address ¡used ¡to ¡store ¡the ¡given ¡value ¡
  • Example: ¡hex/given ¡value ¡is ¡0x01234567 ¡

– What ¡is ¡the ¡big ¡end ¡of ¡the ¡given ¡value? ¡à ¡01 ¡ – What ¡is ¡the ¡li?le ¡end ¡of ¡the ¡given ¡value? ¡à ¡67 ¡ – What ¡is ¡the ¡lower ¡memory ¡address ¡i.e. ¡byte ¡zero? ¡0x100 ¡

42 ¡

Byte ¡order ¡ 0x100 ¡ 0x101 ¡ 0x102 ¡ 0x103 ¡ Big ¡Endian ¡ 01 ¡ 23 ¡ 45 ¡ 67 ¡ Livle ¡Endian ¡ 67 ¡ 45 ¡ 23 ¡ 01 ¡

slide-43
SLIDE 43

Big/Livle ¡Endian ¡one ¡li?le ¡two ¡li?le ¡three ¡li?le ¡endians ¡;o) ¡

Byte ¡

  • rder ¡

Big ¡ Endian ¡ Livle ¡ Endian ¡ 0x100 ¡ 01 ¡ 67 ¡ 0x101 ¡ 23 ¡ 45 ¡ 0x102 ¡ 45 ¡ 23 ¡ 0x103 ¡ 67 ¡ 01 ¡

43 ¡

Byte ¡

  • rder ¡

Big ¡ Endian ¡ Livle ¡ Endian ¡ 0x103 ¡ 67 ¡ 01 ¡ 0x102 ¡ 45 ¡ 23 ¡ 0x101 ¡ 23 ¡ 45 ¡ 0x100 ¡ 01 ¡ 67 ¡

There ¡is ¡no ¡technological ¡reason ¡to ¡choose ¡one ¡byte ¡

  • rdering ¡conven<on ¡over ¡the ¡other ¡

Need ¡to ¡choose ¡a ¡conven<on ¡and ¡be ¡consistent ¡ Typically ¡invisible ¡to ¡most ¡applica<on ¡programmers ¡as ¡ results ¡are ¡iden<cal ¡

What ¡if ¡transferring ¡data, ¡though? ¡ Need ¡to ¡know ¡when ¡looking ¡at ¡integer ¡data ¡in ¡memory ¡

¡

slide-44
SLIDE 44

X86 ¡is ¡livle ¡endian ¡

  • Largely, ¡for ¡the ¡same ¡reason ¡you ¡start ¡at ¡the ¡least ¡significant ¡digit ¡(the ¡

right ¡end) ¡when ¡you ¡add—because ¡carries ¡propagate ¡toward ¡the ¡more ¡ significant ¡digits. ¡Puƒng ¡the ¡least ¡significant ¡byte ¡first ¡allows ¡the ¡ processor ¡to ¡get ¡started ¡on ¡the ¡add ¡aker ¡having ¡read ¡only ¡the ¡first ¡byte ¡of ¡ an ¡offset. ¡

  • Aker ¡you've ¡done ¡enough ¡assembly ¡coding ¡and ¡debugging ¡you ¡may ¡come ¡

to ¡the ¡conclusion ¡that ¡it's ¡not ¡li?le ¡endian ¡that's ¡the ¡strange ¡choice—it's ¡

  • dd ¡that ¡we ¡humans ¡use ¡big ¡endian. ¡
  • A ¡side ¡note: ¡Humans ¡mostly ¡read ¡numbers ¡and ¡only ¡some<mes ¡use ¡them ¡

for ¡calcula<on. ¡Furthermore ¡we ¡oken ¡don't ¡need ¡the ¡exact ¡numbers ¡when ¡ dealing ¡with ¡large ¡quan<<es ¡-­‑ ¡taking ¡that ¡into ¡account ¡-­‑ ¡big ¡endian ¡is ¡a ¡ sensible ¡choice ¡for ¡humans ¡

  • It ¡reflects ¡the ¡difference ¡between ¡considering ¡memory ¡to ¡always ¡be ¡
  • rganized ¡a ¡byte ¡at ¡a ¡<me ¡versus ¡considering ¡it ¡to ¡be ¡organized ¡a ¡unit ¡at ¡a ¡

<me, ¡where ¡the ¡size ¡of ¡the ¡unit ¡can ¡vary ¡(byte, ¡word, ¡dword, ¡etc.) ¡

44 ¡

slide-45
SLIDE 45

Endian-­‑ness ¡

  • When ¡is ¡byte ¡ordering ¡an ¡issue? ¡
  • 1. ¡Communica<ons ¡over ¡a ¡network ¡between ¡different ¡machines ¡
  • 2. ¡Representa<on ¡of ¡integer/real ¡numeric ¡data ¡

45 ¡

  • 3. ¡Circumven<on ¡of ¡normal ¡

type ¡system ¡

Ø Using ¡a ¡“cast” ¡to ¡allow ¡ an ¡object ¡to ¡be ¡ referenced ¡according ¡to ¡ a ¡different ¡data ¡type ¡ from ¡which ¡it ¡was ¡ created ¡

§ Use ¡and ¡even ¡necessary ¡ for ¡system-­‑level ¡ programming ¡

Ø Can ¡cast ¡such ¡that ¡the ¡ value ¡is ¡a ¡sequence ¡of ¡ bytes ¡rather ¡than ¡an ¡

  • bject ¡of ¡the ¡original ¡

data ¡type ¡

#include ¡<stdio.h> ¡ void ¡main() ¡ { ¡ ¡ ¡int ¡x ¡= ¡0x12345678, ¡i; ¡ ¡ ¡unsigned ¡char ¡*xptr ¡= ¡&x; ¡ ¡ ¡priny("the ¡integer ¡x ¡is ¡0x%x\n",x); ¡ ¡ ¡for ¡(i ¡= ¡0; ¡i ¡< ¡4; ¡i++) ¡ ¡ ¡ ¡ ¡priny("byte ¡%d ¡is ¡%.2x\n",i+1, ¡*(xptr+i)); ¡ } ¡ ¡ (castex.c) ¡

slide-46
SLIDE 46

Systems ¡can ¡have ¡different… ¡

  • word ¡size ¡
  • byte ¡sizes ¡for ¡each ¡type ¡
  • endian-­‑ness ¡(for ¡numeric ¡values) ¡
  • representa<on ¡of ¡pointers ¡
  • character ¡encoding ¡schemes ¡(ascii, ¡ebcdic, ¡

unicode) ¡

  • instruc<on ¡formats ¡(again, ¡just ¡a ¡sequence ¡of ¡

bytes) ¡

  • ETC ¡

46 ¡