How to write an assembly program to calculate the dot products of two arrays ? Thx

  • Thread starter colourbox
  • Start date
  • #1
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.
 

Answers and Replies

  • #2
Borek
Mentor
28,501
2,914


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
101
0


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
560
1


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
33,949
5,607


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.
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
chiro
Science Advisor
4,790
132


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
33,949
5,607


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/ [Broken]
 
Last edited by a moderator:

Related Threads on How to write an assembly program to calculate the dot products of two arrays ? Thx

Replies
3
Views
21K
Replies
18
Views
6K
Replies
10
Views
757
Replies
4
Views
1K
  • Last Post
Replies
13
Views
3K
  • Last Post
Replies
2
Views
3K
Replies
14
Views
7K
  • Last Post
Replies
18
Views
8K
Top