220 likes | 244 Views
Learn to convert algorithms to assembly code with do, while, for loops, if-else blocks. Includes comparison and branch instructions.
E N D
Control Structure Assembly Language
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
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
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
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
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
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
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
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
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
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
For Loop • Structure: for( ex1 ; ex2 ; ex3 ) st; • Translate to: ex1; while( ex2 ) { st; ex3; } Assembly Language
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
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
If-Then • Structure: if( ex1 ) { st; } • Test "ex1" • Skip "st" if ex1 is false Assembly Language
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
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
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
If-Then-Else Example d = a; if((a+b) > c) { a = a + b; c = c + 1; } else { d = c; } a = c + d; Assembly Language
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
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