Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop an x86_64 assembly language program that uses a function and a loop to c

ID: 3751854 • Letter: D

Question

Develop an x86_64 assembly language program that uses a function and a loop to calculate values across an array. Procedure For this assignment you must write all code in x86_64 assembly (but you may use/call C functions in your assembly code). All code should be in one source file only (asm_program.asm) You may reuse code from any of my assembly examples for this assignment. 1. Create a function which takes four 64-bit integer arguments (eg. W, X, Y, Z) o Your function should compute (W - X) * (Y Z), and leave the result in RAX when it returns. o Your function should accept the four arguments using appropriate conventions (see registers listed in the lecture slides). o Your function should NOT print anything - it should ONLY perform the math, and return the result via RAX. 2. Use a loop to compute the above function over an array of size 16 (A[0], A[1], ..., A[15]) o Declare an array (in memory) containing 16 reasonable 64-bit integer values (of your choosing). o Use a loop to iterate over the elements of the array, and use your function (above) to calculate the value of every four consecutive array values o For example, rax -(A[0] - A[1]) (A[2] A[3]), then rax (AI1] - A[2]) (AI3] A[4]), etc. o For each iteration, print out the calculation, the result, and start a newline (exactly like this): (5 - 2) (3 +4) 21 o The last iteration should compute rax- (A[ 12] - A[13]) (A[14] A[15]) (i.e. don't calculate past the end of the array!) Develop an x86_64 assembly language program that uses a function and a loop to calculate values across an array. Procedure For this assignment you must write all code in x86_64 assembly (but you may use/call C functions in your assembly code). All code should be in one source file only (asm_program.asm) You may reuse code from any of my assembly examples for this assignment. 1. Create a function which takes four 64-bit integer arguments (eg. W, X, Y, Z) o Your function should compute (W - X) * (Y Z), and leave the result in RAX when it returns. o Your function should accept the four arguments using appropriate conventions (see registers listed in the lecture slides). o Your function should NOT print anything - it should ONLY perform the math, and return the result via RAX. 2. Use a loop to compute the above function over an array of size 16 (A[0], A[1], ..., A[15]) o Declare an array (in memory) containing 16 reasonable 64-bit integer values (of your choosing). o Use a loop to iterate over the elements of the array, and use your function (above) to calculate the value of every four consecutive array values o For example, rax -(A[0] - A[1]) (A[2] A[3]), then rax (AI1] - A[2]) (AI3] A[4]), etc. o For each iteration, print out the calculation, the result, and start a newline (exactly like this): (5 - 2) (3 +4) 21 o The last iteration should compute rax- (A[ 12] - A[13]) (A[14] A[15]) (i.e. don't calculate past the end of the array!)

Explanation / Answer

When you use the instruction ADC or SBB to add or subtract an integer with the previous carry, you reasonably want to reserve the previous carry flag (CF) with PUSHFD and POPFD, since an address update with ADD will overwrite the CF. The following Extended_Add example borrowed from the textbook [2] is to calculate the sum of two extended long integers BYTE by BYTE:

Hide   Copy Code

As we know, the INC instruction makes an increment by 1 without affecting the CF. Obviously we can replace above ADD with INC to avoid PUSHFD and POPFD. Thus the loop is simplified like this:

Hide   Copy Code

Now you might ask what if to calculate the sum of two long integers DWORD by DWORD where each iteration must update the addresses by 4 bytes, as TYPE DWORD. We still can make use of INC to have such an implementation:

Hide   Copy Code

Applying a scaling factor here would be more general and preferred. Similarly, wherever necessary, you also can use the DEC instruction that makes a decrement by 1 without affecting the carry flag.