106k views
0 votes
In this task, you’ll need to write a complete program to calculate the dot product of two

vectors in assembly. You should write this program in a .s file, and you should be able to
assemble/link/execute it using QEMU emulator without any warning or error. For now
we haven’t learned how to print things out to the terminal, so you don’t need to print out
anything, and the CAs will check your code manually data vec1: .quad 10,20,30 vec2: .quad 1,2,3

User Dennis H
by
8.1k points

1 Answer

2 votes

This program assumes that the vectors vec1 and vec2 are both of length 3.

.section .data

vec1: .quad 10, 20, 30

vec2: .quad 1, 2, 3

result: .quad 0

.section .text

.global _start

_start:

movq $0, %rcx # Initialize loop counter

movq $0, %rdx # Initialize dot product accumulator

calculate_dot_product:

movq vec1(%rcx), %rax # Load element from vec1

movq vec2(%rcx), %rbx # Load element from vec2

imulq %rax, %rbx # Multiply corresponding elements

addq %rbx, %rdx # Add the result to the accumulator

incq %rcx # Increment loop counter

cmpq $3, %rcx # Compare with the length of the vectors (assumed to be 3)

jl calculate_dot_product # Jump to the next iteration if not reached the end

movq %rdx, result # Store the result in the result variable

# Exit the program

User Andrew Jackman
by
8.1k points

No related questions found