190 likes | 314 Views
ECE 447: Lecture 18. Frequently Used Operations in C and Assembly Language. ECE 447: Comparing Unsigned Numbers. Assembly language. C. section .bss k: rmb 1 l: rmb 1 m: rmb 1 section .text comp: ldaa k cmpa l ; k vs. l bls next ; if (k l) goto next
E N D
ECE 447: Lecture 18 Frequently Used Operations in C and Assembly Language
ECE 447: Comparing Unsigned Numbers Assembly language C section .bss k: rmb 1 l: rmb 1 m: rmb 1 section .text comp: ldaa k cmpa l ; k vs. l bls next ; if (kl) goto next staa m ; m = k next rts unsigned char k, l, m; void comp(void) { if (k > l) m = k; }
ECE 447: Branch Instructions N Z V C – – – – REL after comparison register vs. memory unsigned numbers signed numbers BHI higher > BLO lower < BHS higher or same BLS lower or same BGT greater than > BLT less than < BGE greater than or equal BLE less than or equal BEQ equal = BNE not equal
ECE 447: Brach Instructions after arithmetic operations (testing for overflow) unsigned numbers signed numbers BCS carry set BCC carry clear BVS overflow set BVC overflow clear after testing register or memory BPL plus 0 BMI minus < 0 unconditional BRA always BRN never
ECE 447: Comparing Signed Numbers Assembly language C section .bss k: rmb 1 l: rmb 1 m: rmb 1 section .text comp: ldaa k cmpa l ; k vs. l ble next ; if (kl) goto next staa m ; m = k next rts signed char k, l, m; void comp(void) { if (k > l) m = k; }
ECE 447: if/else Statements C Assembly language section .bss k: rmb 1 l: rmb 1 max: rmb 1 section .text comp: ldaa k cmpa l ; k vs. l bls next ; if (kl) goto next staa max ; max = k bra if_end next ldaa l staa max ; max = l if_end rts unsigned char k, l, max; void comp(void) { if (k > l) max = k; else max = l; }
ECE 447: switch Statements C Assembly language section .bss c: rmb 1 lines: rmb 1 breaks: rmb 1 regular: rmb 1 section .text count: ldaa c cmpa #$A beq lines_inc cmpa #$9 beq breaks_inc cmpa #$20 beq breaks_inc unsigned char c, lines, breaks, regular; void count(void) { switch(c) { case ‘\n’: lines++; break; case: ‘\t’: case: ‘ ‘: breaks++; break; default: regular ++; break; }
ECE447: switch Statements C Assembly language inc regular bra switch_end lines_inc inc lines bra switch_end breaks_inc inc breaks switch_end rts
ECE447: for Loop C Assembly language section .data k: rmb 0 section .text loop: ldaa k ldab #4 loop_begin adda #10 decb bne loop_begin staa k unsigned char k=0; void loop(void) { unsigned char i; for(i=0; i<4, i++) { k = k+10; } }
ECE 447: while Loop Assembly language C section .data k: rmb 0 section .text loop: ldaa k clrb loop_begin cmpb #4 bhs while_end adda #10 incb bra loop_begin while_end staa k rts unsigned char k=0; void loop(void) { unsigned char i; i=0; while(i<4) { k = k+10; i++; } }
ECE 447: do-while Loop Assembly language C section .data k: rmb 0 section .text loop: ldaa k clrb loop_begin adda #10 incb cmpb #4 blo loop_begin staa k rts unsigned char k=0; void loop(void) { unsigned char i; i=0; do { k = k+10; i++; } while(i<4) }
ECE 447: Double for Loop C Assembly language section .data k: rmb 0 section .text double_loop: ldaa k ldx #4 loop_outside ldab #5 loop_inside adda #10 decb bne loop_inside dex bne loop_outside staa k rts unsigned char k=0; void double_loop(void) { unsigned char i, j; for(i=0; i<4, i++) for(j=0; j<5; j++) { k = k+10; } }
ECE 447: Setting a bit in global variable C Assembly language #define BIT3 0x04 unsigned char c = 5; void set_bit3(void) { c |= BIT3; } BIT3 EQU %00001000 section .data c: fcb 5 section .text set_bit3: ldaa c oraa #BIT3 staa c rts
ECE 447: Setting a bit in global variable C Assembly language BIT3 EQU %00001000 section .data c: fcb 5 section .text set_bit3: ldx #c bset 0,X,BIT3 rts
ECE 447: Setting a bit in Memory C Assembly language #define BIT3 0x04 #define ADDR 0x1005 void set_bit3(void) { unsigned char *ptr; ptr = (unsigned char *) ADDR; *ptr |= BIT3; } BIT3 EQU %00001000 ADDR EQU $1005 set_bit3: ldx #ADDR ldaa #BIT3 oraa 0,X staa 0,X rts
ECE 447: Setting a bit in Memory C Assembly language BIT3 EQU %00001000 ADDR EQU $1005 set_bit3: ldx #ADDR bset 0,X,BIT3 rts
ECE 447: Clear a bit in a global variable C Assembly language #define BIT3 0x04 unsigned char c = 5; void set_bit3(void) { c &= ~BIT3; } BIT3 EQU %00001000 section .data c: fcb 5 section .text set_bit3: ldaa c anda #~BIT3 staa c rts
ECE 447: Clear a bit in a global variable C Assembly language BIT3 EQU %00001000 section .data c: fcb 5 section .text set_bit3: ldx #c bclr 0,X,BIT3 rts
ECE 447: Sharing a global variable Between C and ASM source Create the variable in ASM source .global GLOBAL_8_BIT_VAR GLOBAL_8_BIT_VAR rmb 1 Declare it as a volatile extern in the C source file(s) extern volatile unsigned char GLOBAL_8_BIT_VAR Now the variable can be accessed by both C and ASM source