51单片机定时器汇编1秒
时间: 2023-07-06 16:23:06 浏览: 105
() {
int prime;
do {
prime = rand() % MAX_PRIME + 1;
} while (!is_prime(prime));
return prime;
}
// 随机生成公私钥对
void generate_key_pair(BigNum *n, BigNum *假设使用的是51单片机内部定时器T0,其时钟源频率为11.0592MHze, BigNum *d) {
srand(time(NULL));
int p = generate_prime();
int q = generate_prime();
while/12=921.6kHz,那么定时器T0的计数器初值为(65536-921.6 (q == p) {
q = generate_prime();
}
int phi = (p - 1) * (q - )/256=253,即T0计数从253开始,每计数到256就产生一次中断,中断1);
int x, y;
int gcd = exgcd(phi, KEY_LENGTH, &x, &y);
while (gcd != 1 || x <= 0) {
p = generate_prime();
q = generate_prime();
while (q == p)频率为921.6kHz/256=3.6kHz,那么中断一次的时间为1/3.6 {
q = generate_prime();
}
phi = (p - 1) * (q - 1);
gcd = exkHz=277.78us,中断1000次即1秒,所以需要中断1000/1.67=599gcd(phi, KEY_LENGTH, &x, &y);
}
BigNum bn_p, bn_q, bn_phi, bn_x,次(1.67=1000/600)。
以下是汇编代码实现:
```
ORG 0H
MOV TM bn_y;
char str_p[10], str_q[10], str_phi[MAX_LEN], str_x[MAX_LEN], str_y[MAX_LENOD,#01H ;设置T0为模式1,仅使用T0计数器
MOV TH0,#0FDH];
sprintf(str_p, "%d", p);
sprintf(str_q, "%d", q);
sprintf(str_phi, "%d", ;设置T0计数器初值为253
MOV TL0,#0FDH
SETB TR0 ;启动 phi);
sprintf(str_x, "%d", x);
sprintf(str_y, "%d", y);
read(&bn_p, str_p);
read(&bn_q, str_q);
read(&bn_phi, str_phi);
read(&bn_x, str_x);
T0
SETB ET0 ;允许T0中断
SETB EA ;总中断使能
MOV R read(&bn_y, str_y);
mul(&bn_p, &bn_q, n);
copy(e, &bn_x);
7,#100H ;设置计数器初值为100
LOOP:
DJNZ R7,LOOP ;循环延时 copy(d, &bn_y);
}
// 加密明文
void encrypt(char *plain, BigNum *n, BigNum *,约为100us
CLR TR0 ;停止T0
CLR ET0 ;禁止T0中断
MOVe, BigNum *cipher) {
BigNum bn_plain, bn_c;
read(&bn_plain, plain);
int i = TH0,#0FDH ;重新设置T0计数器初值为253
MOV TL0,#0FDH
MOV 0;
while (compare(&bn_plain, n) > 0) {
BigNum bn_t;
div(&bn_plain R7,#100H ;重新设置计数器初值为100
SETB TR0 ;启动T0
SET, pow(10, KEY_LENGTH - 1), &bn_t);
int mod = mod(&bn_plain, (int)pow(10, KEY_LENGTH - 1));
mul(&bn_t, e, &bn_c);
BigNum bn_mod;
char strB ET0 ;允许T0中断
DJNZ A,LOOP ;循环599次,约为1秒
END
```
阅读全文