Control Structure - PowerPoint PPT Presentation

About This Presentation
Title:

Control Structure

Description:

Control Structure – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 22
Provided by: cpEngC
Category:

less

Transcript and Presenter's Notes

Title: Control Structure


1
Control Structure
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

3
Comparison Instruction
  • To compare two values (register vs. register or
    register vs. constant)
  • cmp o2, 30
  • or
  • cmp 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.

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)

5
DoWhile Loop
S1Sn
  • 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

L1
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 lt 11)
  • printf(d\n, y)

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

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 lt 11 ?
  • bl loop
  • nop ! Delay slot

9
Our Second Program
  • set fmt, o0
  • mov l1, o1
  • call printf ! printf(d\n, y)
  • nop
  • mov 1, g1 ! Exit request
  • ta 0

10
While Loop
L1
  • 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

S1Sn
11
While Loop Example
L1
  • while(a lt 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

S1 and S2
12
For Loop
  • Structure
  • for( ex1 ex2 ex3 )
  • st
  • Translate to
  • ex1
  • while( ex2 )
  • st
  • ex3

13
For Loop Example
  • for (a1 a lt b a)
  • c c a
  • Translate to
  • a 1
  • while( a lt b )
  • c c a
  • a / a a 1 /
  • Note a l0, b l1, c l2

14
For Loop Example
  • mov 1, l0 ! a 1
  • loop cmp l0, l1 ! Compare a and b.
  • bg exit ! Exit for-loop if a gt 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

15
If-Then
  • Structure
  • if( ex1 )
  • st
  • Test "ex1"
  • Skip "st" if ex1 is false

16
If-Then Example
  • d a
  • if((ab) gt c)
  • a a b
  • c c 1
  • a c d
  • NOTE
  • a l0
  • b l1
  • c l2
  • d l3
  • tmp l4 -- keep ab value

17
If-Then Example
  • mov l0, l3 ! d a
  • add l0, l1, l4 ! tmp (a b)
  • cmp l4, l2
  • ble next ! Jump if((ab) lt 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

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

19
If-Then-Else Example
  • d a
  • if((ab) gt c)
  • a a b
  • c c 1
  • else
  • d c
  • a c d

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((ab) lt 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.

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
Write a Comment
User Comments (0)
About PowerShow.com