infoind - universitat jaume imermaja.act.uji.es/docencia/et1032/data2013/tema3/tema3_2_2x1… ·...
Post on 24-Sep-2020
3 Views
Preview:
TRANSCRIPT
30/01/2013
1
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 99
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 100
30/01/2013
2
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 101
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 102
30/01/2013
3
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 103
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 104
30/01/2013
4
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 105
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 106
30/01/2013
5
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 107
0100101xxxxxx110100101010010110010110xxxxx10110010xxxxxx001011001001010xxxx0110
0100101xxxxxxx10010010100010110
0100101xxxxxx110100101010010110010110xxxxx10110010xxxxxx0010110
01001010xxxx0110
0100101xxxxxxx10010010100010110
0100101xxxxxx110100101010010110010110xxxxx10110010xxxxxx001011001001010xxxx0110
0100101xxxxxxx10010010100010110
010010101100110100101010010110010110111010110010011010010110
010010101000110
010010101110110010010100010110
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 108
30/01/2013
6
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 109
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 110
30/01/2013
7
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 111
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 112
30/01/2013
8
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 113
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 114
30/01/2013
9
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 115
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 116
30/01/2013
10
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 117
double num_real;
int entero;
char *ptr_cadena;
var1+=((var2>5)?vector[ind++]:(int)funcion(var3=x+5));
Tema 3 – Soporte lógico 118
#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
30/01/2013
11
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 119
#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 120
30/01/2013
12
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 121
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 122
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
30/01/2013
13
Tema 3 – Soporte lógico 123
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 124
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
30/01/2013
14
Tema 3 – Soporte lógico 125
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 126
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
30/01/2013
15
Tema 3 – Soporte lógico 127
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 128
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
30/01/2013
16
Tema 3 – Soporte lógico 129
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 130
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));
30/01/2013
17
Tema 3 – Soporte lógico 131
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 132
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;
30/01/2013
18
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 133
Tema 3 – Soporte lógico 134
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
30/01/2013
19
Tema 3 – Soporte lógico 135
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 136
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
30/01/2013
20
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 137
var1 = ((var2 > 5) ? valor1 : valor2);
Tema 3 – Soporte lógico 138
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
}
30/01/2013
21
Tema 3 – Soporte lógico 139
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 140
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) {...}
...
30/01/2013
22
Tema 3 – Soporte lógico 141
ET1032Informática Industrial
ET1032Informática Industrial
do {
valor = polinomio(x,y,3);
matriz[j] = valor;
j = nueva_entrada(valor);
} while (j < LIMIT)
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 142
30/01/2013
23
Tema 3 – Soporte lógico 143
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?
Tema 3 – Soporte lógico 144
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?
30/01/2013
24
Tema 3 – Soporte lógico 145
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
Tema 3 – Soporte lógico 146
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-
30/01/2013
25
Tema 3 – Soporte lógico 147
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
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 148
30/01/2013
26
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 149
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 150
// 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
}
30/01/2013
27
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 151
int strlen(char *); // prototipo
...
...
int longitud;
char *texto = "Esta cadena";
longitud = strlen(texto); // llamada
Tema 3 – Soporte lógico 152
#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
30/01/2013
28
Tema 3 – Soporte lógico 153
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
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 154
30/01/2013
29
ET1032Informática Industrial
ET1032Informática Industrial
Tema 3 – Soporte lógico 155
Tema 3 – Soporte lógico 156
// 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
30/01/2013
30
Tema 3 – Soporte lógico 157
// 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
Tema 3 – Soporte lógico 158
#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
top related