logique et arithm´etique
J.-M Friedt
Masques, bits, logique et arithm´ etique
J.-M Friedt
FEMTO-ST/d´epartement temps-fr´equence [email protected]
transparents `ajmfriedt.free.fr
27 f´evrier 2021
1 / 24
logique et arithm´etique
J.-M Friedt
Affichage de valeurs num´ eriques :
hexad´ ecimal
On suppose avoir une fonction qui affiche une chaˆıne de caract`eres.
Quelle est la d´efinition d’une chaˆıne de caract`eres en C ? Comment en connaˆıtre la fin ?1
On veut afficher en hexad´ecimal des nombres cod´es sur 8, 16 et 32 bits.
Approche na¨ıve :
#i n c l u d e <s t d i o . h>
i n t main ( ) {c h a r c [ 2 5 6 ] ;
i n t 3 2 t a =10;
s p r i n t f ( c ," % d ", a ) ; }
donne un ex´ecutable qui occupe 1840 octets de programme
$ a v r−g c c −mmcu=a t m e g a 3 2 u 4 s o u r c e . c
$ a v r−o b j c o p y −O b i n a r y a . o u t a . b i n
−r w x r−x r−x 1 j m f r i e d t j m f r i e d t 1840 Jan 13 1 6 : 5 4 a . b i n 1. voiraffiche chaine.c
2 / 24
logique et arithm´etique
J.-M Friedt
Remplacer sprintf() – 8 bits
Soit un entier sur 8 bits c : remplir la chaine de caract`eresbuffer avec les chiffres repr´esentant cen hexad´ecimal (utiliser masques et divisions : 5 lignes)
man ascii
2 3 4 5 6 7 30 40 50 60 70 80 90 100 110 120 --- --- 0: 0 @ P ‘ p 0: ( 2 < F P Z d n x
1: ! 1 A Q a q 1: ) 3 = G Q [ e o y
2: " 2 B R b r 2: * 4 > H R \ f p z 3: # 3 C S c s 3: ! + 5 ? I S ] g q { 4: $ 4 D T d t 4: " , 6 @ J T ^ h r | 5: % 5 E U e u 5: # - 7 A K U _ i s } 6: & 6 F V f v 6: $ . 8 B L V ‘ j t ~ 7: ’ 7 G W g w 7: % / 9 C M W a k u DEL 8: ( 8 H X h x 8: & 0 : D N X b l v 9: ) 9 I Y i y 9: ’ 1 ; E O Y c m w A: * : J Z j z
B: + ; K [ k { C: , < L \ l | D: - = M ] m } E: . > N ^ n ~ F: / ? O _ o DEL
3 / 24
logique et arithm´etique
J.-M Friedt
Remplacer sprintf() – 8 bits
Soit un entier sur 8 bits c : remplir la chaine de caract`erest avec les chiffres repr´esentant cen hexad´ecimal
#i f d e f PC
#i n c l u d e<s t d i o . h>
#e n d i f
v o i d c (c h a r c ,c h a r ∗t )
{t [ 0 ] = ( ( c&0 x f 0 )>>4)+’ 0 ’;i f ( t [0]>’ 9 ’) t [ 0 ] + = 7 ; t [ 1 ] = ( c&0 x 0 f )+’ 0 ’; i f ( t [1]>’ 9 ’) t [ 1 ] + = 7 ; t [ 2 ] = 0 ;
} i n t main ( ) {c h a r t [ 2 5 6 ] ;
c h a r c c=0x 4 2 ; c ( cc , t ) ;
#i f d e f PC p r i n t f (" % s \ n ", t ) ;
#e l s e a f f i c h e ( t ) ;
#e n d i f }
$ a v r−g c c −mmcu=a t m e g a 3 2 u 4 s o u r c e . c
$ a v r−o b j c o p y −O b i n a r y a . o u t a . b i n
−r w x r−x r−x 1 j m f r i e d t j m f r i e d t 430 Jan 14 1 3 : 5 3 a . b i n
Code compilable sur AVRet PC :gcc -DPC fichier.c
4 / 24
logique et arithm´etique
J.-M Friedt
Remplacer sprintf() – 16 bits
Soit un entier sur 16 bitss : remplir la chaine de caract`eres buffer avec les chiffres repr´esentants en hexad´ecimal (2 lignes)
5 / 24
logique et arithm´etique
J.-M Friedt
Remplacer sprintf() – 16 bits
Soit un entier sur 16 bitss: remplir la chaine de caract`eres tavec les chiffres repr´esentant sen hexad´ecimal
#i f d e f PC
#i n c l u d e<s t d i o . h>
#d e f i n e a f f i c h e p r i n t f
#e l s e
#d e f i n e a f f i c h e
#e n d i f
// b i e n p e n s e r a f i n i r p a r l ’ o c t e t de p o i d s f a i b l e ( d ’@ l a p l u s f o r t e ) c a r on // a j o u t e \0 a l a f i n de l a c h a i n e
v o i d c (c h a r c ,c h a r ∗t )
{t [ 0 ] = ( ( c&0 x f 0 )>>4)+’ 0 ’;i f ( t [0]>’ 9 ’) t [ 0 ] + = 7 ; t [ 1 ] = ( c&0 x 0 f )+’ 0 ’; i f ( t [1]>’ 9 ’) t [ 1 ] + = 7 ; t [ 2 ] = 0 ;
}
v o i d s (s h o r t s ,c h a r ∗t ) {c ( ( s &0 x f f 0 0 )>>8,&t [ 0 ] ) ;
c ( s &0 x f f ,& t [ 2 ] ) ; }
i n t main ( ) {c h a r t [ 2 5 6 ] ;
c h a r c c=0x 4 2 ; s h o r t s s =0x 5 6 7 8 ; l o n g l l =0x 1 2 3 4 5 6 7 8 ; c ( cc , t ) ; a f f i c h e (" % s \ n ", t ) ; s ( s s , t ) ; a f f i c h e (" % s \ n ", t ) ; }
6 / 24
logique et arithm´etique
J.-M Friedt
Remplacer sprintf() – 32 bits
Soit un entier sur 32 bitsl : remplir la chaine de caract`eres buffer avec les chiffres repr´esentantl en hexad´ecimal (2 lignes)
7 / 24
logique et arithm´etique
J.-M Friedt
Remplacer sprintf() – 32 bits
Soit un entier sur 32 bitsl: remplir la chaine de caract`eres tavec les chiffres repr´esentant len hexad´ecimal
#i f d e f PC
#i n c l u d e<s t d i o . h>
#d e f i n e a f f i c h e p r i n t f
#e l s e
#d e f i n e a f f i c h e
#e n d i f
v o i d c (c h a r c ,c h a r ∗t )
{t [ 0 ] = ( ( c&0 x f 0 )>>4)+’ 0 ’;i f ( t [0]>’ 9 ’) t [ 0 ] + = 7 ; t [ 1 ] = ( c&0 x 0 f )+’ 0 ’; i f ( t [1]>’ 9 ’) t [ 1 ] + = 7 ; t [ 2 ] = 0 ;
}
v o i d s (s h o r t s ,c h a r ∗t ) {c ( ( s &0 x f f 0 0 )>>8,&t [ 0 ] ) ;
c ( s &0 x f f ,& t [ 2 ] ) ; }
v o i d l (l o n g l ,c h a r ∗t )
{s ( ( l &0 x f f f f 0 0 0 0 )>>16,&t [ 0 ] ) ; // idem que t s ( l &0 x f f f f ,& t [ 4 ] ) ;
} i n t main ( ) {c h a r t [ 2 5 6 ] ;
c h a r c c=0x 4 2 ; s h o r t s s =0x 5 6 7 8 ; l o n g l l =0x 1 2 3 4 5 6 7 8 ; c ( cc , t ) ; a f f i c h e (" % s \ n ", t ) ; s ( s s , t ) ; a f f i c h e (" % s \ n ", t ) ; l ( l l , t ) ; a f f i c h e (" % s \ n ", t ) ; }
-rwxr-xr-x 1 jmfriedt jmfriedt 662 Jan 13 17 :08 a.bin
8 / 24
logique et arithm´etique
J.-M Friedt
Echanger deux variables ´
Proposer une fonction qui prend en argument deux entiers aetb, et intervertit leur contenu
9 / 24
logique et arithm´etique
J.-M Friedt
Echanger deux variables ´
Proposer une fonction qui prend en argument deux entiers aetb, et intervertit leur contenu
Est-ce que ce programme fonctionne ?
#i n c l u d e <s t d i o . h>
v o i d f (i n t a ,i n t b ) {i n t tmp ;
p r i n t f (" % d % d - > ", a , b ) ; tmp=a ; a=b ; b=tmp ;
p r i n t f (" % d % d \ n ", a , b ) ; }
i n t main ( ) {i n t a =1 , b =2;
p r i n t f (" % d % d \ n ", a , b ) ; f ( a , b ) ; p r i n t f (" % d % d \ n ", a , b ) ; }
10 / 24
logique et arithm´etique
J.-M Friedt
Echanger deux variables ´
Proposer une fonction qui prend en argument deux entiers aetb, et intervertit leur contenu
Est-ce que ce programme fonctionne ?
#i n c l u d e <s t d i o . h>
v o i d f (i n t a ,i n t b ) {i n t tmp ;
p r i n t f (" % d % d - > ", a , b ) ; tmp=a ; a=b ; b=tmp ;
p r i n t f (" % d % d \ n ", a , b ) ; }
i n t main ( ) {i n t a =1 , b =2;
p r i n t f (" % d % d \ n ", a , b ) ; f ( a , b ) ; p r i n t f (" % d % d \ n ", a , b ) ; }
donne
1 2
1 2 −> 2 1 1 2
Passage par valeurs et non passage par pointeur⇒aetbdu programme principal sont inchang´ees
11 / 24
logique et arithm´etique
J.-M Friedt
Echanger deux variables ´
Proposer une fonction qui prend en argument deux entiers aetb, et intervertit leur contenu
#i n c l u d e <s t d i o . h>
v o i d f (i n t ∗a ,i n t ∗b ) {i n t tmp ;
p r i n t f (" % d % d - > ",∗a ,∗b ) ; tmp=∗a ; ∗a=∗b ; ∗b=tmp ;
p r i n t f (" % d % d \ n ",∗a ,∗b ) ; }
i n t main ( ) {i n t a =1 , b =2;
p r i n t f (" % d % d \ n ", a , b ) ; f (&a ,& b ) ; p r i n t f (" % d % d \ n ", a , b ) ; }
donne
1 2
1 2 −> 2 1 2 1
Passage par pointeur ⇒aetbdu programme principal sont modifi´ees
12 / 24
logique et arithm´etique
J.-M Friedt
Echanger deux variables sans ´ variable interm´ ediaire
Proposer une solution pour ´echangeraet bsans passer par une variable interm´ediairetmp
13 / 24
logique et arithm´etique
J.-M Friedt
Echanger deux variables sans ´ variable interm´ ediaire
Proposer une solution pour ´echangeraet bsans passer par une variable interm´ediairetmp
i n t main ( ) {i n t a =1 , b =2;
p r i n t f (" % d % d \ n ", a , b ) ; aˆ=b ;
bˆ=a ; aˆ=b ;
p r i n t f (" % d % d \ n ", a , b ) ; }
En effet :
1 a=0, b=0 : XOR(0,0)=0 donc on garde toujours 0 ;
2 a=1, b=1 : a=XOR(1,1)=0→b=XOR(0,1)=1 →a=XOR(0,1)=1
3 a=0, b=1 : a=XOR(0,1)=1→b=XOR(1,1)=0 →a=XOR(1,0)=1
4 a=1, b=0 : a=XOR(1,0)=1→b=XOR(0,1)=1 →a=XOR(1,1)=0 valide pour chaque bit, donc valide pour tous les bits. CQFD
14 / 24
logique et arithm´etique
J.-M Friedt
D´ emonstration :
XOR est associatif ((a^b)^c=a^(b^c)) et commutatif (a^b=b^a)
1 a=a^b;b=b^a;a=a^b;se r´eduit (1er terme) en ...
2 b=b^(a^b)eta=(a^b)^(b^(a^b));
3 b=b^(b^a); en utilisant la commutativit´e (1er terme)
4 b=(b^b)^a; en utilisant l’associativit´e (1er terme)
5 b=(0)^a;en utilisant l’identit´ea^0=a (1er terme)
6 b=a;en utilisant l’identit´ea^0=a(1er terme)
7 a=(b^b)^(a^a)^b;devienta=b; pour le deuxi`eme terme
Economie d’un octet au d´´ etriment des op´erations logiques additionnelles
15 / 24
logique et arithm´etique
J.-M Friedt
Transistors et logique
Exemple avec transistors NPN (ICE >0 siVB >0)
OUT IN
IN1
IN2
OUT
IN1
OUT IN2
16 / 24
logique et arithm´etique
J.-M Friedt
Logique → arithm´ etique : XOR
XOR r´epond `a la mˆeme logique que la somme binaire (0 + 0 = 0, 1 + 0 = 1, 1 + 1 = 01)
Comment synth´etiser la table logique de XOR `a partir des portes vues auparavant
Rappel : th´eor`eme de DeMorgan
A·B=A+B etA+B=A·B avec ·la fonction ET et + la fonction OU
17 / 24
logique et arithm´etique
J.-M Friedt
Logique → arithm´ etique : XOR
XOR r´epond `a la mˆeme logique que la somme binaire (0 + 0 = 0, 1 + 0 = 1, 1 + 1 = 01)
Comment synth´etiser la table logique de XOR `a partir des portes vues auparavant
∧ 0 1
0 0 1
1 1 0
= NOT
? 0 1
0 1 0
1 0 1
=
NOT
& 0 1
0 0 0
1 0 1
OR
&(NOT,NOT) 0 1
0 1 0
1 0 0
= NOR((A&B),(A&B))
En d’autres termes, (A·B) +A·B qui devient
A·B·A·B = (A+B)·(A+B) = 2A·A+A·B+B·A+B·B= 3A·B+B·A
2. X =X 3. A·A= 0
18 / 24
logique et arithm´etique
J.-M Friedt
Logique → arithm´ etique : XOR
XOR r´epond `a la mˆeme logique que la somme binaire (0 + 0 = 0, 1 + 0 = 1, 1 + 1 = 01)
Comment synth´etiser la table logique de XOR `a partir des portes vues auparavant
& &
~(X|Y) X=A&B Y=~A&~B Vcc
&
(A&B) (~A&~B) NOR( )
NOT ~
~&
~&
~|
,
~
~ A
B
X
Y
Solution `a 10 transistors
19 / 24
logique et arithm´etique
J.-M Friedt
Logique → arithm´ etique : XOR
XOR r´epond `a la mˆeme logique que la somme binaire (0 + 0 = 0, 1 + 0 = 1, 1 + 1 = 01)
Comment synth´etiser la table logique de XOR `a partir des portes vues auparavant
Solution `a 8 transistors puisque
A·(A·B)·B·(A·B) =A·(A·B) +B·(A·B) = 2A·(A·B) +B·(A·B)
2. (X =X)
20 / 24
logique et arithm´etique
J.-M Friedt
Logique → arithm´ etique : XOR
XOR r´epond `a la mˆeme logique que la somme binaire (0 + 0 = 0, 1 + 0 = 1, 1 + 1 = 01)
Comment synth´etiser la table logique de XOR `a partir des portes vues auparavant
Solution `a 8 transistors puisque
A·(A·B)·B·(A·B) =A·(A·B) +B·(A·B) = 2A·(A·B) +B·(A·B) = A·(A+B) +B·(A+B) =A·A+A·B+B·A+B·B= 3A·B+B·A CQFD
2. (X =X) 3. X·X = 0
21 / 24
logique et arithm´etique
J.-M Friedt
Transistors et arithm´ etique
Half adder (XOR puis retenu par AND) et full adder (XOR avec retenue)
x y
c s
x y c
s c
c est carry, s est somme
22 / 24
logique et arithm´etique
J.-M Friedt
Multiplication
Comment multiplier par 15 ?
23 / 24
logique et arithm´etique
J.-M Friedt
Multiplication
Comment multiplier par 15 ?
*15=*16-1et*16s’´ecrit <<4.
D´emonstration avecgccpour processeur ARM :
i n t main ( )
{v o l a t i l e i n t d , c =42;
d=c∗1 5 ; }
devient par
arm-none-eabi-gcc -O2 -c t.c arm-none-eabi-objdump -dSt t.o la s´equence d’opcodes
14: e0633203 rsb r3, r3, r3, lsl #4 18: e58d3000 str r3, [sp]
qui d´ecalle de 4 vers la gauche (multiplication par 16) et soustraction de r3 (*16-1)
Exercice : comment multiplier par 10 ? par 20 ? Exercice : comment est-ce que gcccompile
for (k=0;k<(20*10);k++) {...}?
24 / 24