Vous êtes sur la page 1sur 5

Module : architecture ordinateur (microprocesseur MIPS) k.

AFDEL
Filière SMI

LES INSTRUCTIONS DE BRANCHEMENTS ET DE SAUT


Lorsqu'un programme est en train de s’exécuter, ses instructions sont localisées dans la
mémoire principale. L’adresse d’une instruction est donnée par le premier octet (LSB) des 4 octets de
l’instruction. Chaque cycle machine exécute l’instruction indiquée par PC. PC est ensuite incrémenté
de 4 (taille d’une instruction) pour se positionner sur la prochaine instruction à exécuter. Quand un
saut (jump) est exécuté, PC contient une nouvelle adresse de la prochaine instruction à exécuter en
différé En dit que le processeur saut vers une instruction se trouvant quelque part dans le mémoire
principale.

PC just after this


Address Instruction instruction has executed
(at the bottom of the cycle)

............... ........... 00400000

00400000 load 00400004

00400004 add 00400008

00400008 jump 0x400000 0040000C

0040000C no-op 00400000 -- effect of the jump

j target # after a delay of one machine cycle,


# PC <-- address of target

6 26
000010 00000000000000000000000000 -- champs d’instruction
opcode target -- description des champs

Figure 1

On prend les 26 bits représentons l’adresse cible de l’instruction jump et on les décale de
deux bits pour avoir une adresse sur 28 puis on place les 4 bits (MSB de PC et on les
copie dans les bits b28, b29, b30 et b31 pour avoir l’adresse de branchement.

Note !!! PC contient l’adresse du saut

27
Module : architecture ordinateur (microprocesseur MIPS) k.AFDEL
Filière SMI

BRANCHEMENT CONDITIONNEL

beq u,v,addr # if register $u == register $v


# PC <-- addr (after a delay of one machine cycle.)
# else
# no effect.

... # load values into $8 and $9


beq $8,$9,cont # branch if equal
nop # branch delay slot
... # conditionally
... # executed
... # statements
cont: add $10,$10,$11 # always executed

bne u,v,addr # if register $u ≠ register $v


# PC <-- addr (after a delay of one machine cycle.)
# else
# no effect.

IF…..ELSE….;

... # load values into


# $8 and $9
beq $8,$9,equal # branch if equal
nop # branch delay slot
... #
... # false branch
... #
j cont
nop
equal: ... #
... # true branch
... #
cont: add $10,$10,$11 # always executed

On utilise des sauts conditionnels et


inconditionnels:

Pascal la fonction minimum

if t1 < t2 then t3 := t1 else t3 := t2

28
Module : architecture ordinateur (microprocesseur MIPS) k.AFDEL
Filière SMI

Assembleur Mips

blt $t1, $t2, Then # si t1 >= t2 saut à Then


move $t3, $t2 # t3 := t1
j End # saut à Fi
Then: move $t3, $t1 # t3 := t2
End: # suite du programme

## absVal.asm
##
## Calculate the absolute value of A
##
## Registers:
## $8 --- A, two's comp. integer
## $9 --- sign bit of A
## $10 --- base register for .data section

.text
.globl main

main:
# Get A
lui $10,0x1000 # Init base register
lw $8,0($10) # Load A
sll $0,$0,0

# Is A Negative?
srl $9,$8,31 # Shift sign bit to position 0
beq $0,$9,done # sign bit == zero, done
sll $0,$0,0

# Store -A
sub $8,$0,$8 # negate A
sw $8,0($10) # save it

done: sll $0,$0,0 # target of the branch

.data
A: .word -1

## End of File

29
Module : architecture ordinateur (microprocesseur MIPS) k.AFDEL
Filière SMI

Pascal: calcule dans t2 = 0 la somme des entiers de 1 à t1

while t1 > 0 do begin t2 := t2 + t1; t1 := t1 -1 end

En assembleur, on ne dispose certainement pas d'instruction qui réalise directeemnt la boucle while.
On va donc la déconstruire à l'aide de constructions plus élémentaires (goto et if).

Programme équivalent Code Mips

While: While:
if t1 <= 0 then goto End ble $t1, $0, End
else
begin
t2 := t2 + t1; add $t2, $t2, $t1
t1 := t1 - 1; sub $t1, $t1, 1
goto While j While
end;
End: End:

Programme équivalent Code Mips

begin j Test
goto Test; Loop:
Loop: add $t2, $t2, $t1
t2 := t2 + t1; sub $t1, $t1, 1
t1 := t1 - 1; bgt $t1, $0, Loop
Test:
if t1 > 0 then goto Loop
end

APPELS SYSTEME
SPIM fournit quelques appels système minimalistes: le programme charge dans $v0
le code de l’appel et les arguments dans les registres $a0,$a3 (ou $f12 les valeurs en
virgule flottante). Le résultat se trouve dans $v0 (ou $f0).

Code
Service Arguments Results
in $v0
print_int 1 $a0 = integer to be printed
print_float 2 $f12 = float to be printed
print_double 3 $f12 = double to be printed
print_string 4 $a0 = address of string in memory
read_int 5 integer returned in $v0
read_float 6 float returned in $v0
read_double 7 double returned in $v0
$a0 = memory address of string input buffer
read_string 8
$a1 = length of string buffer (n)
sbrk 9 $a0 = amount address in $v0
exit 10

30
Module : architecture ordinateur (microprocesseur MIPS) k.AFDEL
Filière SMI

Affichage de l’entier contenu dans le registre $t2

li $v0, 1 # load appropriate system call code into register $v0;


# Code for printing integer is 1
move $a0, $t2 # move integer to be printed into $a0: $a0 = $t2
syscall # call operating system to perform operation

Read integer value, store in RAM location with label int_value (presumably declared in data section)

li $v0, 5 # load appropriate system call code into register $v0;


# code for reading integer is 5
syscall # call operating system to perform operation
sw $v0, int_value # value read from keyboard returned in register $v0;
# store this in desired location

affichage d’une chaîne

.data
string1 .asciiz "Print this.\n" # declaration for string variable,
# .asciiz directive makes string null terminated

.text

main: li $v0, 4 # load appropriate system call code into register $v0;
# code for printing string is 4
la $a0, string1 # load address of string to be printed into $a0
syscall # call operating system to perform print operation

e.g. To indicate end of program, use exit system call; thus last lines of program should be:

li $v0, 10 # system call code for exit = 10


syscall # call operating sys

exemple

.data
str: .asciiz "deux plus trois = "
.text
li $v0, 4 # system call code for print_str
la $a0, str # address of string to print
syscall # print the string
li $v0, 1 # system call code for print_int
li $a0, 5 # integer to print
syscall # print it

31

Vous aimerez peut-être aussi