想从C51单片机转入stm8单片机来学习,那么进行软件编程时能不能使用汇编呢?
stm8单片机编译有两种嵌入汇编指令的方法。第一种语法:
#asm //开始汇编指令块
#endasm //结束汇编指令块
第二种语法:
_asm("嵌入的汇编代码",符合C语言规则的参数);
下面我们分别依照两种方法举一个例子。
方法一:
#include "stm8s003.h"
typedef unsigned char u8;
u8 i,the_a;
void subroutine(void) { }
main()
{
the_a = 0x02;
#asm /*#asm要顶格写*/
push a
ld a,_the_a
inc a
ld _the_a,a
call _subroutine
pop a
#endasm
}
方法二:
_asm("push a\n ld a,2\n ld _the_a,a\n pop a\n call _subroutine\n");
while (1) { i = the_a; }
以上是在C语言中嵌入汇编指令的两种软件编程方法。有兴趣的读者可以自行编写一些汇编代码下载到CPU中实验一下。有时候使用汇编技术进行编程,可以简化编程思路,使代码更简洁。