Download Example 1: Greatest Common Divisor (GCD)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

List of prime numbers wikipedia , lookup

Wieferich prime wikipedia , lookup

Sieve of Eratosthenes wikipedia , lookup

Exponentiation wikipedia , lookup

Mersenne prime wikipedia , lookup

Prime number theorem wikipedia , lookup

Transcript
Example 1: Greatest Common
Divisor (GCD)
ICT 106 Fundamentals of
Computer Systems
Week 10
Implement an inline code to return the
greatest common divisor of two integers.
The greatest common divisor is the largest
integer that will evenly divide both
integers.
Practical Inline Assembly
Examples
school of information technology
ICT 106 _Week 10_06
/*
This program returns the greatest common divisor
of two integers. Two procedures are provided: one
is implemented in C and the other is implemented
in inline code
school of information technology
int main()
{
int x, y, out;
printf("Please enter two integers: \n");
scanf("%d%d", &x, &y);
out = GCD(x,y);
printf("C: Greatest common divisor of two numbers %d and
%d is %d.\n\n", x, y, out);
author: eric li
*/
#include <stdio.h>
#include <math.h>
int ASMGCD(int x, int y);
int GCD(int x, int y);
2
out = ASMGCD(x,y);
printf("ASM Greatest common divisor of two numbers %d
and %d is %d.\n\n", x, y, out);
return 0;
}
1
/* C-implementation */
int GCD(int x, int y)
{
x = abs(x);
y = abs(y);
do {
int n = x % y;
x = y;
y = n;
} while (y > 0);
return x;
}
/* inline assembly code */
int ASMGCD(int x, int y)
{
int n;
__asm {
IF01:
mov eax, x;
cmp eax, 0;
jg ENDIF01;
neg eax;
// absolute value
mov x, eax;
ENDIF01:
IF02:
mov eax, y;
cmp eax, 0;
jg ENDIF02;
neg eax;
// absolute value
mov y, eax;
ENDIF02:
DO01:
mov eax, x; // numerator
mov ebx, y; // a reg used for denom
sub edx, edx; // zero dx
idiv ebx;
// signed integer divide
mov n, edx;
// remainder
mov ebx, y;
xchg ebx, x; // x = y
mov ebx, n;
xchg ebx, y; // y = n;
mov ebx, y;
cmp y, 0;
jg
DO01;
DOEND01:
}
return x;
}
// while (y > 0)
Example 2: Prime Number
Write a procedure to test whether an
integer is a prime number or not. The
procedure returns 0 if the integer is a
prime and returns non-zero if it is not. a
prime number is a natural number that
has exactly two natural number divisors,
which are 1 and the prime number itself.
ICT 106 _Week 10_06
8
school of information technology
2
/* The procedure tests if an integer is a prime
number. It returns zero if it is a prime number;
otherwise, it returns non-zero value */
int isPrime(int x)
{
int result=0;
__asm {
mov eax, 1; // initialise the counter to one
DO01:
push eax; // save eax
mov ecx,eax; // call ASMGCD procedure to
push ecx
// check the greatest common
mov edx,x; // divisor of the counter and the
push edx;
// input.
call ASMGCD;
add esp,8;
IF01:
cmp eax, 1;
// if the returned value is one, means
je ELSE01;
// the input is not divisible by the counter;
pop eax;
// otherwise, the input is not a prime, then
mov result, 1; // terminate the loop and return one to the
jmp ENDDO01; // calling function
ELSE01:
pop eax;
inc eax;
cmp eax, x;
jl DO01;
ENDIF01:
ENDDO01:
}
// to check the next integer value till
// the counter is equal to the input
return result;
}
/* Assembly code */
__asm {
mov eax, 2; // let eax=2
Example 3
• To identify all prime numbers which is less than
100 using the previous two procedures.
FOR01:
cmp eax, 100; // test eax <100;
je ENDFOR01;
/* C-implementation */
for(i=2; i<100; i++) {
prime = isPrime(i);
if(prime==0) {
printf("Integer %d is a prime.\n", i);
}
}
mov i, eax; // let i=eax
push eax; // save eax
mov edx,eax; // push parameter onto the stack
push edx;
call isPrime; // call procedure isPrime
add esp,4;
mov prime, eax; // returning value
}
ICT 106 _Week 10_06
11
school of information technology
3
// mix of C and Assembly
if(prime==0) {
printf("Integer %d is a prime.\n", i);
}
__asm {
pop eax; // increment eax by one
inc eax;
jmp FOR01;
ENDFOR01:
}
Integer 2 is a prime.
Integer 3 is a prime.
Integer 5 is a prime.
Integer 7 is a prime.
Integer 11 is a prime.
Integer 13 is a prime.
Integer 17 is a prime.
Integer 19 is a prime.
Integer 23 is a prime.
Integer 29 is a prime.
Integer 31 is a prime.
Integer 37 is a prime.
Integer 41 is a prime.
Integer 43 is a prime.
Integer 47 is a prime.
Integer 53 is a prime.
Integer 59 is a prime.
Integer 61 is a prime.
Integer 67 is a prime.
Integer 71 is a prime.
Integer 73 is a prime.
Integer 79 is a prime.
Integer 83 is a prime.
Integer 89 is a prime.
Integer 97 is a prime.
Example 4: Data Encryption with
RSA
• To demonstrate how to encrypt and
decrypt data using RSA
• RSA encryption is a method for securing
data. It has unique properties that make it
especially attractive for securing data that
must be transferred among many parties.
• Thus, RSA is still widely used in electronic
commerce protocols.
ICT 106 _Week 10_06
15
school of information technology
• RSA involves a public and private key:
– The public key can be known to everyone and is used for the
encryption process.
– The encrypted message can only be decrypted by use of the
private key
• A public key consists of a product of two
large prime numbers (modulus) and an
integer R (public exponent)
• A private key is an integer S (private
exponent)
ICT 106 _Week 10_06
16
school of information technology
4
• Assume:
– Modulus = 33 (i.e. 11x3)
– R=7
– S=3
• Encryption algorithm:
• Given plain data x,
x = x^R;
encrypt_x = x % MODULUS;
ICT 106 _Week 10_06
17
school of information technology
/*
To demonstrate how RSA encryption works. The
encryption method as well as decryption method is
implemented using inline assemble.
author: eric li
*/
• Decryption algorithm:
• Given encrypted data y
y = y ^ S;
decrypt_y = y % MODULUS;
ICT 106 _Week 10_06
18
school of information technology
int main()
{
int array[] = {3, 8, 9, 5, 2, 1}; // original data
int enarray[6]; // encrypted data
int dearray[6]; // decrypted data
int i = 0; // counter
#include <stdio.h>
#include <math.h>
for(i=0; i< 6; i++) {
enarray[i] = encrypt(array[i]);
dearray[i] = decrypt(enarray[i]);
#define PRIME 33
#define R 7
#define S 3
printf("Orriginal value: %d\t Encypted value:
%d\t Decrypted value: %d\n", array[i], enarray[i],
dearray[i]);
}
return 0;
int encrypt(int x); // encryption procedure prototype
int decrypt(int x); // decryption procedure prototype
}
5
Encryption Procedure
/*
x = (int) pow(x, R);
x = x % PRIME;
*/
int encrypt(int x)
{
__asm {
mov ecx, x;
mov eax, x;
mov bx, word ptr 0;
ICT 106 _Week 10_06
21
school of information technology
Decryption Procedure
/*
x = (int) pow(x, S);
x = x % PRIME;
*/
int decrypt(int x)
{
__asm {
mov ecx, x;
mov eax, x;
mov bx, word ptr 0;
ICT 106 _Week 10_06
23
FOR01:
cmp bx, 6;
je ENDFOR01;
mul ecx;
inc bx;
jmp FOR01;
ENDFOR01:
mov ebx, PRIME; // a reg used for denom
sub edx, edx; // zero dx
idiv ebx;
// signed integer divide
mov x, edx; // remainder
}
return x;
}
FOR01:
cmp bx, 2;
je ENDFOR01;
mul ecx;
inc bx;
jmp FOR01;
ENDFOR01:
mov ebx, PRIME; // a reg used for denom
sub edx, edx; // zero dx
idiv ebx;
// signed integer divide
mov x, edx;
// remainder
}
return (x);
}
school of information technology
6
Output
Orriginal value: 3
Orriginal value: 8
Orriginal value: 9
Orriginal value: 5
Orriginal value: 2
Orriginal value: 1
ICT 106 _Week 10_06
Encypted value: 9
Encypted value: 2
Encypted value: 15
Encypted value: 14
Encypted value: 29
Encypted value: 1
25
Decrypted value: 3
Decrypted value: 8
Decrypted value: 9
Decrypted value: 5
Decrypted value: 2
Decrypted value: 1
school of information technology
7