diff --git a/TP/TP3/Makefile b/TP/TP3/Makefile new file mode 100644 index 0000000..34a4735 --- /dev/null +++ b/TP/TP3/Makefile @@ -0,0 +1,13 @@ +88PATH = ../../TD/TD10/88/bin + +asm: main.asm + nasm -f elf32 -o main.o main.asm + ld -m elf_i386 -o main main.o + rm -f main.o + +exec-88: main.s + $(88PATH)/as88 main.s || echo error + $(88PATH)/s88 main.s + +clean: + rm -f main main.# main.$$ main.88 main.o \ No newline at end of file diff --git a/TP/TP3/README.md b/TP/TP3/README.md new file mode 100644 index 0000000..b05c719 --- /dev/null +++ b/TP/TP3/README.md @@ -0,0 +1,23 @@ +# Test de primalité en assembleur (x86 et 8088) + +Les entrées sont dans les fichiers de code, à modifier avant la compilation (avec la valeur par défaut 37). + +## Compilation + +Pour compiler pour 8088, et exécuter directement (sans l'interpréteur interactif): + +```bash +make exec-88 +``` + +Pour compiler pour x86: +```bash +make asm +``` + +Cela demande `nasm` d'installé et fournit un exécutable `main` + +Pour nettoyer le projet: +```bash +make clean +``` \ No newline at end of file diff --git a/TP/TP3/main.asm b/TP/TP3/main.asm new file mode 100644 index 0000000..e638347 --- /dev/null +++ b/TP/TP3/main.asm @@ -0,0 +1,56 @@ +; This programs checks if a number n>1 is prime +; it checks for divisors from 2 to n-1, but does not terminate if n=1 +section .data + is_prime db "prime" + is_len equ $ - is_prime + not_prime db "not prime" + not_len equ $ - not_prime + + +section .text +global _start +_start: + mov ebx, 37 ; hardcoded number to check + call check_prime + +; n is loaded from ebx +check_prime: + mov ecx, 2 ; number to start at for primality check + loop: + cmp ecx, ebx + je end_is_prime ; if at the end of the loop, call end_is_prime + + ; operate division (bdx/ecx) <=> (num/current) + xor edx, edx ; will store remainder + mov eax, ebx + div ecx + + cmp edx, 0 ; check if ecx divides ebx + je end_is_not_prime + inc ecx ; continue loop + jmp loop + + +; success +end_is_prime: + mov edx, is_len + mov ecx, is_prime + mov ebx, 1 ; stdout + mov eax, 4 ; sys_write + int 0x80 + + mov eax, 1 ; sys_exit + mov ebx, 0 ; exit code 0 + int 0x80 ; syscall + +; failure +end_is_not_prime: + mov edx, not_len + mov ecx, not_prime + mov ebx, 1 ; stdout + mov eax, 4 ; sys_write + int 0x80 ; syscall + + mov ebx, 1 ; exit code 1 + mov eax, 1 ; sys_exit + int 0x80 ; syscall diff --git a/TP/TP3/main.s b/TP/TP3/main.s new file mode 100644 index 0000000..7f7196b --- /dev/null +++ b/TP/TP3/main.s @@ -0,0 +1,66 @@ +! This programs checks if a number n>1 is prime +! it checks for divisors from 2 to n-1, but does not terminate if n=1 + +_EXIT = 1 +_WRITE = 4 +_STDOUT = 1 +.SECT .TEXT +start: + MOV BX,37 ! hardcoded number to check + CALL check_prime + + PUSH 2 + PUSH _EXIT + SYS + +! n is loaded from BX +check_prime: + MOV CX,2 ! number to start at for primality check +l: CMP CX,BX + JE end_is_prime ! if CX=BX, exit, n is prime + + ! operate division (DX/CX) <=> (num/current) + XOR DX,DX ! will store remainder + MOV AX,BX + DIV CX + + CMP DX,0 ! check if CX divides BX + JE end_is_not_prime + INC CX ! continue loop + JMP l + +end_is_prime: + PUSH 6 + PUSH is_prime + PUSH _STDOUT + PUSH _WRITE + SYS + + SUB SP,8 + + PUSH 0 + PUSH _EXIT + SYS + +end_is_not_prime: + PUSH 10 + PUSH not_prime + PUSH _STDOUT + PUSH _WRITE + SYS + + SUB SP,8 + + PUSH 1 + PUSH _EXIT + SYS + + + +.SECT .DATA +is_prime: +.ASCII "prime\n" +not_prime: +.ASCII "not prime\n" + +.SECT .BSS