Clemson University -- CPSC 231 -- Fall 2009 condition codes (four bits kept in PSR register - processor status register) N -- negative contains status of result from last instruction Z -- zero to explicitly set condition codes (e.g., subcc) V -- overflow C -- carry cmp reg,immediate == subcc reg, immediate, %g0 cmp reg,reg == subcc reg, reg, %g0 branches ba -- branch always testing individual condition codes: bneg -- branch on negative (N==1) bpos -- branch on positive (N==0) bz -- branch on zero (Z==1) synonym for be bnz -- branch on not zero (Z==0) synonym for bne bvs -- branch on overflow set (V==1) bvc -- branch on overflow clear (V==0) bcs -- branch on carry set (C==1) bcc -- branch on carry clear (C==0) testing result of compare or other operation (signed arithmetic): bl -- branch on less than ((N xor V)==1) ble -- branch on less than or equal ((Z or (N xor V))==1) be -- branch on equal (Z==1) bne -- branch on not equal (Z==0) bge -- branch on greater than or equal ((N xor V)==0) bg -- branch on greater than ((Z or (N xor V))==0) (other branches are available for unsigned arithmetic) Control structures in C and assembly while loop while ( a <= 17 ) { test: a = a + b; cmp %a_r, 17 } bg done nop add %a_r, %b_r, %a_r ba test nop done: for loop for ( a = 1; a <= b; a++ ) { mov 1,%a_r c *= a; test: } cmp %a_r, %b_r bg done nop rewritten as while loop mov %a_r, %o0 mov %c_r, %o1 a = 1; call .mul while ( a <= b ) { nop c *= a; mov %o0, %c_r a++; add %a_r, 1, %a_r } ba test nop done: if-then if ( (a + b) > c ) { add %a_r, %b_r, %o0 a += b; cmp %o0, %c_r } ble next nop add %a_r, %b_r, %a_r next: if-then-else if ( a != b ) { cmp %a_r, %b_r c = 1; be else_part } else { nop c = 0; then_part: } mov 1, %c_r ba next nop else_part: mov 0, %c_r next: three ways to do a counted loop for loop, counted up from 0 to n-1 for ( i = 0; i < n; i++ ) { clr %i_r test: } cmp %i_r, n bge done nop rewritten as while loop i = 0; while ( i < n ) { inc %i_r ba test i++; nop } done: for loop, counted up from 1 to n for ( i = 1; i <= n; i++ ) { mov 1, %i_r test: } cmp %i_r, n bg done nop rewritten as while loop i = 1; while ( i <= n ) { inc %i_r ba test i++; nop } done: for loop, counted down from to n to 1 for ( i = n; i > 0; i-- ) { mov n, %i_r test: } cmp %i_r, 0 ble done nop rewritten as while loop i = n; while ( i > 0 ) { dec %i_r ba test i--; nop } done: