What’s ¡next… ¡
¡
Computer ¡Systems… ¡ ¡ A ¡Programmer’s ¡Perspec8ve ¡ ¡ Have ¡a ¡tour ¡of ¡computer ¡system ¡at ¡first... ¡ Chapter ¡1 ¡
1 ¡
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
What’s ¡next… ¡
Computer ¡Systems… ¡ ¡ A ¡Programmer’s ¡Perspec8ve ¡ ¡ Have ¡a ¡tour ¡of ¡computer ¡system ¡at ¡first... ¡ Chapter ¡1 ¡
1 ¡
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 ¡
Opera8ng ¡System ¡
3 ¡
The ¡role ¡of ¡the ¡opera8ng ¡system ¡
so ¡that ¡programs ¡can ¡be ¡wri?en ¡for ¡a ¡variety ¡
use ¡by ¡all ¡users ¡and ¡programs ¡on ¡a ¡computer ¡
4 ¡
The ¡UNIX ¡Opera8ng ¡System ¡
same ¡<me ¡
– C ¡was ¡developed ¡for ¡the ¡purpose ¡of ¡wri<ng ¡UNIX ¡ and ¡systems ¡programming ¡
– Other ¡UNIX ¡variants ¡exist, ¡such ¡as ¡Solaris, ¡and ¡the ¡ various ¡BSDs ¡(OpenBSD, ¡NetBSD, ¡FreeBSD, ¡OSX) ¡
5 ¡
Linux ¡-‑ ¡OS ¡
6 ¡
h?ps://www.explainxkcd.com/wiki/index.php/456:_Cau<onary ¡
SoKware ¡
7 ¡
Text/Ascii ¡
container ¡holding ¡the ¡bytes, ¡but ¡the ¡bytes ¡ themselves ¡
context ¡
– the ¡same ¡sequence ¡of ¡bits ¡can ¡be ¡used ¡to ¡represent ¡a ¡ character, ¡or ¡an ¡integer, ¡or ¡a ¡floa<ng-‑point ¡number, ¡
8 ¡
Why ¡assembly ¡language? ¡
– 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 ¡
9 ¡
Assembly ¡language… ¡really?! ¡
– Compilers ¡are ¡much ¡be?er ¡& ¡more ¡pa<ent ¡than ¡you ¡are ¡
execu<on ¡model ¡
– Behavior ¡of ¡programs ¡in ¡presence ¡of ¡bugs ¡
– Tuning ¡program ¡performance ¡
– Implemen<ng ¡system ¡sokware ¡
– Crea<ng ¡/ ¡figh<ng ¡malware ¡
10 ¡
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 ¡
hello.c ¡ hello ¡
%gcc ¡ ¡-‑o ¡ ¡hello ¡ ¡hello.c ¡
%hello ¡
Declara8ons ¡and ¡the ¡Preprocessor ¡
– provides ¡ability ¡to ¡include ¡declara<ons ¡from ¡other ¡files ¡
– 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 ¡
linked ¡together ¡to ¡create ¡a ¡file ¡with ¡executable ¡code ¡
– #include ¡<stdio.h> ¡for ¡standard ¡system ¡files ¡ – #include ¡"swap.h“ ¡for ¡user-‑wri?en ¡files ¡
12 ¡
Compila8on ¡stages ¡
– 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 ¡
– makes ¡sure ¡individual ¡parts ¡are ¡consistent ¡within ¡themselves ¡ – creates ¡an ¡object ¡(.o) ¡file ¡
– joins ¡one ¡or ¡more ¡compiled ¡object ¡files ¡together ¡
– 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 ¡
Compiling ¡a ¡C ¡program ¡
– invoca<on: ¡gcc ¡op<ons ¡source-‑files ¡ – common ¡op<ons ¡
– a ¡very ¡simple ¡compila<on ¡command ¡
– a ¡simple ¡compila<on ¡command ¡
– executable ¡is ¡run ¡in ¡UNIX ¡by ¡just ¡typing ¡its ¡name ¡(some<mes ¡preceded ¡by ¡"./", ¡ like ¡this: ¡./prog1) ¡
14 ¡
A ¡look ¡ahead ¡to ¡Chp. ¡3 ¡ ¡
– A ¡tool ¡that ¡determines ¡the ¡instruc<on ¡sequence ¡represented ¡by ¡an ¡ executable ¡program ¡file ¡ – Unix ¡command ¡
– -‑d, ¡-‑-‑disassemble ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
– -‑D, ¡-‑-‑disassemble-‑all ¡ ¡ ¡ ¡ ¡
– -‑S, ¡-‑-‑source ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
– -‑s, ¡-‑-‑full-‑contents ¡ ¡ ¡ ¡ ¡ ¡ ¡
– -‑t, ¡-‑-‑syms ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
– -‑T, ¡-‑-‑dynamic-‑syms ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
15 ¡
Hardware ¡
16 ¡
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 ¡
network ¡adapters, ¡ video ¡cards, ¡etc. ¡ Memory ¡ bus ¡ System ¡ bus ¡
Register ¡File ¡
I/O ¡bus ¡
hellob ¡executable ¡ stored ¡on ¡disk* ¡
Mouse ¡
HW ¡organiza8on ¡details ¡
– Interprets/executes ¡instruc<ons ¡stored ¡in ¡main ¡memory ¡ – Updates ¡the ¡PC ¡to ¡point ¡to ¡the ¡next ¡instruc<on ¡ – PC ¡ ¡
memory ¡
– ALU ¡
– Register ¡file ¡
with ¡their ¡own ¡name ¡
– ISA ¡– ¡instruc<on ¡set ¡architecture ¡defines ¡
– h?p://www.c-‑jump.com/CIS77/reference/Instruc<ons_by_Mnemonic.html ¡
18 ¡
HW ¡organiza8on ¡details ¡(cont.) ¡
– System’s ¡connec<on ¡to ¡the ¡external ¡world ¡ – Transfers ¡informa<on ¡back ¡and ¡forth ¡between ¡the ¡I/O ¡bus ¡and ¡an ¡I/O ¡ device ¡
– Temporary ¡storage ¡ – Holds ¡both ¡the ¡program ¡and ¡the ¡data ¡it ¡manipulates ¡ ¡
– Is ¡organized ¡as ¡a ¡linear ¡array ¡of ¡bytes ¡each ¡with ¡its ¡own ¡unique ¡ address ¡star<ng ¡at ¡zero ¡
– Transfers ¡one ¡“word” ¡at ¡a ¡<me ¡
19 ¡
Memory ¡Hierarchy ¡(chp. ¡6) ¡
20 ¡ 2/14/17 ¡
Target ¡of ¡Memory ¡Hierarchy ¡Op8miza8ons ¡
21 ¡ 2/14/17 ¡
– The ¡latency ¡of ¡a ¡memory ¡access ¡is ¡the ¡<me ¡(usually ¡ in ¡cycles) ¡between ¡a ¡memory ¡request ¡and ¡its ¡ comple<on ¡
– Bandwidth ¡is ¡the ¡amount ¡of ¡useful ¡data ¡that ¡can ¡be ¡ retrieved ¡over ¡a ¡<me ¡interval ¡
– Cost ¡of ¡performing ¡op<miza<on ¡(e.g., ¡copying) ¡ should ¡be ¡less ¡than ¡an<cipated ¡gain ¡
Abstrac8on ¡
– Process ¡(chp. ¡8) ¡
– Virtual ¡Memory ¡(chp. ¡9) ¡
the ¡main ¡memory ¡
– Includes ¡files ¡ – Begins ¡at ¡same ¡fixed ¡address ¡for ¡all ¡processes ¡ – Address ¡space ¡(chp. ¡7) ¡
– Files ¡(chp. ¡10) ¡
22 ¡
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 ¡
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 ¡
8-‑bit ¡bytes ¡
just ¡an ¡ index ¡into ¡ this ¡array ¡ ¡
¡
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 ¡
Informa8on ¡Representa8on ¡and ¡ Interpreta8on ¡ Chapter ¡2 ¡
25 ¡
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 ¡
Number ¡of ¡values ¡(vs ¡range ¡of ¡values) ¡ ¡
“word ¡size” ¡
– Nominal ¡size ¡of ¡integer ¡ and ¡pointer ¡data ¡
in-‑#bits ¡
– Is ¡it ¡big ¡enough? ¡ ¡
becoming ¡more ¡prevalent ¡
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 ¡
¡ ¡ ¡ ¡
Outline ¡
28 ¡
Interpreta8on ¡& ¡Representa8on ¡
– abc ¡ – 123 ¡ – 3.14 ¡ – 0x61 ¡
– 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) ¡
Floa8ng ¡point ¡
– “what ¡every ¡computer ¡scien<st ¡should ¡know ¡about ¡ floa<ng ¡point” ¡
– Due ¡to ¡finite ¡precision ¡of ¡the ¡representa<on ¡
30 ¡
Informa8on ¡Storage ¡(general) ¡
about ¡data ¡types ¡
– It’s ¡the ¡C ¡compiler ¡that ¡maintains ¡this ¡type ¡of ¡informa<on ¡
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 ¡
level ¡programming ¡(chp.3) ¡
31 ¡
Informa8on ¡Storage ¡(details) ¡
iden<fied ¡
addresses ¡ Reminder: ¡no ¡data ¡typing ¡at ¡this ¡level ¡
32 ¡
Hexadecimal ¡Nota8on ¡(Hex) ¡
– 0x ¡or ¡0X ¡prefix ¡interpreted ¡as ¡hex ¡ value ¡ – Not ¡case ¡sensi<ve ¡ – Example ¡
0xFA1D37B, ¡0xfA1d37B ¡
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
Binary ¡to ¡Hex ¡to ¡Octal ¡
digit” ¡
– The ¡power ¡is ¡the ¡# ¡of ¡bits ¡per ¡ hex/octal ¡digit ¡
– Every ¡4 ¡bits ¡= ¡1 ¡hex ¡digit ¡
– Digits ¡0-‑7 ¡
– Every ¡3 ¡bits ¡= ¡1 ¡octal ¡digit ¡
34 ¡
DEC OCT HEX BIN Notes
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
When ¡value ¡x10 ¡is ¡a ¡power ¡of ¡2 ¡
– x ¡= ¡1610 ¡= ¡24 ¡= ¡100002 ¡= ¡1016 ¡
– Reminder: ¡hex ¡digit ¡0 ¡in ¡binary ¡is ¡0000 ¡ – Leading ¡hex ¡digit ¡where ¡0 ¡<= ¡i ¡<=3 ¡
– Followed ¡by ¡j ¡hex ¡0s ¡ – Examples: ¡ ¡
35 ¡
Convert ¡to ¡Decimal ¡(why?) ¡
– E.g., ¡31610 =
– E.g., ¡4748 =
– 13C16 =
¡
– 1001102 =
have ¡the ¡form ¡dn-‑1dn-‑2…d1d0. ¡ ¡
– Summing ¡dn-‑1 ¡×rn-‑1 ¡+ ¡dn-‑2×rn-‑2 ¡+ ¡… ¡+ ¡d0×r0 ¡converts ¡to ¡base ¡10. ¡ ¡ ¡
36 ¡
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 ¡
Base ¡Conversions ¡
– Convert ¡to ¡base ¡10 ¡by ¡mul1plica1on ¡of ¡powers ¡
¡
– Convert ¡from ¡base ¡10 ¡by ¡repeated ¡division ¡
¡ ¡
– Conver<ng ¡base ¡x ¡to ¡base ¡y: ¡convert ¡base ¡x ¡to ¡base ¡10 ¡ then ¡convert ¡base ¡10 ¡to ¡base ¡y ¡
38 ¡
More ¡prac8ce ¡
– 12310 ¡= ¡( ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡)3 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡and ¡check ¡ – 123410 ¡= ¡( ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡)16 ¡ ¡ ¡ ¡ ¡ ¡ ¡and ¡check ¡ – Another ¡way ¡from ¡decimal ¡to ¡base ¡n ¡
39 ¡
n8 ¡ n7 ¡ n6 ¡ n5 ¡ n4 ¡ n3 ¡ n2 ¡ n1 ¡ n0 ¡ 256 ¡ 128 ¡ 64 ¡ 32 ¡ 16 ¡ 8 ¡ 4 ¡ 2 ¡ 1 ¡
for ¡n ¡= ¡2 ¡
Hex ¡and ¡Binary ¡addi8on/subtrac8on ¡
– A ¡+ ¡8 ¡= ¡ ¡ – 13 ¡+ ¡F ¡= ¡ ¡ – BEAD ¡+ ¡4321 ¡= ¡ ¡
– 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 ¡
Addressing ¡and ¡byte ¡ordering ¡
– What ¡is ¡the ¡address ¡of ¡the ¡object? ¡ – What ¡is ¡the ¡order ¡of ¡the ¡bytes ¡in ¡memory? ¡
– 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 ¡
Addressing ¡and ¡byte ¡ordering ¡(cont) ¡
– “The ¡big ¡end ¡goes ¡at ¡byte ¡zero” ¡ – “big ¡end” ¡means ¡the ¡most ¡significant ¡byte ¡of ¡the ¡given ¡value ¡
– “The ¡li?le ¡end ¡goes ¡at ¡byte ¡zero” ¡ – “li?le ¡end” ¡means ¡the ¡least ¡significant ¡byte ¡of ¡the ¡given ¡value ¡
– 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 ¡
Big/Livle ¡Endian ¡one ¡li?le ¡two ¡li?le ¡three ¡li?le ¡endians ¡;o) ¡
Byte ¡
Big ¡ Endian ¡ Livle ¡ Endian ¡ 0x100 ¡ 01 ¡ 67 ¡ 0x101 ¡ 23 ¡ 45 ¡ 0x102 ¡ 45 ¡ 23 ¡ 0x103 ¡ 67 ¡ 01 ¡
43 ¡
Byte ¡
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 ¡
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 ¡
¡
X86 ¡is ¡livle ¡endian ¡
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. ¡
to ¡the ¡conclusion ¡that ¡it's ¡not ¡li?le ¡endian ¡that's ¡the ¡strange ¡choice—it's ¡
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 ¡
<me, ¡where ¡the ¡size ¡of ¡the ¡unit ¡can ¡vary ¡(byte, ¡word, ¡dword, ¡etc.) ¡
44 ¡
Endian-‑ness ¡
45 ¡
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 ¡
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) ¡
Systems ¡can ¡have ¡different… ¡
unicode) ¡
bytes) ¡
46 ¡