Title: Frequently Used Operations
1ECE 447 - Lecture 19
Frequently Used Operations in C and Assembly
Language
2Comparing 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 gt l) m k
3Control instructions (1) - Branches
N Z V C
REL
after comparison register vs. memory
unsigned numbers
signed numbers
BHI higher gt BLO lower lt BHS higher
or same ? BLS lower or same ?
BGT greater than gt BLT less than
lt BGE greater than or equal ? BLE less than
or equal ?
BEQ equal BNE not equal ?
4Control 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 lt 0
unconditional
BRA always BRN never
5Comparing 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 gt l) m k
6If-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 gt l) max k
else max l
7Multiway 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
8Multiway decision (2)
C
Assembly language
inc regular bra switch_end lines_inc inc
lines bra switch_end breaks_inc inc
breaks switch_end rts
9For 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 k0 void loop(void) unsigned
char i for(i0 ilt4, i) k
k10
10While 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 k0 void loop(void) unsigned
char i i0 while(ilt4) k
k10 i
11Do-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 k0 void loop(void) unsigned
char i i0 do k k10
i while(ilt4)
12Double 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 k0 void double_loop(void)
unsigned char i, j for(i0 ilt4, i)
for(j0 jlt5 j) k k10
13Set 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
14Set 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
15Set 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
16Set 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
17Clear 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
18Clear 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
19Accessing 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