Sei sulla pagina 1di 24

Grado Informtica, 2 Curso

Estructura de Computadores

ProgramacinaNivelMquinaV: AlineamientoyUniones
EstructuradeComputadores Semana6 Bibliografa:
[BRY11]Cap.3
ComputerSystems:AProgrammersPerspective.Bryant,OHallaron.Pearson,2011 Signatura ESIIT/C.1BRYcom

TransparenciasdellibroCS:APP,Cap.3 Introduction to Computer Systems:aProgrammers Perspective

Autores: RandalE.BryantyDavidR.OHallaron

Grado Informtica, 2 Curso

Estructura de Computadores

Guadetrabajoautnomo(4h/s)

Lectura:delCap.3CS:APP(Bryant/OHallaron)
Unions,DataAlignment.
3.9.2 3.9.3 pp.278285

Ejercicios:delCap.3CS:APP(Bryant/OHallaron)
Probl.3.41 3.42
pp.285

Bibliografa:
[BRY11]Cap.3
ComputerSystems:AProgrammersPerspective.Bryant,OHallaron.Pearson,2011 Signatura ESIIT/C.1BRYcom

Grado Informtica, 2 Curso

Estructura de Computadores

Progr.MquinaIV:Alineamiento&Uniones

Estructuras
Alineamiento

Uniones

Grado Informtica, 2 Curso

Estructura de Computadores

Estructuras&Alineamiento

DatosDesalineados
c i[0]
p+5

i[1]
p+9

v
p+17

p p+1

struct S1 { char c; int i[2]; double v; } *p;

DatosAlineados
EltipodedatosprimitivorequiereK bytes LadireccindebesermltiplodeK
c
3bytes

i[0]
p+8

i[1]

4bytes

v
p+16 p+24

p+0

p+4 Mltiplode4

Mltiplode8 Mltiplode8
4

Mltiplode8

Grado Informtica, 2 Curso

Estructura de Computadores

PrincipiosdeAlineamiento

DatosAlineados
EltipodedatosprimitivorequiereK bytes LadireccindebesermltiplodeK Requisitoenalgunasmquinas;recomendadoenIA32

tratadodiferentementeenIA32Linux,x8664Linux,yWindows!*

MotivacinparaAlinearlosDatos
Alamemoriaseaccede(fsicamente)entrozos(alineados)de48bytes
(dependiendodelsistema) Ineficientecargaroalmacenardatoquecruzafronteraquad word Mem.virtualmuydelicadacuandoundatoseextiendea2pginas

Compilador
Insertahuecosenestructuraparaasegurarcorrectoalineamientocampos

*dependedelaABIdelcompilador,inclusodelaoptimizacin.Verp.ej.http://gcc.gnu.org/onlinedocs/gcc/Compatibility.html

Grado Informtica, 2 Curso

Estructura de Computadores

CasosConcretosdeAlineamiento(IA32)

1byte:char,
sinrestriccionesenladireccin

2bytes:short,
elLSB* (bitmsbajo)deladireccindebeser02

4bytes:int,float,char *,
los2LSBs deladireccindebenser002

8bytes:double,
Windows(ylamayoradeotrosSOs &repertorios):
los3LSBs deladireccindebenser0002 Linux: los2LSBs deladireccindebenser002 esdecir,setratanlomismoqueuntipodedatosprimitivode4byte

12bytes:long double
Windows,Linux:

los2LSBs deladireccindebenser002 esdecir,setratanlomismoqueuntipodedatosprimitivode4byte

*eninglssuelendecirlowest 1,2,3bitsyleast 1,2,3significant bitsintercambiablemente.Enespaolsolemosdecirmenossignificativos 6

Grado Informtica, 2 Curso

Estructura de Computadores

CasosConcretosdeAlineamiento(x8664)

1byte:char,
sinrestriccionesenladireccin

2bytes:short,
elLSBdeladireccindebeser02

4bytes:int,float,
los2LSBs deladireccindebenser002

8bytes:double,char *,
Windows&Linux:

los3LSBs deladireccindebenser0002

16bytes:long double
Linux:
los3LSBs deladireccindebenser0002 esdecir,setratanlomismoqueuntipodedatosprimitivode8byte

Grado Informtica, 2 Curso

Estructura de Computadores

CumpliendoAlineamientoenEstructuras

struct S1 { Debencumplirserequisitosalinm.decadaelemento char c; int i[2]; Colocacinglobaldelaestructura double v; CadaestructuratieneunrequisitodealineamientoK } *p;

Dentrodelaestructura:

K =Mayoralineamientodecualquierelemento Direccininicial&longitudestructuradebensermltiplosdeK

Ejemplo(bajoWindowsx8664):
K=8,debidoalelementodouble
c
3bytes

i[0]
p+8

i[1]

4bytes

v
p+16 p+24

p+0

p+4 Mltiplode4

Mltiplode8 Mltiplode8
8

Mltiplode8

Grado Informtica, 2 Curso

Estructura de Computadores

ConvencionesdeAlineamientoDistintas

x8664IA32Windows:
K=8,debidoalelementodouble

struct S1 { char c; int i[2]; double v; } *p;


4bytes

c p+0

3bytes

i[0] p+8

i[1]

v p+16 p+24

p+4

IA32Linux
K=4;double tratadocomountipodedatosde4byte

c p+0

3bytes

i[0] p+8

i[1] p+12

v p+20

p+4

Grado Informtica, 2 Curso

Estructura de Computadores

CumpliendoRequisitoAlineamientoGlobal

Sielrequisitodealineamto.mximoesK Lastruct debeocuparglob.mltiplodeK

struct S2 { double v; int i[2]; char c; } *p;

v p+0 p+8

i[0]

i[1]

c p+16

7bytes

p+24

10

Grado Informtica, 2 Curso

Estructura de Computadores

Arrays deEstructuras

Longitudglobalestructura* mltiplodeK Cumplirrequisitosalineamiento decadaelemento


a[0] a+0 a+24 a[1] a+48 a[2]

struct S2 { double v; int i[2]; char c; } a[10];

a+72

v a+24 a+32

i[0]

i[1]

c a+40

7bytes

a+48

*yporconsiguientedelarray,puestoquesonestructurasconcatenadas 11

Grado Informtica, 2 Curso

Estructura de Computadores

AccesoaElementosdelArray

Calculardesplazamientoelem.array:12i
sizeof(S3)*i,incluyendoespaciadoresalineamiento

struct S3 { short i; float v; short j; } a[10];

Elementoj esdesplazamiento8dentrodeestructura Elensambladorgeneradesplazamientoa+8


Resueltoduranteenlazado(entiempodelink)

a+12 i a+12i a[i] a+12i


2bytes

a[0] a+0

j a+12i+8

2bytes

short get_j(int idx) { return a[idx].j; }

# %eax = idx leal (%eax,%eax,2),%eax # 3*idx movswl a+8(,%eax,4),%eax


movswl=Move Signed,Wordto Longyaquejesshort2B 12

Grado Informtica, 2 Curso

Estructura de Computadores

AhorrodeEspacio

Ponerprimerolostiposdedatosgrandes
struct S4 { char c; int i; char d; } *p; struct S5 { int i; char c; char d; } *p;

Efecto(K=4)
c
3bytes

i c d
2bytes

3bytes

13

Grado Informtica, 2 Curso

Estructura de Computadores

Progr.MquinaIV:Alineamiento&Uniones

Estructuras
Alineamiento

Uniones

14

Grado Informtica, 2 Curso

Estructura de Computadores

Ubicacin deUniones

Reservar deacuerdoalelementomsgrande Slopuedeusarseuncampoalavez


union U1 { char c; int i[2]; double v; } *up; struct S1 { char c; int i[2]; double v; } *sp; c
3bytes

c i[0] v up+0 up+4 up+8 i[1]

i[0] sp+8

i[1]

4bytes

v sp+24

sp+0

sp+4

sp+16

alloc(ation)/(ate)eningls.Verleccin2.4pg.17 15

Grado Informtica, 2 Curso

Estructura de Computadores

UsodeUnionesparaAccederPatronesBit
typedef union { float f; unsigned u; } bit_float_t; u f 0 4

float bit2float(unsigned u) { bit_float_t arg; arg.u = u; return arg.f; }

unsigned float2bit(float f) { bit_float_t arg; arg.f = f; return arg.u; }

Lomismoque(float) u ?

Lomismoque(unsigned) f ?

16

Grado Informtica, 2 Curso

Estructura de Computadores

OrdendeBytes:unRepaso

Idea
Palabrasshort/long/quad,almacenadasenmem.como2/4/8Bconsecutivos Culeselms(menos)significativo? Puedecausarproblemasalintercambiardatosbinariosentremquinas

BigEndian (extremomayor)
Elbytemssignificativoestenladireccinmsbaja(vieneprimero) Sparc

LittleEndian (extremomenor)
Elbytemenossignificativoestenladireccinmsbaja Intelx86

byteorderingserefiereaordenamientodebytesenpalabras,noaordenarunarray debytes=sorting *big/little endian=partidariodelextremomayor/menor,verexplicacinenellibro,2.1.4,Aside:Origin ofendian 17

Grado Informtica, 2 Curso

Estructura de Computadores

EjemplodeOrdendeBytes
union { unsigned unsigned unsigned unsigned } dw; char c[8]; short s[4]; int i[2]; long l[1];

32bit

c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] s[0] i[0] l[0] s[1] s[2] i[1] s[3]

64bit

c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] s[0] i[0] l[0]
18

s[1]

s[2] i[1]

s[3]

Grado Informtica, 2 Curso

Estructura de Computadores

EjemplodeOrdendeBytes(Cont).
int j; for (j = 0; j < 8; j++) dw.c[j] = 0xf0 + j; printf("Characters 0-7 == [0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x]\n", dw.c[0], dw.c[1], dw.c[2], dw.c[3], dw.c[4], dw.c[5], dw.c[6], dw.c[7]); printf("Shorts 0-3 == [0x%x,0x%x,0x%x,0x%x]\n", dw.s[0], dw.s[1], dw.s[2], dw.s[3]); printf("Ints 0-1 == [0x%x,0x%x]\n", dw.i[0], dw.i[1]); printf("Long 0 == [0x%lx]\n", dw.l[0]);

19

Grado Informtica, 2 Curso

Estructura de Computadores

OrdendeBytesenIA32
LittleEndian
f0 f1 f2 f3 f4 f5 f6 f7

c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] s[0] i[0] l[0]
LSB Alimprimir MSB LSB MSB

s[1]

s[2] i[1]

s[3]

Salida:
Characters Shorts Ints Long 0-7 0-3 0-1 0 == == == == [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7] [0xf1f0,0xf3f2,0xf5f4,0xf7f6] [0xf3f2f1f0,0xf7f6f5f4] [0xf3f2f1f0]

20

Grado Informtica, 2 Curso

Estructura de Computadores

OrdendeBytesenSun
BigEndian
f0 f1 f2 f3 f4 f5 f6 f7

c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] s[0] i[0] l[0]
MSB Alimprimir LSB MSB LSB

s[1]

s[2] i[1]

s[3]

SalidaenSun:
Characters Shorts Ints Long 0-7 0-3 0-1 0 == == == == [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7] [0xf0f1,0xf2f3,0xf4f5,0xf6f7] [0xf0f1f2f3,0xf4f5f6f7] [0xf0f1f2f3]
21

Grado Informtica, 2 Curso

Estructura de Computadores

OrdendeBytesenx8664
LittleEndian
f0 f1 f2 f3 f4 f5 f6 f7

c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] s[0] i[0] l[0]
LSB Alimprimir MSB

s[1]

s[2] i[1]

s[3]

Salidaenx8664:
Characters Shorts Ints Long 0-7 0-3 0-1 0 == == == == [0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7] [0xf1f0,0xf3f2,0xf5f4,0xf7f6] [0xf3f2f1f0,0xf7f6f5f4] [0xf7f6f5f4f3f2f1f0]

22

Grado Informtica, 2 Curso

Estructura de Computadores

Resumen

Arrays enC

Reservadememoriacontigua Alineadoshastacumplirconrequisitosalineamientodecadaelemento Punteroalprimerelemento Sinchequeodelmites

Estructuras
Reservarbytesenelordendeclarado Rellenosenmedioyalfinalparacumplirconalineamiento

Uniones
Declaracionessuperpuestas Unaformadesoslayarelsistemadetiposdedatos

23

Grado Informtica, 2 Curso

Estructura de Computadores

Guadetrabajoautnomo(4h/s)

Estudio:delCap.3CS:APP(Bryant/OHallaron)
Unions,DataAlignment.

3.9.2 3.9.3 pp.278285 Probl.3.41 3.42 pp.285

Bibliografa:
[BRY11]Cap.3
ComputerSystems:AProgrammersPerspective.Bryant,OHallaron.Pearson,2011 Signatura ESIIT/C.1BRYcom

24

Potrebbero piacerti anche