Presentation is loading. Please wait.

Presentation is loading. Please wait.

Natawut NupairojAssembly Language1 Control Structure.

Similar presentations


Presentation on theme: "Natawut NupairojAssembly Language1 Control Structure."— Presentation transcript:

1 Natawut NupairojAssembly Language1 Control Structure

2 Natawut NupairojAssembly Language2 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 Natawut NupairojAssembly Language3 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 Natawut NupairojAssembly Language4 Branch Instructions The comparison instruction can be used with branch instructions: –babranch always = goto –bnbranch never = nop –blbranch on less (than zero) –blebranch on less or equal (to zero) –bebranch on equal (to zero) –bnebranch on not equal (to zero) –bgebranch on greater or equal (to zero) –bgbranch on greater (than zero)

5 Natawut NupairojAssembly Language5 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 S 1 …S n L1L1

6 Natawut NupairojAssembly Language6 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); }

7 Natawut NupairojAssembly Language7 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 Natawut NupairojAssembly Language8 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

9 Natawut NupairojAssembly Language9 Our Second Program set fmt, %o0 mov %l1, %o1 call printf! printf(“%d\n”, y); nop mov 1, %g1! Exit request ta 0

10 Natawut NupairojAssembly Language10 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: S 1 …S n L1L1

11 Natawut NupairojAssembly Language11 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: L1L1 S 1 and S 2

12 Natawut NupairojAssembly Language12 For Loop Structure: for( ex1 ; ex2 ; ex3 ) st; Translate to: ex1; while( ex2 ) { st; ex3; }

13 Natawut NupairojAssembly Language13 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

14 Natawut NupairojAssembly Language14 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:

15 Natawut NupairojAssembly Language15 If-Then Structure: if( ex1 ) { st; } Test "ex1" Skip "st" if ex1 is false

16 Natawut NupairojAssembly Language16 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

17 Natawut NupairojAssembly Language17 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;

18 Natawut NupairojAssembly Language18 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 Natawut NupairojAssembly Language19 If-Then-Else Example d = a; if((a+b) > c) { a = a + b; c = c + 1; } else { d = c; } a = c + d;

20 Natawut NupairojAssembly Language20 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.

21 Natawut NupairojAssembly Language21 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;


Download ppt "Natawut NupairojAssembly Language1 Control Structure."

Similar presentations


Ads by Google