infoind - universitat jaume imermaja.act.uji.es/docencia/et1032/data2014/tema3/tema3_2_2x1… ·...
TRANSCRIPT
17/02/2014
1
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 310
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 311
17/02/2014
2
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 312
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 313
17/02/2014
3
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 314
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 315
17/02/2014
4
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 316
fldl (%esi,%edx,8)
fmull (%eax)
addl $8,%eax
incl %edx
faddp %st,%st(1)
cmpl $3,%edx
jle .L11
movl 16(%ebp),%eax
fstl (%eax,%ecx,8)
movl %ebx,%ecx
faddp %st,%st(1)
cmpl $999,%ecx
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 317
17/02/2014
5
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 318
0100101xxxxxx110100101010010110010110xxxxx10110010xxxxxx001011001001010xxxx0110
0100101xxxxxxx10010010100010110
0100101xxxxxx110100101010010110010110xxxxx10110010xxxxxx001011001001010xxxx0110
0100101xxxxxxx10010010100010110
0100101xxxxxx110100101010010110010110xxxxx10110010xxxxxx0010110
01001010xxxx0110
0100101xxxxxxx10010010100010110
010010101100110100101010010110010110111010110010011010010110010010101000110
010010101110110010010100010110
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 319
17/02/2014
6
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 320
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 321
17/02/2014
7
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 322
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 323
17/02/2014
8
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 324
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 325
17/02/2014
9
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 326
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 327
17/02/2014
10
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 328
double num_real;
int entero;
char *ptr_cadena;
var1+=((var2>5)?vector[ind++]:(int)funcion(var3=x+5));
Tema 3 – Soporte lógico 329
#include <stdlib.h>
#include "definiciones.h"
#define TAM 250 // constantes del programa
#define max((a),(b)) ((a)>(b)?(a):(b))// macros
#if DEBUG == OK // compilacion condicional
... // aqui vendria el codigo
#endif // podria ser else o elif
ET1032Informática Industrial
ET1032Informática Industrial
17/02/2014
11
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 330
#include <stdio.h>
#define MESES 12
main()
{
int i;
char *meses[]={"Ene","Feb","Mar","Abr","May",
"Jun","Jul","Ago","Sep","Oct","Nov","Dic"};
for(i=0;i<MESES;i++){
printf("El mes %02d es %s.\n",i+1, meses[i]);
}
}
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 331
17/02/2014
12
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 332
tBuffer = 2 * (TAM + 1);
ptrBuffer = malloc(tBuffer);
if (ptrBuffer == NULL) {
myError = NOMEM;
ptrBuffer = staticBuffer;
}
else {
myError = 0;
auxBuffer[free++] = staticBuffer;
}
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 333
tBuffer = 2 * (TAM + 1);
ptrBuffer = malloc(tBuffer);
if (ptrBuffer == NULL) {
myError = NOMEM;
ptrBuffer = staticBuffer;
}
else {
myError = 0;
auxBuffer[free++] = staticBuffer;
}
Asignación Prelación entre operadores,
parámetros pasados a una función,
expresiones a evaluar…
Comparación
Separador de sentencias de
ejecución secuencial
Delimitador de bloques de sentencias
de ejecución común
Acceso a elementos de un vector
La indentación es recomendable
17/02/2014
13
Tema 3 – Soporte lógico 334
char c; // entero de 8 bits, caracter
int i; // entero segun la arquitectura
float r; // real en precision simple (32b)
double d; // real de precision doble (64b)
long int li; // o simplemente long
short int si; // util segun la arquitectura
unsigned int ui; // entero sin signo
long long int lli; // entero mayor ...
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 335
char a,b,c=‘a’; // comillas simples
int i=-345,j,k;
long int l1=0L; // constante con L
float r,s=1.45;
double d,f=34.0 // .0 para indicar real
a=37,b=‘\013’; // numerico, octal
j=035530; // octal
k=0x3fff; // hexadecimal
r=3.4e-42; //3.4 por 10 elevado a -42
d=4.034L; // constante tipo double
ET1032Informática Industrial
ET1032Informática Industrial
17/02/2014
14
Tema 3 – Soporte lógico 336
char a, b, c;
int i, j, k;
// los basicos con prelacion clasica
a = 2 * ( b + c - 4) / 3;
// modulo, predecremento, postincremento
i -= j++ % --k;
// operaciones bit a bit << >> & | ^ ~
i = (j + k) >> 3;// desplazamiento, como <<
j = i & ~k; // and y not (comp. a 1)
a = (b | 0x3f ) ^ c; // or y xor
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 337
char vec_char[TAM]; // vectores, matrices
int vec_int[TAM2], mat3_int[T1][T2][T3];
int vec_data[] = {4507,-3,0xbad5,12, 83455};
char *ptr_char; // y punteros
char *frase = "Esto es una cadena de texto";
int *ptr_int;
void *ptr_untyped;
vec_char[index] = ‘h’; // 0<= int index < TAM
mat3_int[indx1][indx2][indx3] = 14507;
int_var = vec_data[0]; //int_var vale 4507
ET1032Informática Industrial
ET1032Informática Industrial
17/02/2014
15
Tema 3 – Soporte lógico 338
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 339
ET1032Informática Industrial
ET1032Informática Industrial
char c, *ptr_char = "Cadena";
int *ptr_int, i;
c = *ptr_char; // c vale ‘C’
ptr_int = (int *)ptr_char;
i = *ptr_int; // i vale 0x43616465
17/02/2014
16
Tema 3 – Soporte lógico 340
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 341
ET1032Informática Industrial
ET1032Informática Industrial
int *ptr_int1,*ptr_int2,vec_int[]={8,43,-7,11};
int var_int = 1;
ptr_int1 = (int *)malloc(4 * sizeof(int));
ptr_int2 = &var_int;
vec_int = (int *)malloc(10 * sizeof(int));
17/02/2014
17
Tema 3 – Soporte lógico 342
ET1032Informática Industrial
ET1032Informática Industrial
int *ptr_int1, *ptr_int2;
char *ptr_char;
ptr_int1 = (int *)malloc(4 * sizeof(int));
ptr_int1[3] = 27;
ptr_int2[0] = 27; // Error de memoria
ptr_int1[4] = 27; // Error de memoria
free(ptr_int1); // Para liberar memoria
ptr_int2 += 3; // + (3 * sizeof(int))
ptr_char++; // + sizeof(char)
Tema 3 – Soporte lógico 343
ET1032Informática Industrial
ET1032Informática Industrial
struct color {
char nombre[MAX_LNG];
int r, g, b;
} col1, col2;
struct color col3, *ptr_col;
col3.r = 0; col3.g = 0, col3.b = 0xffffff;
strcpy(col3.nombre, "Azul");
col1 = col3;
ptr_col = &col3;
col2.r = ptr_col->r;
17/02/2014
18
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 344
Tema 3 – Soporte lógico 345
ET1032Informática Industrial
ET1032Informática Industrial
tBuffer = 2 * (TAM + 1);
ptrBuffer = malloc(tBuffer);
if(ptrBuffer == NULL){ // evalua condicion
myError = NOMEM; // bloque afirmativo
ptrBuffer = staticBuffer;
}
... // resto del programa
17/02/2014
19
Tema 3 – Soporte lógico 346
ET1032Informática Industrial
ET1032Informática Industrial
if(ptrBuffer == NULL){ // evalua condicion
myError = NOMEM; // bloque afirmativo
ptrBuffer = staticBuffer;
}
else { // bloque negativo
myError = 0;
auxBuffer[free++] = staticBuffer;
}
... // resto del programa
Tema 3 – Soporte lógico 347
ET1032Informática Industrial
ET1032Informática Industrial
if(ptrBuffer == NULL){ // evalua condicion
myError = NOMEM; // bloque afirmativo
ptrBuffer = staticBuffer;
}
else if(var2 > LIMIT){ // evalua cond. 2
myError = 0;
auxBuffer[free++] = staticBuffer;
}
else {...} // ninguna condicion
... // resto del programa
17/02/2014
20
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 348
var1 = ((var2 > 5) ? valor1 : valor2);
Tema 3 – Soporte lógico 349
ET1032Informática Industrial
ET1032Informática Industrial
switch (var1+var2){ // evalua expresion
case VAL1: ... // valor constante
... // instrucciones
break; // salta al final
case VAL2:
case VAL3: ... // lo mismo para ambas
case VAL4: ... // sin break sigue
break;
default: ... // si no se da ninguna
break; // cosmetico
}
17/02/2014
21
Tema 3 – Soporte lógico 350
ET1032Informática Industrial
ET1032Informática Industrial
for (i=0;i<LIMIT;i++) {
valor = convolucion(matriz2,filtro,i);
matriz[i] = valor;
actualiza_histograma(valor);
}
Tema 3 – Soporte lógico 351
ET1032Informática Industrial
ET1032Informática Industrial
for (j=valor(x,y); i<LIMIT; i+=3) {...}
for (;;) {...} // bucle infinito
for (; i<j+LIMIT; i=2*i-500*j) {...}
...
17/02/2014
22
Tema 3 – Soporte lógico 352
ET1032Informática Industrial
ET1032Informática Industrial
while (j < LIMIT) {
valor = polinomio(x,y,3);
matriz[j] = valor;
j = nueva_entrada(valor);
}
Tema 3 – Soporte lógico 353
ET1032Informática Industrial
ET1032Informática Industrial
do {
valor = polinomio(x,y,3);
matriz[j] = valor;
j = nueva_entrada(valor);
} while (j < LIMIT)
17/02/2014
23
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 354
Tema 3 – Soporte lógico 355
ET1032Informática Industrial
ET1032Informática Industrial
for (i=0; i<LIMIT; i++) {
valor = convolucion(matriz2,filtro,i);
if (valor<UMBRAL_NEG) break;
matriz[i] = valor;
actualiza_histograma(valor);
}
while (i<LIMIT) { // i ya vale 0
valor = convolucion(matriz2,filtro,i);
if (valor<UMBRAL_NEG) break;
matriz[i++] = valor;
actualiza_histograma(valor);
} // ¿son equivalentes ambos bucles?
17/02/2014
24
Tema 3 – Soporte lógico 356
ET1032Informática Industrial
ET1032Informática Industrial
for (i=0; i<LIMIT; i++) {
valor = convolucion(matriz2,filtro,i);
if (valor<UMBRAL_NEG) continue;
matriz[i] = valor;
actualiza_histograma(valor);
}
while (i<LIMIT) { // i ya vale 0
valor = convolucion(matriz2,filtro,i);
if (valor<UMBRAL_NEG) continue;
matriz[i++] = valor;
actualiza_histograma(valor);
} // ¿son equivalentes ambos bucles?
Tema 3 – Soporte lógico 357
ET1032Informática Industrial
ET1032Informática Industrial
for (i=0; i<LIMIT; i++) {
...
while (var>3*j+1) { // bucles anidados
...
if (cond==SALIR) goto FUERA;
...
}
...
}
FUERA:
... // la ejecuón continua aqui
17/02/2014
25
Tema 3 – Soporte lógico 358
ET1032Informática Industrial
ET1032Informática Industrial
(a==b) (a!=b) // igualdad y desigualdad
(a<b) (a>b) (a>=b) (a<=b) // relacion
(a==b)&&(a<=c) // and logico -no bit a bit-
(a!=b)||(a>c) // or logico -no bit a bit-
Tema 3 – Soporte lógico 359
ET1032Informática Industrial
ET1032Informática Industrial
if (lng == strlen(cadena))
// lng es igual que la longitud de cadena
if (lng = strlen(cadena))
// la longitud no es 0 -y la asigna a lng
if ((lng = strlen(cadena)) <= MAX_LEN)
// asigna y evalua
c1 && c2 || c3 && c4 ...
//de izquierda a derecha: cortocircuitos
17/02/2014
26
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 360
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 361
17/02/2014
27
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 362
// definicion
int strlen(char *ps) {
int len = 0; // variables locales y
// parametros en la pila
while(ps[len]) // cadena acabada en ‘/0’
len++;
return len; //devolvemos el valor
}
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 363
int strlen(char *); // prototipo
...
...
int longitud;
char *texto = "Esta cadena";
longitud = strlen(texto); // llamada
17/02/2014
28
Tema 3 – Soporte lógico 364
#include <stdio.h> // preprocesador
#define MI_CNST 255
void invierte(char *s); // prototipo
int var1, var2 = 0; // variables globales
int main( ) // entrada al codigo
{
int var3, var4; // variables locales
... // en la pila
exit(0); // llamada al sistema
} // para terminar
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 365
int main(int argc, char *argv[])
// argc da el numero de parametros mas uno
// argv[0] es el nombre del programa miprog
// luego van los parametros p1, p2... */
// como cadenas de texto acabadas en ‘/0‘
ET1032Informática Industrial
ET1032Informática Industrial
17/02/2014
29
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 366
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 367
17/02/2014
30
Tema 3 – Soporte lógico 368
// MIPROG V1.1 - German Fabregat - 10/09/2012
// ...
#include "miprog.h" // Constantes, macros...
// int mifuncion(int veces, char *origen)
// Entrada: veces - numero de ...
// origen – cadena de ...
// Salida: indica la suma de ........
// ...
int mifuncion(int veces, char *origen)
{...}
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 369
// verificamos errores en las llamadas
if (sigaction(i,NULL,&accion) < 0) {
printf("Para la señal %s: ",nomsignal[i-1];
perror("Error leyendo sigaction");
exit(-1);
}
// Si todo ha ido bien seguimos
...
ET1032Informática Industrial
ET1032Informática Industrial
17/02/2014
31
Tema 3 – Soporte lógico 370
#ifdef DEBUG
printf("Iteracion %d del bucle interno\n",i);
printf("tam:%d - cad:%s\n",tam,cad);
#endif
ET1032Informática Industrial
ET1032Informática Industrial