1 / 21

Control Structure

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.

vmartins
Télécharger la présentation

Control Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Structure 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 Assembly Language

  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. 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) Assembly Language

  5. Do…While Loop S1…Sn • 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 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); } 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 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 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 Assembly Language

  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: S1…Sn Assembly Language

  11. While Loop Example L1 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: S1 and S2 Assembly Language

  12. For Loop • Structure: for( ex1 ; ex2 ; ex3 ) st; • Translate to: ex1; while( ex2 ) { st; ex3; } 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 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: Assembly Language

  15. If-Then • Structure: if( ex1 ) { st; } • Test "ex1" • Skip "st" if ex1 is false 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 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; Assembly Language

  18. 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: If-Then-Else 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; 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. 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; Assembly Language

More Related