Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Bibliografía:
[BRY16] Cap.3 Computer Systems: A Programmer’s Perspective 3rd ed. Bryant, O’Hallaron. Pearson, 2016
Signatura ESIIT/C.1 BRY com
Bibliografía:
[BRY16] Cap.3 Computer Systems: A Programmer’s Perspective 3rd ed. Bryant, O’Hallaron. Pearson, 2016
Signatura ESIIT/C.1 BRY com
† direccionamiento relativo a contador
de programa, “PC-relative”
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition ‡ penalización por predicción saltos 2
Grado Informática, 2º Curso Estructura de Computadores
CF a 1 sii sale acarreo del MSB† (cn) (hacer caso cuando comp. sin signo)
ZF a 1 sii a == b
SF a 1 sii (a-b) < 0 (como número con signo)
OF a 1 sii overflow‡ en complemento a dos (atender si comp. con signo)
(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)
definición overflow OF = (cn ^ cn-1) mientras que acarreo CF = cn
Saltos
Instrucciones jCC
Saltar a otro lugar del código si se cumple el código de condición CC
Versión Goto
ntest = !Test; Crear regiones de código separadas
if (ntest) goto Else;
para las expresiones Then y Else
val = Then_Expr;
goto Done; Ejecutar sólo la adecuada
Else:
val = Else_Expr;
Done:
. . .
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition † “pipeline” = “tubería”, aquí es “segmentación de cauce” 17
Grado Informática, 2º Curso Estructura de Computadores
absdiff:
movq %rdi, %rax # x
subq %rsi, %rax # result = x-y
movq %rsi, %rdx
subq %rdi, %rdx # eval = y-x
cmpq %rsi, %rdi # x:y
cmovle %rdx, %rax # if <=, result = eval
ret † generar con gcc -fif-conversion -Og -S control.c
ó incluso con gcc -O -S control.c
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition en el gcc-7.3.0 que viene con Ubuntu-18.04 18
Grado Informática, 2º Curso Estructura de Computadores
Cálculos costosos
val = Test(x) ? Hard1(x) : Hard2(x);
Cálculos arriesgados
val = p ? *p : 0;
Ejercicio
cmpq b,a equiv. restar t=a-b pero sin ajustar destino
%rax SF CF OF ZF
xorq %rax, %rax 0x0000 0000 0000 0000 0 0 0 1
subq $1, %rax 0xFFFF FFFF FFFF FFFF 1 1 0 0
cmpq $2, %rax 0xFFFF FFFF FFFF FFFF 1 0 0 0
setl %al 0xFFFF FFFF FFFF FF01 1 0 0 0
movzbl %al, %eax 0x0000 0000 0000 0001 1 0 0 0
Ejercicio
cmpq b,a equiv. restar t=a-b pero sin ajustar destino
%rax SF CF OF ZF
xorq %rax, %rax 0x0000 0000 0000 0000 0 0 0 1
subq $1, %rax 0xFFFF FFFF FFFF FFFF 1 1 0 0
cmpq $2, %rax 0xFFFF FFFF FFFF FFFF 1 0 0 0
setl %al 0xFFFF FFFF FFFF FF01 1 0 0 0
movzbl %al, %eax 0x0000 0000 0000 0001 1 0 0 0
Body: {
Sentencia1;
Sentencia2;
…
Sentencian;
}
“body” = cuerpo,
“statement” = sentencia,
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition “test” = comprobación. 25
Grado Informática, 2º Curso Estructura de Computadores
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition † “jump-to-middle” 26
Grado Informática, 2º Curso Estructura de Computadores
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition † pount_goto_jtm viene de “jump-to-middle” 27
Grado Informática, 2º Curso Estructura de Computadores
Versión Goto
Versión Do-While if (!Test)
if (!Test) goto done;
goto done; loop:
do Body
Body if (Test)
while(Test); goto loop;
done: done:
† evitar test al principio con gcc -O123 -fno-tree-ch
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition ó con gcc -Os -fno-reorder-blocks 28
Grado Informática, 2º Curso Estructura de Computadores
Versión While
Init;
while (Test ) {
Body
Update;
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 31
Grado Informática, 2º Curso Estructura de Computadores
Conversión For-While
long pcount_for_while
Init (unsigned long x)
{
i = 0 size_t i;
long result = 0;
Test i = 0;
i < WSIZE while (i < WSIZE)
{
Update unsigned bit =
(x >> i) & 0x1;
i++ result += bit;
i++;
Body }
{ return result;
unsigned bit = }
(x >> i) & 0x1;
result += bit;
}
† “switch”=conmutador,
tampoco traducimos
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition “for” o “while” 34
Grado Informática, 2º Curso Estructura de Computadores
long switch_eg
(long x, long y, long z) Ejemplo de
{
long w = 1; sentencia switch
switch(x) {
case 1:
w = y*z; Múltiples etiquetas de caso
break; Aquí: 5 y 6
case 2:
w = y/z; Caídas en cascada†
/* Fall Through */ Aquí: 2
case 3:
w += z; Casos ausentes†
break; Aquí: 4
case 5:
case 6:
w -= z;
break;
default:
w = 2;
}
return w;
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition † “fall-through cases”, “missing cases” 35
Grado Informática, 2º Curso Estructura de Computadores
•
Traducción aprox. (C ficticio) •
goto *JTab[x]; •
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition † “jump table”, “jump targets” 36
Grado Informática, 2º Curso Estructura de Computadores
Inicialización:
Notar que w no
se inicializa aquí
switch_eg:
movq %rdx, %rcx # z → %rcx
cmpq $6, %rdi # x:6 ¿Qué rango de valores
† ja .L8 # default:
jmp *.L4(,%rdi,8) # goto *Jtab[x] cubre default?
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition † “above” = mayor sin signo 37
Grado Informática, 2º Curso Estructura de Computadores
Inicialización:
switch_eg:
movq %rdx, %rcx # z → %rcx
cmpq $6, %rdi # x:6
† ja .L8 # default:
Salto
jmp *.L4(,%rdi,8) # goto *Jtab[x]
indirecto
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition † “Ln” = “Label n” = etiqueta (de salto) n 38
Grado Informática, 2º Curso Estructura de Computadores
Tabla de Saltos
Tabla de saltos switch(x) {
.section .rodata case 1: // .L3
.align 8 w = y*z;
.L4:
break;
.quad .L8 # x = 0
.quad .L3 # x = 1 case 2: // .L5
.quad .L5 # x = 2 w = y/z;
.quad .L9 # x = 3 /* Fall Through */
.quad .L8 # x = 4 case 3: // .L9
.quad .L7 # x = 5 w += z;
.quad .L7 # x = 6 break;
case 5:
case 6: // .L7
w -= z;
break;
default: // .L8
w = 2;
}
Bloques de Código (x == 1)
switch(x) { .L3:
case 1: // .L3 movq %rsi, %rax # y
w = y*z; imulq %rdx, %rax # y*z
break; ret
. . .
}
Registro Uso(s)
%rdi Argumento x
%rsi Argumento y
%rdx Argumento z
%rax Valor de retorno
merge:
w += z;
Bloques de Código (x == 2, x == 3)
long w = 1; .L5: # Case 2
. . . movq %rsi, %rax
switch(x) { † cqto
. . . idivq %rcx # y/z
case 2: jmp .L6 # goto merge
w = y/z; .L9: # Case 3
/* Fall Through */ movl $1, %eax # w = 1
case 3: .L6: # merge:
w += z; addq %rcx, %rax # w += z
break; ret
. . .
}
Registro Uso(s)
%rdi Argumento x
%rsi Argumento y
%rdx Argumento z
%rax Valor de retorno
mnemotécnico CQO según Intel
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition † “Convert Quad to Oct” 43
Grado Informática, 2º Curso Estructura de Computadores
Registro Uso(s)
%rdi Argumento x
%rsi Argumento y
%rdx Argumento z
%rax Valor de retorno
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 44
Grado Informática, 2º Curso Estructura de Computadores
Resumen
Control C
if-then-else
do-while
while, for
switch
Control Ensamblador
Salto condicional
Movimiento condicional
Salto indirecto (mediante tablas de saltos)
Compilador genera secuencia código p/implementar control más complejo
Técnicas estándar
Bucles convertidos a forma do-while (ó salta-en medio ó copia-test)
Sentencias switch grandes usan tablas de saltos
Sentencias switch poco densas → árboles decisión (if-elseif-elseif-else)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 45
Grado Informática, 2º Curso Estructura de Computadores
Bibliografía:
[BRY16] Cap.3 Computer Systems: A Programmer’s Perspective 3rd ed. Bryant, O’Hallaron. Pearson, 2016
Signatura ESIIT/C.1 BRY com
† direccionamiento relativo a contador
de programa, “PC-relative”
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition ‡ penalización por predicción saltos 46