Compare commits
3 Commits
130eaa54f0
...
25d2cbe07e
Author | SHA1 | Date | |
---|---|---|---|
25d2cbe07e | |||
8ec1f9ead9 | |||
35ad2f3582 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -4,4 +4,5 @@
|
||||
**/build
|
||||
|
||||
TD/TD3/ENB.log
|
||||
TD/TD3/ibm4q05.dat
|
||||
TD/TD3/ibm4q05.dat
|
||||
TD/TD10/88
|
BIN
Cours/ARC-L10.pdf
Normal file
BIN
Cours/ARC-L10.pdf
Normal file
Binary file not shown.
BIN
Cours/ARC-L9.pdf
Normal file
BIN
Cours/ARC-L9.pdf
Normal file
Binary file not shown.
13
TP/TP3/Makefile
Normal file
13
TP/TP3/Makefile
Normal file
@ -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
|
26
TP/TP3/README.md
Normal file
26
TP/TP3/README.md
Normal file
@ -0,0 +1,26 @@
|
||||
# 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).
|
||||
|
||||
Le programme prend en entrée un entier `n>1` et vérifie pour chacun des nombres de 2 à n-1 si celui-ci le divise.
|
||||
Le résultat s'affiche sur la sortie standard et dans l'exit code du programme.
|
||||
|
||||
## 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
|
||||
```
|
56
TP/TP3/main.asm
Normal file
56
TP/TP3/main.asm
Normal file
@ -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
|
66
TP/TP3/main.s
Normal file
66
TP/TP3/main.s
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user