Bonsoir,

je dois essayer de reprogrammer la fonction scanf, donc voici une première version qui plante lors de l'interruption de lecture dans la fonction mread....

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
#include <stdio.h>
#include <inttypes.h>
 
ssize_t mscanf(const char *,...);
 
int main()
{
	int a = 6;
	char c = 'c';
	double d = 12.102;
 
	printf("sizeof int = %d || sizeof char = %d || sizeof double = %d\n",sizeof(int),sizeof(char),sizeof(double));
 
	printf("a = %d || c = %c || d = %f\n",a,c,d);
 
	mscanf("%d",&a);
 
	printf("a = %d\n", a);
 
	return 0;
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 
.global mscanf
.type mscanf, @function
mscanf:
	pushq %rbp
	movq %rsp, %rbp
	movq %rdi, %r15								# %r15 = &format [ %d || %f || %c ]
	movq %rsi, %r14								# %r14 = &var1
	call parser										# parse &format
	call mread										# sys_red [ !! ERREUR !!]
	movq %rbp, %rsp
	popq %rbp
	ret
 
.global parser
.type parser, @function
parser:
	pushq %rbp
	movq %rsp, %rbp
	movq %r15, %rcx								# %rcx = &format
parser_loop:
	movb (%rcx), %al
	cmp $32, %al									# case : espace
	je parser_next								# incq %rcx
	cmp $37, %al									# case : %
	je parser_next								# incq %rcx
	cmp $100, %al									# case : d
	je int_init										# pour les entiers
	jmp error											# SINON ERREUR
int_init:												# %rdx = taille = 4
	movq $4, %rdx
	jmp parser_end
char_init:											# %rdx = taille = 1
	movq $1, %rdx
	jmp parser_end
double_init:										# %rdx = taille = 8
	movq $8, %rdx
	jmp parser_end
error:
	pushq $1											# on quitte avec une erreur
	call exit
parser_next:
	incq %rcx											# %rcx = &format + 1 (plutot 8 ?)
	jmp parser_loop
parser_end:
	movq %rbp, %rsp
	popq %rbp
	ret
 
.global mread
.type mread, @function
mread:												# %rdxja initialisé
	pushq %rbp
	movq %rsp, %rbp
	movq $3, %rax								# sys_print
	movq $0, %rbx								# stdin
	movq %r14, %rcx							# %rcx = &var1
	int $0x80										# interruption
	movq %rbp, %rsp
	pop %rbp
	ret
 
.global mprint
.type mprint, @function
mprint:
	pushq %rbp
	movq %rsp, %rbp
	movq $4,	%rax							# sys_read
	movq $1, %rbx								# stdout
	movq $9, %rdx								# taille
	movq %r15, %rcx							# &msg
	int $0x80										# interruption
	movq %rbp, %rsp
	popq %rbp
	ret
 
.global exit
.type exit, @function
exit:
	pushq %rbp
	movq %rsp, %rbp
	movq 16(%rbp), %rbx
	movq $1, %rax
	int $0x80											# interruption
	ret          									# ne doit pas s'exec
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
myScanf:	myScanfA myScanfC
			gcc -gstabs -o myScanf myScanfA.o myScanfC.o
 
myScanfA: 	myScanf.s
			gcc -g -c -o myScanfA.o myScanf.s
 
myScanfC: 	myScanf.c
			gcc -g -c -o myScanfC.o myScanf.c
 
clean:
			rm myScanf myScanfC.o myScanfA.o || echo true

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
:$./myScanf 
sizeof int = 4 || sizeof char = 1 || sizeof double = 8
a = 6 || c = c || d = 12.102000
5
a = 6
:$ 5
bash: 5 : commande introuvable
Merci d'avance