ASSEMBLER
Generación de un ejecutable Programa de alto nivel Programa Compiler Assembler Linker Archivo Ejecutable Programa en lenguaje assembler Archivo Fuente Assembler Archivo Objeto Archivo Fuente Assembler Archivo Objeto Linker Archivo Ejecutable Archivo Fuente Assembler Archivo Objeto Library
Generación de un ejecutable Archivo Fuente Assembler Archivo Objeto Archivo Fuente Assembler Archivo Objeto Linker Archivo Ejecutable Archivo Fuente Assembler Archivo Objeto Library Contiene instrucciones de máquina, datos y enlaces a bibliotecas Puede tener referencias a funciones o variables de otros archivos
Generación de un ejecutable #include<stdio.h> int main(int argc, char* argv[]){ } int i; int sum=0; for(i=0;i<=100;i++) sum += sum+1; printf( La suma de 0 a 100 es %d\n, sum);
Generación de un ejecutable.text.align 2.globl main main: subu $sp,$sp,32 sw $ra,20($sp) sd $a0,32($sp) sw $0,24($sp) sw $0,28($sp) loop: lw $t6,28($sp) mul $t7,$t6,$t6 lw $t8,24($sp) addu $t9,$t8,$t7 sw $t9,24($sp) addu $t0,$t6,1 sw $t0,28($sp) ble $t0,100, loop la $a0, str lw $al, 24($sp) jal printf move $v0, $0 lw $ra,20($sp) addu $sp,$sp,32 j $ra str:.data.align 0.asciiz La suma de 0 a 100 es %d\n Notar que sólo el label main puede ser referenciado desde otro archivo
Facilidades.asciiz La suma de 0 a 100 es %d\n equivale a:.byte 76 97 32 115 117 109.byte 97 32 100 101 32 48 32.byte 97 32 49 48 48 32 101.byte 115 32 37 100 92 110
(Des)Ensamblar Código Assembler addi $t1, $t2, 0x1000 Ensamblar Desensamblar 0010 0001 0100 1001 0001 0000 0000 0000 Código Máquina
Memoria Unidad Aritmética y Lógica (ALU). Unidad Aritmética entera, operaciones de multiplicación y división. CPU Registros FPU Registros Alu Mul Div Hi Lo U. Aritmética Traps Manejo de Memoria Unidad punto flotante (FPU). Coprocesador dedicado al manejo de memoria caché y virtual.
Procesador MIPS
Procesador MIPS Nombre Registro Número Uso $zero $0 Constante 0 $at $1 Reservado para el assembler $v0 $2 Para evaluación de expresiones y $v1 $3 retorno de resultados de una función $a0 $4 Argumento 1 $a1 $5 Argumento 2 $a2 $6 Argumento 3 $a3 $7 Argumento 4 $t0 $8 Temporal (no se preserva a través de los llamados) $t1 $9 Temporal (no se preserva a través de los llamados) $t2 $10 Temporal (no se preserva a través de los llamados) $t3 $11 Temporal (no se preserva a través de los llamados) $t4 $12 Temporal (no se preserva a través de los llamados) $t5 $13 Temporal (no se preserva a través de los llamados) $t6 $14 Temporal (no se preserva a través de los llamados) $t7 $15 Temporal (no se preserva a través de los llamados)
Procesador MIPS Nombre Registro Número Uso $s0 $16 Temporal que debe preservarse entre llamados a funciones $s1 $17 Temporal que debe preservarse entre llamados a funciones $s2 $18 Temporal que debe preservarse entre llamados a funciones $s3 $19 Temporal que debe preservarse entre llamados a funciones $s4 $20 Temporal que debe preservarse entre llamados a funciones $s5 $21 Temporal que debe preservarse entre llamados a funciones $s6 $22 Temporal que debe preservarse entre llamados a funciones $s7 $23 Temporal que debe preservarse entre llamados a funciones $t8 $24 Temporal (no se preserva a través de los llamados) $t9 $25 Temporal (no se preserva a través de los llamados) $k0 $26 Reservado para el núcleo del Sist. Operativo $k1 $27 Reservado para el núcleo del Sist. Operativo $gp $28 Puntero al área global de datos $sp $29 Puntero al tope de la pila. Stack pointer $fp $30 Puntero a zona de variables en la pila. Frame pointer $ra $31 Dirección de retorno (usado en invocaciones a funciones)
Ciclo de ejecución Acceso a Memoria Tipo y tamaño de la Instrucción (estructura) Acceso a Memoria o a Registros Acceso a Memoria o a Registros Saltos condicionales, bifurcaciones, subrutinas...
Formato de Instrucciones Formato de instrucciones MIPS R I J Op (6) Rs (5) Rt (5) Rd (5) Shamnt (5) Funct (6) Op (6) Address (26) Op (6) Rs (5) Rt (5) Inmediate (16)
Formato de Instrucciones Formato de instrucciones MIPS R I J Op (6) Rs (5) Rt (5) Rd (5) Shamnt (5) Funct (6) Op (6) Address (26) Op (6) Rs (5) Rt (5) Inmediate (16)
Formato de Instrucciones Formato de instrucciones MIPS R I J Op (6) Rs (5) Rt (5) Rd (5) Shamnt (5) Funct (6) Op (6) Address (26) Op (6) Rs (5) Rt (5) Inmediate (16)
Formato de Instrucciones Formato de instrucciones MIPS R I J Op (6) Rs (5) Rt (5) Rd (5) Shamnt (5) Funct (6) Op (6) Address (26) Op (6) Rs (5) Rt (5) Inmediate (16)
Procesador MIPS Repertorio de Instrucciones Intrucción Ejemplo Significado add add $t1, $t2, $t3 $t1=$t2+$t3 add unsigned addu $t1, $t2, $t3 $t1=$t2+$t3 subtract sub $t1, $t2, $t3 $t1=$t2-$t3 Subtract unsigned subu $t1, $t2, $t3 $t1=$t2-$t3 and and $t1, $t2, $t3 $t1=$t2 & $t3 or or $t1, $t2, $t3 $t1=$t2 $t3 xor xor $t1, $t2, $t3 $t1=$t2 $t3 nor nor $t1, $t2, $t3 $t1=~($t2 $t3) set on less than slt $t1,$t2,$t3 if($t2<$t3) $t1=1; else $t0=0;
Repertorio de Instrucciones Intrucción Ejemplo Significado shift left logical sll $t1, $t2, K $t1 = $t2 << K shift left arithm. sla $t1, $t2, K $t1 = $t2 << K shift left logical variable sllv $t1, $t2, $t3 $t1 = $t2 << $t3
Repertorio de Instrucciones Intrucción Ejemplo Significado add inmediate addi $t1, $t2, K $t1 = $t2 + K add inmediate unsigned addiu $t1, $t2, K $t1 = $t2 + K and inmediate andi $t1, $t2, K $t1 = $t2 & $t3 or inmediate ori $t1, $t2, K $t1 = $t2 K xor inmediate xori $t1, $t2, K $t1 = $t2 K set less than inmediate slti $t1, $t2, K if($t2<k) $t1=1; else $t1=0;
Repertorio de Instrucciones Intrucción Ejemplo Significado load byte lb $t1, K($t2) $t1=m[$t2+k] load byte unsigned lbu $t1, K($t2) $t1=m[$t2+k] load halfword lh $t1, K($t2) $t1=m[$t2+k] load halfword unsigned lhu $t1, K($t2) $t1=m[$t2+k] load word lw $t1, K($t2) $t1=m[$t2+k] Store byte sb $t1, K($t2) M[$t2+K]=$t1 Store halfword sh $t1, K($t2) M[$t2+K]=$t1 Store word sw $t1, K($t2) M[$t2+K]=$t1
Repertorio de Instrucciones Intrucción Ejemplo Significado multiply mult $t2, $t3 Hi, Lo = $t2*$t3 multiply unsigned multu $t2, $t3 Hi, Lo = $t2*$t3 divide div $t2, $t3 divide unsigned divu $t2, $t3 Lo = [$t2 / $t3] Hi = $t2 % $t3 Lo = [$t2 / $t3] Hi = $t2 % $t3 Intrucción Ejemplo Significado Move from Hi mfhi $t1 $t1 = Hi Move from Lo mflo $t1 $t1 = Lo Move to Hi mthi $t1 Hi = $t1 Move to Lo mtlo $t1 Lo = $t1
Repertorio de Instrucciones Intrucción Ejemplo Significado branch on equal branch on not equal Comparaciones con 0 beq $t1, $t2, K bne $t1, $t2, K bgtz $t1, offset bltz $t1, offset bgez $t1, offset bltzal $t1, offset bgezal $t1, offset if($t1==$t2) PC=PC+4+K 4; if($t1!=$t2) PC=PC+4+K 4; if($t1>0) PC=PC+4+offset 4; if($t1<0) PC=PC+4+offset 4; if($t1>=0) PC=PC+4+offset 4; if($t1<0){ $ra = PC +4; PC=PC+4+offset 4; } if($t1>=0){ $ra = PC +4; PC=PC+4+offset 4; }
Repertorio de Instrucciones Intrucción Ejemplo Significado Jump j label goto label Jump and link jal label $ra= PC+4; goto label; Jump register jr $t1 PC=$t1 Jump and link register jalr $t1 $ra= PC+4; PC=$t1;
SEGMENTO ESTÁTICO DE DATOS
Segmento Estático de Datos: Directivas Assembler 0x64636261
Manejo de Números de 4[Bytes] 0x64636261 Op (6) Rs (5) Rt (5) Inmediate (16)
Segmento Estático de Datos: Directivas Assembler 0x64636261 lui $t1,0x6463 ori $t1, $t1, 0x6261
Segmento Estático de Datos: Directivas Assembler 0x64636261 lui $t1,0x6463 ori $t1, $t1, 0x6261 Macro li $t1,0x64636261 Ver página A-59 del Apéndice A (pdf)
Segmento Estático de Datos: Directivas Assembler 0x12340261 la $t1,0x12340261 lui $t1,0x1234 ori $t1, $t1, 0x0261
Segmento Estático de Datos
Segmento Estático de Datos: Directivas Assembler
Segmento Estático de Datos: Directivas Assembler.data 0x10010000 #variables estaticas v1:.word 0x10001000 v2:.word 0x20002000 v3:.word 0x30003000 v4:.word 0x40004000 v5:.word 0x50005001 v6:.word 0x60006000 v7:.half 0x1234 v8:.byte 0xFF.align 2 arr:.word 0,1,2,3,4,5 string:.asciiz "abcdef" Data Segment [0x10010000] 0x10001000 [0x10010004] 0x20002000 [0x10010008] 0x30003000 [0x1001000C] 0x40004000 [0x10010010] 0x50005001 [0x10010014] 0x60006000 [0x10010018] 0x00ff1234 [0x1001001C] 0x00000000 [0x10010020] 0x00000001 [0x10010024] 0x00000002 [0x10010028] 0x00000003 [0x1001002C] 0x00000004 [0x10010030] 0x00000005 [0x10010030] 0x64636261 [0x10010030] 0x00006665 [0x10010030] 0x00000000
Segmento Estático de Datos: Directivas Assembler Notación del segmento de datos Data Segment [0x10010000] 0x10001000 [0x10010004] 0x20002000 [0x10010008] 0x30003000 [0x1001000C] 0x40004000 [0x10010010] 0x50005001 [0x10010014] 0x60006000 [0x10010018] 0x00ff1234 [0x1001001C] 0x00000000 [0x10010020] 0x00000001 [0x10010024] 0x00000002 [0x10010028] 0x00000003 [0x1001002C] 0x00000004 [0x10010030] 0x00000005 [0x10010030] 0x64636261 [0x10010030] 0x00006665 [0x10010030] 0x00000000 [0x10010000] 0x10001000 0x20002000 0x30003000 0x40004000 [0x10010010] 0x50005001 0x60006000 0x00ff1234 0x00000000 [0x10010020] 0x00000001 0x00000002 0x00000003 0x00000004 [0x10010030] 0x00000005 0x64636261 0x00006665 0x00000000
Segmento Estático de Datos: Directivas Assembler 0x10010010 y 0x10010008 Data Segment [0x10010000] 0x10001000 [0x10010004] 0x20002000 [0x10010008] 0x30003000 [0x1001000C] 0x40004000 [0x10010010] 0x50005001 [0x10010014] 0x60006000 [0x10010018] 0x00ff1234 [0x1001001C] 0x00000000 [0x10010020] 0x00000001 [0x10010024] 0x00000002 [0x10010028] 0x00000003 [0x1001002C] 0x00000004 [0x10010030] 0x00000005 [0x10010030] 0x64636261 [0x10010030] 0x00006665 [0x10010030] 0x00000000
Segmento Estático de Datos: Directivas Assembler la $t1, v1 lw $t2, 8($t1) lw $t3, 16($t1) Data Segment [0x10010000] 0x10001000 [0x10010004] 0x20002000 [0x10010008] 0x30003000 [0x1001000C] 0x40004000 [0x10010010] 0x50005001 [0x10010014] 0x60006000 [0x10010018] 0x00ff1234 [0x1001001C] 0x00000000 [0x10010020] 0x00000001 [0x10010024] 0x00000002 [0x10010028] 0x00000003 [0x1001002C] 0x00000004 [0x10010030] 0x00000005 [0x10010030] 0x64636261 [0x10010030] 0x00006665 [0x10010030] 0x00000000 Otra(s) opción(es)?
Segmento Estático de Datos: Directivas Assembler... v7:.half 0x1234 v8:.byte 0xFF... Data Segment [0x10010000] 0x10001000 [0x10010004] 0x20002000 [0x10010008] 0x30003000 [0x1001000C] 0x40004000 [0x10010010] 0x50005001 [0x10010014] 0x60006000 [0x10010018] 0x00ff1234 [0x1001001C] 0x00000000 [0x10010020] 0x00000001 [0x10010024] 0x00000002 [0x10010028] 0x00000003 [0x1001002C] 0x00000004 [0x10010030] 0x00000005 [0x10010030] 0x64636261 [0x10010030] 0x00006665 [0x10010030] 0x00000000
Segmento Estático de Datos: Directivas Assembler... v7:.half 0x1234 v8:.byte 0xFF... la $t4, v7 lh $t5, 0($t4) lb $t6, 2($t4) Data Segment [0x10010000] 0x10001000 [0x10010004] 0x20002000 [0x10010008] 0x30003000 [0x1001000C] 0x40004000 [0x10010010] 0x50005001 [0x10010014] 0x60006000 [0x10010018] 0x00ff1234 [0x1001001C] 0x00000000 [0x10010020] 0x00000001 [0x10010024] 0x00000002 [0x10010028] 0x00000003 [0x1001002C] 0x00000004 [0x10010030] 0x00000005 [0x10010030] 0x64636261 [0x10010030] 0x00006665 [0x10010030] 0x00000000 Otra(s) opción(es)?
Segmento Estático de Datos: Directivas Assembler Notación Significado t8 = &var; la $t8, var *p contenido &p dirección *t8 = t7; sw $t7, 0($t8) Memoria &var var contenido *t8
Invocación de funciones: Variables Globales int i=10, j=5, k=0; // var. Globales void f1(void){ k = i + j; } main(void){... f1(); //invoca función f1 sin argumentos... }
Invocación de funciones: Variables Globales.data i:.word 10 j:.word 5 k:.word 0.text.globl main main:... jal f1 f1: la $t0, i lw $s1, 0($t0) # s1 = i lw $s2, 4($t0) # s2 = j add $t1, $s1, $s2 # t1 = i + j sw $t1, 8($t0) # k = t1 jr $ra
Invocación de funciones: Por Valor int i=10, j=20, k=30; int f2(int a0, int a1) { return (a0+a1); } main(){... k = f2(i, j);... }
Invocación de funciones: Por Valor.data i:.word 1 j:.word 2 k:.word 3.text.globl main:... la $t0, i main lw $a0,0($t0) lw $a1,4($t0) jal f2 sw $v0,8($t0)... f2: add $v0, $a0, $a1 jr $ra # a0 = i # a1 = j # invocación de función # k = v0
Invocación de funciones: Por Referencia int i=1, j=2, k=3; int f3(int *a0, int *a1) return (*a0+*a1); } main(){... k= f3(&i,&j);... }
.data i:.word 1 j:.word 2 k:.word 3.text.globl main main:... la $a0,i la $a1,j jal f3 sw $v0, 8($a0)... j main Procesador MIPS Invocación de funciones: Por Referencia f3: lw $t1,0($a0) lw $t2,0($a1) add $v0,$t1,$t2 jr ra
Stack
Stack
Procesador MIPS Stack : Push # PUSH the item in $t0: subu $sp,$sp,4 #point to the place #for the new item, sw $t0,($sp) #store the contents of $t0 #as the new top
Procesador MIPS Stack : Push # POP the item into $t0: lw $t0,($sp) #Copy top the item to $t0. addu $sp,$sp,4 #Point to the item beneath #the old top.
Stack : Push.data a:.word 2 b:.word 3.text main: lw lw mul $t0,a $t1,bb $t0,$t0,$t1 subu sw... $sp,$sp, $t0,
Stack : Push.data a:.word 2 b:.word 3.text main: lw lw mul $t0,a $t1,bb $t0,$t0,$t1 subu sw... $sp,$sp,4 $t0,0($sp)
Stack : Push.data a:.word 2 b:.word 3.globl main main: lw $t0,a # get a lw $t1,bb # get b mul $t0,$t0,$t1 # a*b subu $sp,$sp,4 # push a*b sw $t0,($sp) lw $t0,a # get a li $t1,-12 # mul $t0,$t0,$t1 # -12a subu $sp,$sp,4 # push -12a sw $t0,($sp) li $t1,-7 # init sum to -7 lw $t0,($sp) # pop 18b addu $sp,$sp,4 addu $t1,$t1,$t0 # 18b-7 lw $t0,($sp) # pop -12a addu $sp,$sp,4 addu $t1,$t1,$t0 # -12a+18b-7 lw $t0,($sp) # pop ab addu $sp,$sp,4 addu $t1,$t1,$t0 # ab-12a+18b-7 done: li $v0,1 # print sum move $a0,$t1 syscall li $v0,10 # exit syscall lw $t0,b # get b li $t1,18 # mul $t0,$t0,$t1 # 18b subu $sp,$sp,4 # push 18b sw $t0,($sp)