Commit 29991ae2 authored by Franco Marziano's avatar Franco Marziano
Browse files

talleres

parent a1f69dd0
File added
File added
NASM:=nasm
NASMFLAGS:=-Wall -f elf64 -g -F DWARF -Wall
SRCS:= sumador.asm \
print_uint64.asm
TARGET:=sumador
OBJS=$(SRCS:.asm=.o)
all: $(TARGET)
%.o: %.asm
$(NASM) $(NASMFLAGS) $< -o $@
$(TARGET): $(OBJS)
ld -o $@ $^
clean:
rm -f *.o
rm -f sumador
\ No newline at end of file
global print_uint64
; Lógica para poder imprimir uints de 64 bits
table:
db '0123456789ABCDEF'
UINT64_STRING_SIZE EQU 20
print_uint64:
sub rsp, UINT64_STRING_SIZE
; Máximo offset desde el comienzo del string
mov rax, UINT64_STRING_SIZE - 1
mov BYTE [rsp+rax], 0
dec rax
mov BYTE [rsp+rax], 10 ; \n
dec rax
mov rcx, 16
.loop:
mov rsi, rdi
and rsi, 0b1111
mov sil, [table+rsi]
mov [rsp+rax], sil
dec rax
shr rdi, 4
loop .loop
mov BYTE [rsp+rax], 'x'
dec rax
mov BYTE [rsp+rax], '0'
dec rax
mov rax, 1
mov rdi, 1
mov rsi, rsp
mov rdx, 20
syscall
add rsp, UINT64_STRING_SIZE
ret
\ No newline at end of file
extern print_uint64
section .data
section .text
global _start
_start:
mov rdi, 4
call print_uint64
mov eax, 1
int 0x80
\ No newline at end of file
File added
CC=cc
CFLAGS= -std=c11 -Wall -Wextra -pedantic -O0 -g -lm -Wno-unused-variable -Wno-unused-parameter -no-pie
NASM=nasm
NASMFLAGS=-f elf64 -g -F DWARF
all: main tester
OBJS := checkpoint2_c.o checkpoint2_asm.o checkpoint3_c.o checkpoint3_asm.o checkpoint4_c.o checkpoint4_asm.o
tester: tester.c $(OBJS)
$(CC) $(CFLAGS) $^ -o $@
main: main.c $(OBJS)
$(CC) $(CFLAGS) $^ -o $@
tester.o: tester.c checkpoints.h test-utils.h
$(CC) $(CFLAGS) -c $< -o $@
checkpoint2_c.o: checkpoint2.c checkpoints.h
$(CC) $(CFLAGS) -c $< -o $@
checkpoint3_c.o: checkpoint3.c checkpoints.h
$(CC) $(CFLAGS) -c $< -o $@
checkpoint4_c.o: checkpoint4.c checkpoints.h
$(CC) $(CFLAGS) -c $< -o $@
checkpoint2_asm.o: checkpoint2.asm
$(NASM) $(NASMFLAGS) $< -o $@
checkpoint3_asm.o: checkpoint3.asm
$(NASM) $(NASMFLAGS) $< -o $@
checkpoint4_asm.o: checkpoint4.asm
$(NASM) $(NASMFLAGS) $< -o $@
clean:
rm -f *.o
rm -f main tester
rm -f salida.propios.*
extern sumar_c
extern restar_c
;########### SECCION DE DATOS
section .data
;########### SECCION DE TEXTO (PROGRAMA)
section .text
;########### LISTA DE FUNCIONES EXPORTADAS
global alternate_sum_4
global alternate_sum_4_simplified
global alternate_sum_8
global product_2_f
global product_9_f
global alternate_sum_4_using_c
;########### DEFINICION DE FUNCIONES
; uint32_t alternate_sum_4(uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4);
; registros: x1[?], x2[?], x3[?], x4[?]
alternate_sum_4:
;prologo
push rbp
mov rbp, rsp
;recordar que si la pila estaba alineada a 16 al hacer la llamada
;con el push de RIP como efecto del CALL queda alineada a 8
mov rax, rdi
sub rax, rsi
add rax, rdx
sub rax, rcx
;epilogo
pop rbp
ret
; uint32_t alternate_sum_4_using_c(uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4);
; registros: x1[rdi], x2[rsi], x3[rdx], x4[rcx]
alternate_sum_4_using_c:
;prologo
push rbp ; alineado a 16
mov rbp,rsp
push r12
push r13
mov r12, rdx
mov r13, rcx
call restar_c
mov rdi, rax
mov rsi, r12
call sumar_c
mov rdi, rax
mov rsi, r13
call restar_c
;epilogo
pop r13
pop r12
pop rbp
ret
; uint32_t alternate_sum_4_simplified(uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4);
; registros: x1[?], x2[?], x3[?], x4[?]
alternate_sum_4_simplified:
mov rax, rdi
sub rax, rsi
add rax, rdx
sub rax, rcx
ret
; uint32_t alternate_sum_8(uint32_t x1, uint32_t x2, uint32_t x3, uint32_t x4, uint32_t x5, uint32_t x6, uint32_t x7, uint32_t x8);
; registros y pila: x1[?], x2[?], x3[?], x4[?], x5[?], x6[?], x7[?], x8[?]
alternate_sum_8:
;prologo
push rbp
mov rbp, rsp
xor rax, rax
mov rax, rdi
sub rax, rsi
add rax, rdx
sub rax, rcx
add rax, r8
sub rax, r9
add rax, [rbp + 0x10]
sub rax, [rbp + 0x18]
;epilogo
pop rbp
ret
; SUGERENCIA: investigar uso de instrucciones para convertir enteros a floats y viceversa
;void product_2_f(uint32_t * destination, uint32_t x1, float f1);
;registros: destination[?], x1[?], f1[?]
product_2_f:
; prologo
push rbp
mov rbp, rsp
cvtsi2sd xmm1, rsi ; Convierto el int a float de 64 bits
cvtps2pd xmm0, xmm0 ; Convierto el float de 32 bits a float de 64 bits
mulsd xmm0, xmm1 ; Multiplico y como el resultado es un float, lo guarda en xmm0.
cvttsd2si r8, xmm0 ; Trunco el resultado(lo convierto a entero de 64)
mov [rdi], r8d ; r8d son los 32 bits menos significativos del r8 (es decir, del 0 al 31)
; epilogo
pop rbp
ret
;extern void product_9_f(uint32_t * destination
;, uint32_t x1, float f1, uint32_t x2, float f2, uint32_t x3, float f3, uint32_t x4, float f4
;, uint32_t x5, float f5, uint32_t x6, float f6, uint32_t x7, float f7, uint32_t x8, float f8
;, uint32_t x9, float f9);
;registros y pila: destination[rdi], x1[?], f1[?], x2[?], f2[?], x3[?], f3[?], x4[?], f4[?]
; , x5[?], f5[?], x6[?], f6[?], x7[?], f7[?], x8[?], f8[?],
; , x9[?], f9[?]
product_9_f:
;prologo
push rbp
mov rbp, rsp
;convertimos los flotantes de cada registro xmm en doubles
cvtss2sd xmm0, xmm0
cvtss2sd xmm1, xmm1
cvtss2sd xmm2, xmm2
cvtss2sd xmm3, xmm3
cvtss2sd xmm4, xmm4
cvtss2sd xmm5, xmm5
cvtss2sd xmm6, xmm6
cvtss2sd xmm7, xmm7
cvtss2sd xmm8, [rbp + 0x30]
;multiplicamos los doubles en xmm0 <- xmm0 * xmm1, xmmo * xmm2 , ...
mulsd xmm0, xmm1
mulsd xmm0, xmm2
mulsd xmm0, xmm3
mulsd xmm0, xmm4
mulsd xmm0, xmm5
mulsd xmm0, xmm6
mulsd xmm0, xmm7
mulsd xmm0, xmm8
; convertimos los enteros en doubles y los multiplicamos por xmm0.
cvtsi2sd xmm1, rsi
mulsd xmm0, xmm1
cvtsi2sd xmm2, rdx
mulsd xmm0, xmm2
cvtsi2sd xmm3, rcx
mulsd xmm0, xmm3
cvtsi2sd xmm4, r8
mulsd xmm0, xmm4
cvtsi2sd xmm5, r9
mulsd xmm0, xmm5
cvtsi2sd xmm6, [rbp + 0x10]
mulsd xmm0, xmm6
cvtsi2sd xmm7, [rbp + 0x18]
mulsd xmm0, xmm7
cvtsi2sd xmm8, [rbp + 0x20]
mulsd xmm0, xmm8
cvtsi2sd xmm9, [rbp + 0x28]
mulsd xmm0, xmm9
; retornamos la respuesta
movq [rdi], xmm0
; epilogo
pop rbp
ret
#include "checkpoints.h"
uint32_t sumar_c(uint32_t a,uint32_t b){
return a + b;
}
uint32_t restar_c(uint32_t a,uint32_t b){
return a - b;
}
/* Pueden programar alguna rutina auxiliar del checkpoint 2 acá */
;########### ESTOS SON LOS OFFSETS Y TAMAÑO DE LOS STRUCTS
; Completar:
NODO_LENGTH EQU 32
LONGITUD_OFFSET EQU 24
PACKED_NODO_LENGTH EQU 21
PACKED_LONGITUD_OFFSET EQU 17
;########### SECCION DE DATOS
section .data
;########### SECCION DE TEXTO (PROGRAMA)
section .text
;########### LISTA DE FUNCIONES EXPORTADAS
global cantidad_total_de_elementos
global cantidad_total_de_elementos_packed
;########### DEFINICION DE FUNCIONES
;extern uint32_t cantidad_total_de_elementos(lista_t* lista);
;registros: lista[?]
cantidad_total_de_elementos:
push rbp
mov rbp, rsp
xor rax, rax
mov rdi, [rdi]
while:
cmp rdi, 0x0
je fin
add eax, dword [rdi + LONGITUD_OFFSET] ; uso eax porque longitud es uint32_t
mov rdi, [rdi]
jmp while
fin:
pop rbp
ret
;extern uint32_t cantidad_total_de_elementos_packed(packed_lista_t* lista);
;registros: lista[?]
cantidad_total_de_elementos_packed:
push rbp
mov rbp, rsp
xor rax, rax
mov rdi, [rdi]
while_packed:
cmp rdi, 0x0
je fin_packed
add eax, dword [rdi + PACKED_LONGITUD_OFFSET] ; uso eax porque longitud es uint32_t
mov rdi, [rdi]
jmp while_packed
fin_packed:
pop rbp
ret
#include "checkpoints.h"
/* Pueden programar alguna rutina auxiliar del checkpoint 3 acá */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment