Writing Assembly LC2 Program to Calculate Dot Products of Two Arrays

In summary, to write an assembly program to calculate the dot products of two arrays with LC 2, you will need to use loops and pointers to access the elements of the arrays. It is recommended to write the program in pseudocode first and then translate it into assembly. Using higher level languages like C can also be beneficial as they may have auto-vectorization options. However, if writing in assembly, using SSE or SSE2 instruction sets can improve performance. It is important to be familiar with the specific architecture and registers available to optimize the code. Finally, it may be helpful to refer to examples from other assembly programs for guidance.
  • #1
colourbox
1
0
how to write an assembly program to calculate the dot products of two arrays ?? Thx

Anyone know how to write an assembly program (source code) to calculate the dot products of two arrays with LC 2 ?


Given:

The size of each array is 15 (a[0],...,a[14] ; b[0],...,b[14]) that are stored in the memory.

The dot product result is obtained by: a[0]*b[0] + a[1]*b[1] + ... + a[14]*b[14] and result is stored in the memory.
 
Technology news on Phys.org
  • #2


Just like in every other language - open loop and go through both vectors at the same time. Try to write it in some pseudocode first, then translate into assembly.

And why do you want to do it an assembly, any higher level language will be much easier.
 
  • #3


I would recommend to compile it in C, then view in your debugger in mixed mode (with assembly) or just decompile it, and then use dirrect assembly statements in your C program if you want to improve performance. Generally, you can't improve performance significantly unless you see that your compiler doesn't compile it right.

Watch out for extra pointer dereferencing and assignment to registers. The rest should be fine. When I say pointer dereferenceing, I mean to make sure that the variable you use to access the array address is in the register, rather than in RAM. That should improve speed by reducing memory bus access.

Depending on what you are looking for, don't forget to turn on optimizations.
 
  • #4


In fact it is probably BETTER to write this kind of code in a higher level language like C-- array dot product is the kind of thing that could possibly be caught by auto-vectorization in your compiler if your compiler has it (meaning your code could take advantage of processor vector units like SSE). If you are writing in assembly on the other hand you will not get these advantages unless you go writing SSE instructions by hand...

This sounds like an assignment for a class, apparently "LC-2" is some kind of toy architecture from a particular textbook? It is unlikely anyone here would have experience with the specific textbook/tool you're using, so maybe it would help if you just explained to us what you have so far and where you're getting stuck.
 
  • #5


Coin said:
In fact it is probably BETTER to write this kind of code in a higher level language like C-- array dot product is the kind of thing that could possibly be caught by auto-vectorization in your compiler if your compiler has it (meaning your code could take advantage of processor vector units like SSE). If you are writing in assembly on the other hand you will not get these advantages unless you go writing SSE instructions by hand...
But I suspect that the OP is taking a class in assembly, so writing the code in a higher-level language is probably not an option.
Coin said:
This sounds like an assignment for a class, apparently "LC-2" is some kind of toy architecture from a particular textbook? It is unlikely anyone here would have experience with the specific textbook/tool you're using, so maybe it would help if you just explained to us what you have so far and where you're getting stuck.

colourbox,
I don't know the architecture this is running on, so I don't know what registers are available. You'll need two registers that act as pointers to individual cells in the two arrays. You'll need another register that acts as a loop counter, from 15 down to 0. (When the register gets to 0, you're done.)

In each iteration of the loop you need to multiply the numbers that the two indexing registers point to, and add the result to an accumulating register (originally initialized to 0).
Then you advance each index to point to the next element in each array, and decrement the loop counter.

That's the basic idea.
 
  • #6


Colourbox if you're trying to write optimal routines you might want to look into vector processing functions like SSE or SSE2 (that is the intel instruction set, I think AMD chipsets also support it but don't quote me on this).

The SSE/SSE2 instruction sets basically let you execute mathematical instructions on vectors of words (from what i remember SSE2 words are 128-bit in size).

The Intel Compiler has native support for vector operations where you use macros to specify the commands. Otherwise you can download the architecture and instruction set manuals from the manufacturers website.
 
  • #7


I might be wrong, but I don't think colourbox is interested in optimized code or the SSE/SSE2 instruction set on Intel chips, but is interested specifically in LC2 assembly language. I found some examples from a guy who was in an assembly class at UCR (Univ. of Calif. at Riverside, I think) here:http://matmrosko.com/2007/11/09/assembly-ucr-cs61/
 
Last edited by a moderator:

Related to Writing Assembly LC2 Program to Calculate Dot Products of Two Arrays

1. What is Assembly LC2?

Assembly LC2 is a low-level programming language used for microprocessors. It is a type of assembly language specifically designed for the LC2 microprocessor, commonly used in educational settings to teach basic computer architecture concepts.

2. What is a dot product?

A dot product, also known as a scalar product, is a mathematical operation that takes two equal-length sequences of numbers and returns a single number. It is calculated by multiplying corresponding elements in the two sequences and then adding the products together.

3. Why would I need to calculate dot products of two arrays?

Calculating dot products of two arrays is useful in many applications, such as data analysis, signal processing, and machine learning. It allows us to determine the similarity or correlation between two sets of data.

4. How do I write an Assembly LC2 program to calculate dot products of two arrays?

To write an Assembly LC2 program to calculate dot products of two arrays, you will need to understand the LC2 instruction set architecture and its assembly language syntax. You will also need to know how to load data into registers, perform arithmetic operations, and store results. It is recommended to consult a reference manual or tutorial for specific syntax and examples.

5. Are there any tips for writing efficient Assembly LC2 programs for dot products?

One important tip for writing efficient Assembly LC2 programs for dot products is to use the LC2's multiply and accumulate (MAC) instruction, which performs both multiplication and addition in a single step. Additionally, minimizing the number of memory accesses and using preloading techniques can also help improve performance.

Similar threads

  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
7
Replies
235
Views
10K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
4
Views
693
Back
Top