Writing Assembly LC2 Program to Calculate Dot Products of Two Arrays

Click For Summary
The discussion focuses on writing an assembly program to calculate the dot product of two arrays using the LC-2 architecture. The arrays, each containing 15 elements, are stored in memory, and the dot product is computed by multiplying corresponding elements and summing the results. Participants suggest starting with pseudocode before translating it into assembly. They emphasize the importance of using registers efficiently to reduce memory access times and recommend considering higher-level languages like C for better performance through compiler optimizations, such as auto-vectorization. However, it is acknowledged that the task may be part of an academic assignment, limiting the use of higher-level languages. Suggestions include using registers as pointers for array elements and maintaining a loop counter to iterate through the arrays. Additionally, there is mention of the potential benefits of vector processing instructions like SSE/SSE2 for optimization, although the primary focus remains on the specifics of LC-2 assembly language.
colourbox
Messages
1
Reaction score
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


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.
 


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.
 


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.
 


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.
 


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.
 


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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
6K
  • · Replies 122 ·
5
Replies
122
Views
16K
Replies
2
Views
2K
Replies
235
Views
14K
Replies
20
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K