格式:__asm{} 注:两个下划线
内嵌汇编指令中最好不使用物理寄存器名称,以免编译时出错
问题描述:在C工程的main()函数中定义了两个函数:badf()--加一;max()--求三个数最大数。
#include<stdio.h>
#include<string.h>
#define ITM_Port8(n) (*((volatile unsigned char*)(0xE0000000+4*n)))
#define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n)))
#define ITM_Port32(n) (*((volatile unsigned long*)(0xE0000000+4*n)))
#define DEMCR (*((volatile unsigned long *)(0xE000EDFC))) //Debug Exception and Monitor Control Register, DEMCR
#define TRCENA 0x01000000
#include <stdio.h>
struct __FILE{int handle;};
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f) // ???????
{
if(DEMCR & TRCENA)
{
while(ITM_Port32(0) == 0);
ITM_Port32(0) = ch;
}
return(ch);
}
int badf(int x);
int max(int a,int b,int c);
int main(void)
{
int ch;
int x;
ch=badf(10);
printf("ch=%d",ch);
x=max(4,1,20);
printf("\nmax=%d",x);
return 0;
}
int badf(int x)
{ __asm
{add x,x,1}
return(x);
}
int max(int a,int b,int c)
{
int maxtem;
__asm
{ mov maxtem,a
cmp maxtem,b
bhi loop1 ;a>b? ;hi后缀--无符号大于
mov maxtem,b
loop1:
cmp maxtem,c
bhi loop2 ;a>c?
mov maxtem,c
loop2:
mov a,maxtem
}
return(a);
}
结果