Question: Currently my code won't print: .global main .text main: push %rbx mov $0, %r15 # counter for array mov $16, %r14 # storing the length
Currently my code won't print:
.global main .text main: push %rbx mov $0, %r15 # counter for array mov $16, %r14 # storing the length of the array in r14 pop %rbx
repeat: cmp %r15, %r14 # compare counter with length of array jge done mov array(,%r15,8), %rdi # load array[counter] into rdi mov array(,%r15,8), %rsi # load array[counter] into rsi inc %r15 # increment counter by 1 mov array(,%r15,8), %rdx # load array[counter] into rdx inc %r15 # increment counter by 1 mov array(,%r15,8), %rcx # load array[counter] into rcx inc %r15 # increment counter by 1 call Compute
mov %rdi, %rsi # move rdi into rsi mov $print_string, %rdi # load print_string into rdi mov %rax, %rdx mov $0, %rax # load 0 into rax call printf
jmp repeat
done: mov $60, %rax mov $0, %rdi syscall
Compute: mov %rdx, %rax # move rdx into rax mov %rcx, %rdx # move rcx into rdx mov $8, %rcx # load 8 into rcx (divisor) mov $0, %r10 # load 0 into r10 (remainder) mov $0, %rdx # load 0 into rdx (quotient) div %rcx # divide rax by rcx push %rdx mul %rsi # multiply rax by rsi pop %rdx sub %rax, %rdx # subtract rax from rdx mov $8, %rcx # load 8 into rcx (divisor) mov $0, %rdx # load 0 into rdx (quotient) div %rcx # divide rdx by rcx ret
.data print_string: .ascii "((%d * %d) - (%d / %d))/8 = %d \0" array: .quad 2, 15, 14, 7, 5, 2, 18, 3, 3, 45, 12, 4, 55, 17, 108, 9
1. Create a function named "Compute" which takes four 64-bit integer arguments (eg. W, X, Y, Z). Your function should compute ((WX)(Y/Z))/8, and leave the result in RAX when it returns. Your function should accept the four arguments using appropriate conventions (see registers listed in the lecture slides). - 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]) Declare an array (in memory) containing the following 16 64-bit integer values: 2,15,14,7,5,2,18,3,3,45,12,4,55,17,108,9 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. For example, rax=((A[0]A[1])(A[2]/A[3]))/8, then rax=((A[1]A[2])(A[3]/A[4]))/8, then rax=((A[2]A[3])(A[4]/A[5]))/8, then rax=((A[12]A[13])(A[14]/A[15]))/8 done Based on the sample array, rax=((215)(14/7))/8, then rax=((1514)(7/5))/8, then rax=((147)(5/2))/8, then rax=((5517)(108/9))/8 done For each iteration, print out the calculation, the result, and start a newline (Output based on the sample array): ((215)(14/7))/8=3 ((1514)(7/5))/8=26 ((147)(5/2))/8=12 ((75)(2/18))/8=4 ((5517)(108/9))/8=115 The last iteration should compute rax=((A[12]A[13])(A[14]/A[15]))/8... (i.e. don't calculate past the end of the array!) Your output should exactly match the above form
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
