1 / 19

ECE 447: Lecture 18

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

arvin
Download Presentation

ECE 447: Lecture 18

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. ECE 447: Lecture 18 Frequently Used Operations in C and Assembly Language

  2. 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 staa m ; m = k next rts unsigned char k, l, m; void comp(void) { if (k > l) m = k; }

  3. 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 

  4. 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

  5. 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 (kl) goto next staa m ; m = k next rts signed char k, l, m; void comp(void) { if (k > l) m = k; }

  6. 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 (kl) 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; }

  7. 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; }

  8. 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

  9. 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; } }

  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++; } }

  11. 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) }

  12. 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; } }

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

More Related