1 / 19

Frequently Used Operations in C and Assembly Language

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

brook
Download Presentation

Frequently Used Operations in C and Assembly Language

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 19 Frequently Used Operations in C and Assembly Language

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

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

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

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

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

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

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

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

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

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

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

More Related