240 likes | 547 Views
ECE 447 - Lecture 19. Frequently Used Operations in C and Assembly Language. 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 19 Frequently Used Operations in C and Assembly Language
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; }
Control instructions (1) - Branches 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
Control instructions (2) - Branches 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
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; }
If-else statement 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; }
Multiway decision (1) 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; }
Multiway decision (2) C Assembly language inc regular bra switch_end lines_inc inc lines bra switch_end breaks_inc inc breaks switch_end rts
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; } }
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++; } }
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) }
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; } }
Set a bit in a global variable (1) 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
Set a bit in a global variable (2) C Assembly language BIT3 EQU %00001000 section .data c: fcb 5 section .text set_bit3: ldx #c bset 0,X,BIT3 rts
Set a bit at a specific address (1) 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
Set a bit at a specific address (2) C Assembly language BIT3 EQU %00001000 ADDR EQU $1005 set_bit3: ldx #ADDR bset 0,X,BIT3 rts
Clear a bit in a global variable (1) 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
Clear a bit in a global variable (2) C Assembly language BIT3 EQU %00001000 section .data c: fcb 5 section .text set_bit3: ldx #c bclr 0,X,BIT3 rts
Accessing 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 C source extern volatile unsigned char GLOBAL_8_BIT_VAR • Now the variable can be accessed by both C and ASM source