Question: can I get help writing two function in assembly please . // I included an example of the assembly language we use in class named

can I get help writing two function in assembly please .

// I included an example of the assembly language we use in class named it is inside a file named "demo".

// file.c

#include

#include

#include

// do not change anything above this comment

unsigned int authenticate(unsigned int x)

{

unsigned int okay = 0;

/* Here is where you will put the assembly code to authenticate

* the user:

* 1. calculate the value 4*x + 7 where x is the input value.

* 2. XOR this with the number 0xDEAD (57005 in decimal).

* 3. if this value is less than or equal to 0xDEAD,

* then the user is authenticated. Set the return value to 1.

* 4. if this value is less than to 0xDEAD, then the user is not

* authenticated. Set the return value to 0.

*/

return okay;

}

//-----------------------------

double calcSecretNumber(double x)

{

double result = 0.0;

/* need to calculate and return

* the secret number using this

* formula:

* fnum = 2.25*sqrt(3.45 + fx^2)

*/

return result;

}

-----------------------------------------------------------------------------------------------------------------

// demo

// demo of GCC inline assembler

// Example of the inline assembler we use in class .

char charArray[] = {0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,

0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};

int intArray[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};

int main(int argc, char **argv)

{

int localVar = 0x12345678;

double fpVar = 16.0;

// embedded assembly language

asm(" mov $0x89ABCDEF, %%eax "

" mov $0xFF, %%bl "

" movsx %%bl, %%eax "

" movzx %%bl, %%eax "

// two instructions in one asm statement

// (not a good idea, but interesting that you can do this)

" mov $0x12, %%al mov $0x34, %%ah "

// swapping two values

" mov $0xBBBBBBBB, %%ebx "

" mov $0xAAAAAAAA, %%eax "

" xchg %%ebx, %%eax "

// extended asm

" mov %[localVarIn], %%eax " // store localVar into eax

" add $4, %%eax " // add 4 to eax

" mov %%eax, %[localVarOut] " // copy register to localVar

// int array

" mov $3, %%ecx " // initialize ecx

" lea %[intArray], %%ebx " // load address of start of array

" mov (%%ebx), %%eax " // get first element of array

" mov (%%ebx, %%ecx, 4), %%eax " // get indexed element of array

// char array

" mov $0, %%eax " // clear eax

" lea %[charArray], %%ebx " // load address of start of array

" mov (%%ebx), %%al " // get first element of array

" mov (%%ebx, %%ecx, 1), %%al " // get indexed element of array

// conditional statement

" cmp $3, %[localVarIn] " // comparison operation

" jg gt " // conditional jump if > 3

" nop " // didn't jump

"gt: nop " // conditional jump target

// floating point load, store

" fldl %[fpVarIn] " // load fpVar onto floating point stack

" fld %%st " // duplicate value on top of fp stack

" faddp " // add the two values on the fp stack

" fstpl %[fpVarOut] " // store the sum back into fpVar

: [localVarOut] "=m" (localVar), // outputs

[fpVarOut] "=m" (fpVar)

: [localVarIn] "m" (localVar), // inputs

[intArray] "m" (intArray),

[charArray] "m" (charArray),

[fpVarIn] "m" (fpVar)

: "eax", "ebx", "ecx" // clobbers

);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!