From 8068505761a990e7000b4f825351fc2f97351006 Mon Sep 17 00:00:00 2001 From: Gabriel Mocanu Date: Fri, 24 Dec 2021 17:51:18 +0200 Subject: [PATCH] Add demos for lab08. Add demos for lab08. Signed-off-by: Gabriel Mocanu --- laborator/content/stiva/0-mean/mean.asm | 2 +- laborator/content/stiva/1-max/max.asm | 2 +- .../content/stiva/2-reverse/reverse-array.asm | 2 +- .../3-stack-addressing/stack-addressing.asm | 2 +- .../stiva/4-local-var/merge-arrays.asm | 2 +- laborator/content/stiva/5-gcd/gcd.asm | 2 +- .../stiva/demo/stack-addressing/Makefile | 2 + .../stack-addressing/stack-addressing.asm | 29 ++++++++++++ .../stiva/demo/stack-operations/Makefile | 2 + .../stack-operations/stack-operations.asm | 45 +++++++++++++++++++ laborator/content/stiva/demo/sum-n/.gitignore | 3 -- laborator/content/stiva/demo/sum-n/Makefile | 21 +-------- laborator/content/stiva/demo/sum-n/sum_n.asm | 30 ++++++------- .../content/stiva/demo/sum-n/sum_n_init.asm | 32 ------------- .../content/stiva/demo/sum-n/sum_n_interm.asm | 26 ----------- .../content/stiva/demo/sum_n_init/Makefile | 2 + .../stiva/demo/sum_n_init/sum_n_init.asm | 32 +++++++++++++ .../content/stiva/demo/sum_n_interm/Makefile | 2 + .../stiva/demo/sum_n_interm/sum_n_interm.asm | 26 +++++++++++ laborator/content/stiva/utils/printf32.asm | 18 -------- 20 files changed, 163 insertions(+), 119 deletions(-) create mode 100644 laborator/content/stiva/demo/stack-addressing/Makefile create mode 100644 laborator/content/stiva/demo/stack-addressing/stack-addressing.asm create mode 100644 laborator/content/stiva/demo/stack-operations/Makefile create mode 100644 laborator/content/stiva/demo/stack-operations/stack-operations.asm delete mode 100644 laborator/content/stiva/demo/sum-n/.gitignore delete mode 100644 laborator/content/stiva/demo/sum-n/sum_n_init.asm delete mode 100644 laborator/content/stiva/demo/sum-n/sum_n_interm.asm create mode 100644 laborator/content/stiva/demo/sum_n_init/Makefile create mode 100644 laborator/content/stiva/demo/sum_n_init/sum_n_init.asm create mode 100644 laborator/content/stiva/demo/sum_n_interm/Makefile create mode 100644 laborator/content/stiva/demo/sum_n_interm/sum_n_interm.asm delete mode 100644 laborator/content/stiva/utils/printf32.asm diff --git a/laborator/content/stiva/0-mean/mean.asm b/laborator/content/stiva/0-mean/mean.asm index 00c3e18d..4ca0498c 100644 --- a/laborator/content/stiva/0-mean/mean.asm +++ b/laborator/content/stiva/0-mean/mean.asm @@ -1,4 +1,4 @@ -%include "../utils/printf32.asm" +%include "printf32.asm" %define ARRAY_SIZE 13 %define DECIMAL_PLACES 5 diff --git a/laborator/content/stiva/1-max/max.asm b/laborator/content/stiva/1-max/max.asm index b814be93..1e6cdc7c 100644 --- a/laborator/content/stiva/1-max/max.asm +++ b/laborator/content/stiva/1-max/max.asm @@ -1,4 +1,4 @@ -%include "../utils/printf32.asm" +%include "printf32.asm" section .text diff --git a/laborator/content/stiva/2-reverse/reverse-array.asm b/laborator/content/stiva/2-reverse/reverse-array.asm index 711f9637..8319189b 100644 --- a/laborator/content/stiva/2-reverse/reverse-array.asm +++ b/laborator/content/stiva/2-reverse/reverse-array.asm @@ -1,4 +1,4 @@ -%include "../utils/printf32.asm" +%include "printf32.asm" %define ARRAY_LEN 7 diff --git a/laborator/content/stiva/3-stack-addressing/stack-addressing.asm b/laborator/content/stiva/3-stack-addressing/stack-addressing.asm index 0e0ed81b..1af42912 100644 --- a/laborator/content/stiva/3-stack-addressing/stack-addressing.asm +++ b/laborator/content/stiva/3-stack-addressing/stack-addressing.asm @@ -1,4 +1,4 @@ -%include "../utils/printf32.asm" +%include "printf32.asm" %define NUM 5 diff --git a/laborator/content/stiva/4-local-var/merge-arrays.asm b/laborator/content/stiva/4-local-var/merge-arrays.asm index 5c88e4bc..ea8effb7 100644 --- a/laborator/content/stiva/4-local-var/merge-arrays.asm +++ b/laborator/content/stiva/4-local-var/merge-arrays.asm @@ -1,4 +1,4 @@ -%include "../utils/printf32.asm" +%include "printf32.asm" %define ARRAY_1_LEN 5 %define ARRAY_2_LEN 7 diff --git a/laborator/content/stiva/5-gcd/gcd.asm b/laborator/content/stiva/5-gcd/gcd.asm index 03b9b6e3..6a1b8933 100644 --- a/laborator/content/stiva/5-gcd/gcd.asm +++ b/laborator/content/stiva/5-gcd/gcd.asm @@ -1,4 +1,4 @@ -%include "../utils/printf32.asm" +%include "printf32.asm" section .text diff --git a/laborator/content/stiva/demo/stack-addressing/Makefile b/laborator/content/stiva/demo/stack-addressing/Makefile new file mode 100644 index 00000000..3f3e1c3c --- /dev/null +++ b/laborator/content/stiva/demo/stack-addressing/Makefile @@ -0,0 +1,2 @@ +PROGNAME := stack-addressing +include ../../../utils/Makefile.generic diff --git a/laborator/content/stiva/demo/stack-addressing/stack-addressing.asm b/laborator/content/stiva/demo/stack-addressing/stack-addressing.asm new file mode 100644 index 00000000..a8c1ef9f --- /dev/null +++ b/laborator/content/stiva/demo/stack-addressing/stack-addressing.asm @@ -0,0 +1,29 @@ +%include "printf32.asm" + +section .text + +extern printf +global main +main: + push ebp + mov ebp, esp + + push dword 10 + push dword 11 + push dword 12 + push dword 13 + + mov eax, ebp +print_stack: + PRINTF32 `0x\x0` + PRINTF32 `%x\x0`, eax + PRINTF32 `: 0x\x0` + PRINTF32 `%x\n\x0`, [eax] + + sub eax, 4 + cmp eax, esp + jge print_stack + + xor eax, eax + leave + ret diff --git a/laborator/content/stiva/demo/stack-operations/Makefile b/laborator/content/stiva/demo/stack-operations/Makefile new file mode 100644 index 00000000..f60c8b51 --- /dev/null +++ b/laborator/content/stiva/demo/stack-operations/Makefile @@ -0,0 +1,2 @@ +PROGNAME := stack-operations +include ../../../utils/Makefile.generic diff --git a/laborator/content/stiva/demo/stack-operations/stack-operations.asm b/laborator/content/stiva/demo/stack-operations/stack-operations.asm new file mode 100644 index 00000000..e8265d84 --- /dev/null +++ b/laborator/content/stiva/demo/stack-operations/stack-operations.asm @@ -0,0 +1,45 @@ +%include "printf32.asm" + +section .text + +; esp -> stack pointer +; ebp -> base pointer + +extern printf +global main +main: + push ebp + mov ebp, esp + + push dword 10 ; sub esp, 4; mov [esp], 10; + push dword 11 ; sub esp, 4; mov [esp], 11; + push dword 12 ; sub esp, 4; mov [esp], 12; + push dword 13 ; sub esp, 4; mov [esp], 13; + push dword 14 ; sub esp, 4; mov [esp], 13; + + + pusha ; push all registers on the stack + popa ; pop all registers from the stack + + ; Version 1 + pop eax; ; mov eax, [esp]; add esp, 4 + pop eax; ; mov eax, [esp]; add esp, 4 + pop eax; ; mov eax, [esp]; add esp, 4 + pop eax; ; mov eax, [esp]; add esp, 4 + pop eax; ; mov eax, [esp]; add esp, 4 + + ; Version 2 + ; add esp, 20 ; 4 * number_of_push + + ; Version 3 + ; mov esp, ebp + + ; sub esp <-> add esp -> use to allocate/deallocate memory + + ; Aloc 8 bytes <-> 2 int + ; sub esp, 8 + ; mov [esp], 10 + ; mov [esp + 4], 12 + + leave + ret diff --git a/laborator/content/stiva/demo/sum-n/.gitignore b/laborator/content/stiva/demo/sum-n/.gitignore deleted file mode 100644 index 781ef022..00000000 --- a/laborator/content/stiva/demo/sum-n/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/sum_n_init -/sum_n_interm -/sum_n diff --git a/laborator/content/stiva/demo/sum-n/Makefile b/laborator/content/stiva/demo/sum-n/Makefile index 8f7c9efb..858978e4 100644 --- a/laborator/content/stiva/demo/sum-n/Makefile +++ b/laborator/content/stiva/demo/sum-n/Makefile @@ -1,19 +1,2 @@ -NASM = nasm -AFILES = sum_n_init.asm sum_n_interm.asm sum_n.asm -OBJS = $(AFILES:.asm=.o) -ASM_FLAGS = -f elf32 -g -LD=gcc -LDFLAGS = -m32 -g -BINARIES = $(OBJS:.o=) - -all: $(BINARIES) - -%.o: %.asm - $(NASM) $(ASM_FLAGS) -o $@ $< - -mean: mean.o - $(LD) $(LDFLAGS) -o $@ $^ - -clean: - rm -f *.o $(BINARIES) *.s - rm -f *~ +PROGNAME := sum_n +include ../../../utils/Makefile.generic diff --git a/laborator/content/stiva/demo/sum-n/sum_n.asm b/laborator/content/stiva/demo/sum-n/sum_n.asm index 76332b8b..e5ab8f3c 100644 --- a/laborator/content/stiva/demo/sum-n/sum_n.asm +++ b/laborator/content/stiva/demo/sum-n/sum_n.asm @@ -1,25 +1,25 @@ -%include "../utils/printf32.asm" +%include "printf32.asm" section .text extern printf global main main: - push ebp - mov ebp, esp + push ebp + mov ebp, esp - ; ecx <- i (number / increment) - ; eax <- sum - ; init i (i <- 100) - mov ecx, 100 - ; init sum (sum <- 0) - xor eax, eax ; mov eax, 0 + ; ecx <- i (number / increment) + ; eax <- sum + ; init i (i <- 100) + mov ecx, 100 + ; init sum (sum <- 0) + xor eax, eax ; mov eax, 0 .L3: - add eax, ecx - loop .L3 ; dec ecx; cmp ecx, 0; jne .L3 + add eax, ecx + loop .L3 ; dec ecx; cmp ecx, 0; jne .L3 - PRINTF32 `%u\n\x0`, eax - mov eax, 0 - pop ebp - ret + PRINTF32 `%u\n\x0`, eax + mov eax, 0 + pop ebp + ret diff --git a/laborator/content/stiva/demo/sum-n/sum_n_init.asm b/laborator/content/stiva/demo/sum-n/sum_n_init.asm deleted file mode 100644 index ebba628f..00000000 --- a/laborator/content/stiva/demo/sum-n/sum_n_init.asm +++ /dev/null @@ -1,32 +0,0 @@ -%include "../utils/printf32.asm" - -section .data - i: dd 0 - sum: dd 0 - -section .text -extern printf -global main - -main: - push ebp - mov ebp, esp - mov dword [i], 1 -.L3: - mov eax, [i] - cmp eax, 100 - ja .L2 - mov edx, [sum] - mov eax, [i] - add eax, edx - mov [sum], eax -; mov eax, [i] -; add eax, 1 -; mov [i], eax - inc dword [i] - jmp .L3 -.L2: - PRINTF32 `%u\n\x0`, dword [sum] - mov eax, 0 - pop ebp - ret diff --git a/laborator/content/stiva/demo/sum-n/sum_n_interm.asm b/laborator/content/stiva/demo/sum-n/sum_n_interm.asm deleted file mode 100644 index 54a1f877..00000000 --- a/laborator/content/stiva/demo/sum-n/sum_n_interm.asm +++ /dev/null @@ -1,26 +0,0 @@ -%include "../utils/printf32.asm" - -section .data - i: dd 0 - sum: dd 0 - -section .text -extern printf -global main - -main: - push ebp - mov ebp, esp - mov dword [i], 1 -.L3: - mov eax, [i] - cmp eax, 100 - ja .L2 - add [sum], eax - inc dword [i] - jmp .L3 -.L2: - PRINTF32 `%u\n\x0`, dword [sum] - mov eax, 0 - pop ebp - ret diff --git a/laborator/content/stiva/demo/sum_n_init/Makefile b/laborator/content/stiva/demo/sum_n_init/Makefile new file mode 100644 index 00000000..c3dd7fe8 --- /dev/null +++ b/laborator/content/stiva/demo/sum_n_init/Makefile @@ -0,0 +1,2 @@ +PROGNAME := sum_n_init +include ../../../utils/Makefile.generic diff --git a/laborator/content/stiva/demo/sum_n_init/sum_n_init.asm b/laborator/content/stiva/demo/sum_n_init/sum_n_init.asm new file mode 100644 index 00000000..e00f64e8 --- /dev/null +++ b/laborator/content/stiva/demo/sum_n_init/sum_n_init.asm @@ -0,0 +1,32 @@ +%include "printf32.asm" + +section .data + i: dd 0 + sum: dd 0 + +section .text +extern printf +global main + +main: + push ebp + mov ebp, esp + mov dword [i], 1 +.L3: + mov eax, [i] + cmp eax, 100 + ja .L2 + mov edx, [sum] + mov eax, [i] + add eax, edx + mov [sum], eax +; mov eax, [i] +; add eax, 1 +; mov [i], eax + inc dword [i] + jmp .L3 +.L2: + PRINTF32 `%u\n\x0`, dword [sum] + mov eax, 0 + pop ebp + ret diff --git a/laborator/content/stiva/demo/sum_n_interm/Makefile b/laborator/content/stiva/demo/sum_n_interm/Makefile new file mode 100644 index 00000000..ec9e1c8e --- /dev/null +++ b/laborator/content/stiva/demo/sum_n_interm/Makefile @@ -0,0 +1,2 @@ +PROGNAME := sum_n_interm +include ../../../utils/Makefile.generic diff --git a/laborator/content/stiva/demo/sum_n_interm/sum_n_interm.asm b/laborator/content/stiva/demo/sum_n_interm/sum_n_interm.asm new file mode 100644 index 00000000..a0b7b361 --- /dev/null +++ b/laborator/content/stiva/demo/sum_n_interm/sum_n_interm.asm @@ -0,0 +1,26 @@ +%include "printf32.asm" + +section .data + i: dd 0 + sum: dd 0 + +section .text +extern printf +global main + +main: + push ebp + mov ebp, esp + mov dword [i], 1 +.L3: + mov eax, [i] + cmp eax, 100 + ja .L2 + add [sum], eax + inc dword [i] + jmp .L3 +.L2: + PRINTF32 `%u\n\x0`, dword [sum] + mov eax, 0 + pop ebp + ret diff --git a/laborator/content/stiva/utils/printf32.asm b/laborator/content/stiva/utils/printf32.asm deleted file mode 100644 index 59389eb1..00000000 --- a/laborator/content/stiva/utils/printf32.asm +++ /dev/null @@ -1,18 +0,0 @@ -;;;;;;;;;;;;;;;;;;;;;;;;;;;; -%macro PRINTF32 1-* - pushf - pushad - jmp %%endstr -%%str: db %1 -%%endstr: -%rep %0 - 1 -%rotate -1 - push dword %1 -%endrep - push %%str - call printf - add esp, 4*%0 - popad - popf -%endmacro -;;;;;;;;;;;;;;;;;;;;;;;;;;;;