control structure
DESCRIPTION
Control Structure. Control Structures. Write an algorithm first and then convert it to an assembly program. Learn how to translate basic control structures into machine language. Basic control structures: do loop while loop for loop if-then-else. Comparison Instruction. - PowerPoint PPT PresentationTRANSCRIPT
Natawut Nupairoj Assembly Language 1
Control Structure
Natawut Nupairoj Assembly Language 2
Control Structures
• Write an algorithm first and then convert it to an assembly program.
• Learn how to translate basic control structures into machine language.
• Basic control structures:– do loop– while loop– for loop– if-then-else
Natawut Nupairoj Assembly Language 3
Comparison Instruction
• To compare two values (register vs. register or register vs. constant):cmp %o2, 30
orcmp %g2, %i1
• The result of this instruction effects the following flags:Z - whether the result was zero.N - whether the result was negative.V - whether the result was overflow.C - whether the result generated a carry.
Natawut Nupairoj Assembly Language 4
Branch Instructions
• The comparison instruction can be used with branch instructions:– ba branch always = goto– bn branch never = nop– bl branch on less (than zero)– ble branch on less or equal (to zero)– be branch on equal (to zero)– bne branch on not equal (to zero)– bge branch on greater or equal (to zero)– bg branch on greater (than zero)
Natawut Nupairoj Assembly Language 5
Do…While Loop
• Structure:do {
some statements here;
} while(logical expression);
• Translate to:
loop: assembly code for S1
assembly code for S2 … Sn
assembly code for L1
branch to loop if L1 is true
S1…Sn
L1
Natawut Nupairoj Assembly Language 6
Our Second Program
• Let modify our first program to include a loop:main()
{
int x, y;
x = 0;
do {
y = ((x - 1) * (x - 7)) / (x - 11);
x = x + 1;
} while (x < 11);
printf(“%d\n”, y);
}
Natawut Nupairoj Assembly Language 7
Our Second Program
/* Variables
* Store x in %l0
* Store y in %l1 */
fmt: .asciz “%d\n”
.align 4
.global main
main: save %sp, -64, %sp
clr %l0 ! x = 0
loop: sub %l0, 1, %o0 !(x - 1) to %o0
sub %l0, 7, %o1 !(x - 7) to %o1
call .mul !(x - 1)*(x - 7)
nop
Natawut Nupairoj Assembly Language 8
Our Second Program
sub %l0, 11, %o1 !(x - 11) to %o1
call .div !(x-1)*(x-7)/(x-11)
nop
mov %o0, %l1 ! Store it in y
add %l0, 1, %l0 ! x = x + 1
cmp %l0, 11 ! x < 11 ?
bl loop
nop ! Delay slot
Natawut Nupairoj Assembly Language 9
Our Second Program
set fmt, %o0
mov %l1, %o1
call printf ! printf(“%d\n”, y);
nop
mov 1, %g1 ! Exit request
ta 0
Natawut Nupairoj Assembly Language 10
While Loop
• The condition is tested first.while (logical expression) {
some statements here;
};
• Translate to:loop: assembly code for L1
branch to exit if L1 is false
assembly code for S1
assembly code for S2 … Sn
unconditional branch to loop
done:
S1…Sn
L1
Natawut Nupairoj Assembly Language 11
While Loop Example
while(a <= 17) {
a = a + b;
c = c + 1;
}
loop: cmp %l0, 17
bg done ! Branch if L1 is false
nop ! Delay slot
add %l0, %l1, %l0 ! a = a + b
add %l2, 1, %l2 ! c = c + 1
ba loop ! Always branch to “loop”
nop ! Delay slot
done:
L1
S1 and S2
Natawut Nupairoj Assembly Language 12
For Loop
• Structure:for( ex1 ; ex2 ; ex3 )
st;
• Translate to:ex1;
while( ex2 ) {
st;
ex3;
}
Natawut Nupairoj Assembly Language 13
For Loop Example
for (a=1 ; a <= b ; a++) {
c = c * a;
}
• Translate to:
a = 1;
while( a <= b ) {
c = c * a;
a++; /* a = a + 1 */
}
Note: a = %l0, b = %l1, c = %l2
Natawut Nupairoj Assembly Language 14
For Loop Example
mov 1, %l0 ! a = 1;
loop: cmp %l0, %l1 ! Compare a and b.
bg exit ! Exit for-loop if a > b.
nop
mov %l2, %o0 ! First param for .mul
mov %l1, %o1 ! Second param for .mul
call .mul ! %o0 = c * b
nop
mov %o0, %l2 ! Store result in c
add %l0, 1, %l0 ! a++;
ba test
nop
exit:
Natawut Nupairoj Assembly Language 15
If-Then
• Structure:if( ex1 ) {
st;
}
• Test "ex1"• Skip "st" if ex1 is false
Natawut Nupairoj Assembly Language 16
If-Then Example
d = a;
if((a+b) > c) {
a = a + b;
c = c + 1;
}
a = c + d;
• NOTE: – a = %l0– b = %l1– c = %l2– d = %l3– tmp = %l4 -- keep a+b value
Natawut Nupairoj Assembly Language 17
If-Then Example
mov %l0, %l3 ! d = a;
add %l0, %l1, %l4 ! tmp = (a + b)
cmp %l4, %l2
ble next ! Jump if((a+b) <= c)
nop ! Delay slot
! Inside if statement.
add %l0, %l1, %l0 ! a = a + b;
add %l2, 1, %l2 ! c = c + 1;
! End of inside if statement.
next:
add %l2, %l3, %l0 ! a = c + d;
Natawut Nupairoj Assembly Language 18
If-Then-Else
• Structure:if( ex1 ) {
st1;
} else {
st2;
}
• Test "ex1"• If ex1 is false, goto
”ELSE-BLOCK:”.• IF-BLOCK:
– Execute “st1”– Goto “DONE:”
• ELSE-BLOCK:– Execute “st2”
• DONE:
Natawut Nupairoj Assembly Language 19
If-Then-Else Example
d = a;
if((a+b) > c) {
a = a + b;
c = c + 1;
} else {
d = c;
}
a = c + d;
Natawut Nupairoj Assembly Language 20
If-Then-Else Example
mov %l0, %l3 ! d = a;
add %l0, %l1, %l4 ! tmp = (a + b)
cmp %l4, %l2
ble else-block ! Jump if((a+b) <= c)
nop ! Delay slot
! if-block: Inside if statement.
add %l0, %l1, %l0 ! a = a + b;
add %l2, 1, %l2 ! c = c + 1;
ba done ! Skip the else-block.
nop ! Delay slot
! End of inside if statement.
Natawut Nupairoj Assembly Language 21
If-Then-Else Example
else-block:
! Inside else-block.
mov %l2, %l3 ! d = c
! End of else-block.
done:
add %l2, %l3, %l0 ! a = c + d;