TP2 Rapport de Micro

JIN Zhuoyuan 22213816

Ex1

1.1
Op1 Op2 Type d’opération Résultat N Z C V
0x08000000 0x07000000 + 0x0F000000 0 0 0 0
- 0x01000000 0 0 1 0
0x40000000 0x40000000 + 0x80000000 1 0 0 1
0x40000000 0x80000000 - 0xC0000000 1 0 0 1
0x00F00000 0xFFFFFFFF + 0x00EFFFFF 0 0 1 0
0x7F000000 0x0F000000 + 0x8E000000 1 0 0 1
- 0x70000000 0 0 1 0
0x0F000000 0x7F000000 + 0x8E000000 1 0 0 1
- 0x90000000 1 0 0 0
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
.data
const1: .word 0x08000000
const2: .word 0x07000000
const3: .word 0x40000000
const4: .word 0x80000000
const5: .word 0x00F00000
const6: .word 0xFFFFFFFF
const7: .word 0x7F000000
const8: .word 0x0F000000

.text
.global main
.type main, %function

main:
/* ligne 1*/
LDR R0, =const1
LDR R0,[R0]
LDR R1, =const2
LDR R1,[R1]
ADD R2,R0,R1 // calcule 0x08000000 + 0x07000000
SUB R3,R0,R1 // calcule 0x08000000 - 0x07000000
/* ligne 2*/
LDR R0, =const3
LDR R1,[R0]
LDR R0,[R0]
ADD R2,R0,R1 // calcule 0x40000000 + 0x40000000
/* ligne 3*/
LDR R1, =const4
LDR R1,[R1]
SUB R2,R0,R1 // calcule 0x40000000 - 0x80000000
/* ligne 4*/
LDR R0, =const5
LDR R0,[R0]
LDR R1, =const6
LDR R1,[R1]
ADD R2,R0,R1 // calcule 0x00F00000 + 0xFFFFFFFF
/* ligne 5*/
LDR R0, =const7
LDR R0,[R0]
LDR R1, =const8
LDR R1,[R1]
ADD R2,R0,R1 // calcule 0x7F000000 + 0x0F000000
SUB R3,R0,R1 // calcule 0x7F000000 - 0x0F000000
/* ligne 6*/
ADD R2,R1,R0 // calcule 0x0F000000 + 0x7F000000
SUB R3,R1,R0 // calcule 0x0F000000 - 0x7F000000

stop: B stop
BX LR
.end
1.2
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
.data
op1_low: .word 0xFFFFFFFE // Partie basse de l'opérande 1 [31:0]
op1_high: .word 0x02000000 // Partie haute de l'opérande 1 [63:32]
op2_low: .word 0x00000011 // Partie basse de l'opérande 2 [31:0]
op2_high: .word 0x02000000 // Partie haute de l'opérande 2 [63:32]
result_64: .skip 8

.text
.global main
.type main, %function

main:
// Charger les parties hautes en premier (changement d'ordre)
LDR R0, =op1_high // 使用低寄存器 R0
LDR R4, [R0]
LDR R1, =op2_high // 使用低寄存器 R1
LDR R5, [R1]
// Charger les parties basses ensuite
LDR R2, =op1_low
LDR R6, [R2]
LDR R3, =op2_low
LDR R7, [R3]
// Addition des 32 bits faibles
ADD R2, R6, R7
// Addition avec retenue pour les 32 bits forts
ADC R4, R4, R5
// Sauvegarde du résultat (changer l'ordre d'écriture)
LDR R0, =result_64
STR R4, [R0, #4] // Sauvegarde d'abord la partie haute
STR R2, [R0] // Puis la partie basse

stop:
B stop
BX LR
.end

resultat: 0x0F000000 01000004
1-2

1.3
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

MOV R0, #0x3A //R0 = 0x3A (0011 1010)
MOV R1, #0x0F //R1 = 0x0F (0000 1111)
MOV R2, #0x10 //R2 = 0x10 (0001 0000)

AND R1, R0, R1 // R1 = ?
//R1 = R0 & R1 = 0011 1010 & 0000 1111 = 0000 1010 (0x0A)
AND R0, R0, R2 // R0 = ?
//R0 = R0 & R2 = 0011 1010 & 0001 0000 = 0001 0000 (0x10)
ORR R0, R1, R0 // R0 = ?
//R0 = R1 | R0 = 0000 1010 | 0001 0000 = 0001 1010 (0x1A)
BIC R0, R0, R2 // R0 = ?
//R0 = R0 & (~R2) = 0001 1010 & 1110 1111 = 0000 1010 (0x0A)


//suite
LDR R0, =VAL //R0 = 0x87654321
LDR R0, [R0] //R0 = 0x87654321
MOV R1, #0xFF //R1 = 0xFF (0000 0000 1111 1111)

LSL R2, R1, #4 // R2 = ?
//R2 = R1 << 4 = 0x0FF0 (0000 1111 1111 0000)
ASR R3, R2, #2 // R3 = ?
//R3 = R2 >> 2 (算术右移) = 0x03FC (0000 0011 1111 1100)
ASR R4, R0, #2 // R4 = ?
//R4 = R0 >> 2 (算术右移) = 0xE1D950C8 (最高位1,符号位扩展)
LSR R4, R1, #1 // R4 = ?
//R4 = R1 >> 1 (逻辑右移) = 0x007F (0000 0000 0111 1111)
EOR R2, R4, R2 // R2 = ?
//R2 = R4 ⊕ R2 = 0x007F ⊕ 0x0FF0 = 0x0F8F (0000 1111 1000 1111)
BIC R4, R4, R1 // R4 = ?
//R4 = R4 & (~R1) = 0x007F & 0xFF00 = 0x0000

  • AND est utilisé pour conserver certains bits et effacer les autres.
  • ORR est utilisé pour définir certains bits.
  • BIC est utilisé pour effacer des bits spécifiques.
  • LSL (décalage logique à gauche) est utilisé pour accélérer la multiplication (×2ⁿ).
  • ASR (décalage arithmétique à droite) est utilisé pour la division des nombres signés.
  • LSR (décalage logique à droite) ne préserve pas le bit de signe.
  • EOR (ou exclusif) est utilisé pour inverser certains bits.
  • BIC (effacement de bits) est utilisé pour masquer certains bits.

Ex2 Suite de Fibonacci

La suite de Fibonacci est une suite d’entiers dont chaque terme est la somme des deux termes précédents. Les deux premiers termes permettent de démarrer le calcul et sont initialisés à 0 et 1 respectivement.

On note donc que :

F(n) = F(n-1) + F(n-2)

2.1
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
#include <stdio.h>
#include <stdlib.h>

#define TAB_SIZE 10

int fibo(int x, int y);
int fibo_suite[TAB_SIZE] = {0};

int main(void) {
int val1 = 0, val2 = 1;

// Initialize the first two values of the Fibonacci sequence
fibo_suite[0] = 0; // F(0) = 0
fibo_suite[1] = 1; // F(1) = 1

// Calculate the Fibonacci sequence up to TAB_SIZE terms using the C function
for (int i = 2; i < TAB_SIZE; i++) {
fibo_suite[i] = fibo(val1, val2); // Use the C function to calculate the next Fibonacci number
val1 = val2; // Update val1 to the previous Fibonacci value
val2 = fibo_suite[i]; // Update val2 to the current Fibonacci value
}

// Print the Fibonacci sequence
for (int i = 0; i < TAB_SIZE; i++) {
printf("F(%d) = %d\n", i, fibo_suite[i]);
}

return 0;
}

// C function to calculate Fibonacci (F(i) = F(i-1) + F(i-2))
int fibo(int x, int y) {
return x + y; // Return the sum of the two numbers (F(i) = F(i-1) + F(i-2))
}

Resultat:
f2-1

2.2
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
#include <stdio.h>
#include <stdlib.h>

#define TAB_SIZE 10

int fibo(int x, int y);
int fibo_suite[TAB_SIZE] = {0};
void fibo_asm(int x, int y);

int res = 0;
int x_local = 0;
int y_local = 0;

int main(void) {
int val1 = 0, val2 = 1;

// Initialize the first two values of the Fibonacci sequence
fibo_suite[0] = 0; // F(0) = 0
fibo_suite[1] = 1; // F(1) = 1

// Calculate the Fibonacci sequence up to TAB_SIZE terms using the assembly function
for (int i = 2; i < TAB_SIZE; i++) {
fibo_asm(val1, val2); // Call the assembly function to compute the next Fibonacci number
val1 = val2; // Update val1 to the previous Fibonacci value
val2 = res; // Update val2 to the current result
fibo_suite[i] = res; // Store the result in the array
}

// Print the Fibonacci sequence
for (int i = 0; i < TAB_SIZE; i++) {
printf("F(%d) = %d\n", i, fibo_suite[i]);
}

// Infinite loop (could be used for debugging purposes)
while (1) {
// You can add more code here if needed
}

return 0;
}

// C function to calculate Fibonacci (not used here)
int fibo(int x, int y) {
return x + y; // Return the sum of the two numbers (F(i) = F(i-1) + F(i-2))
}

// Assembly function to calculate Fibonacci number and store it in the global variable 'res'
void fibo_asm(int x, int y) {
// Assign the input parameters to local variables
x_local = x;
y_local = y;

// Assembly code to calculate the Fibonacci number
asm("LDR r0, =x_local"); // Load address of x_local
asm("LDR r0, [r0]"); // Load value of x_local into r0
asm("LDR r1, =y_local"); // Load address of y_local
asm("LDR r1, [r1]"); // Load value of y_local into r1
asm("ADD r2, r0, r1"); // r2 = r0 + r1 (F(i) = F(i-1) + F(i-2))

// Store the result in the global variable 'res'
asm("LDR r3, =res"); // Load address of res
asm("STR r2, [r3]"); // Store result (r2) into 'res'
}
  1. x=0 y=1 res=1
    01
  2. x=1 y=1 res=2
    11
  3. x=1 y=2 res=3
    12

après c’est kif-kif

……

  1. x=13 y=21 res=34
    1321

Ex3

C:
1
2
3
4
5
6
7
8
9
10
int count_e(char* tab){  
int res =0;
for(int i=0; i<strlen(tab);i++){
char tmp =tab[i];
if(tmp=='e' ||tmp=='E'){
res++;
}
}
return res;
}

resultat:
c

asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int count_e_asm(char* tab){

asm("LDR r0,=string1");
asm("LDR r1,=size");
asm("LDR r1,[r1]");
asm("MOV r2,#-1"); //index string
asm("MOV r4,#0"); //compteur du nombre de 'e' et 'E'

asm("begin: ADD r2,r2,#1");
asm("LDRB r3,[r0,r2]");
asm("CMP r3, #101"); //char 'e' in ASCII
asm("BEQ count");
asm("CMP r3, #69"); //char 'E' in ASCII
asm("BEQ count");
asm("B test_fin");
asm("count: ADD r4,r4,#1");
asm("B test_fin");
asm("test_fin: CMP r2,r1");
asm("BNE begin");
asm("LDR r5,=nb_e_asm");
asm("STR r4,[r5]");

return 0;
}

resultat:
asm