Writing Assembly LC2 Program to Calculate Dot Products of Two Arrays

Click For Summary

Discussion Overview

The discussion revolves around writing an assembly program to calculate the dot products of two arrays using LC2 assembly language. Participants explore various approaches to implementing this task, including considerations of performance and the appropriateness of using higher-level languages.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant inquires about writing an assembly program for calculating the dot product of two arrays, specifying the size and storage of the arrays in memory.
  • Another participant suggests starting with pseudocode before translating it into assembly, questioning the necessity of using assembly over higher-level languages.
  • A different viewpoint recommends compiling in C and using a debugger to view the assembly, noting that performance improvements are often limited unless the compiler is not optimizing correctly.
  • Some participants argue that using a higher-level language like C might be more beneficial due to potential compiler optimizations such as auto-vectorization, which could enhance performance without manual assembly coding.
  • One participant emphasizes the need for specific registers in LC2 assembly to manage array indexing and loop counting, outlining a basic structure for the loop and multiplication process.
  • Another participant mentions vector processing functions like SSE/SSE2 for optimization, but questions whether the original poster is focused on optimized code or specifically on LC2 assembly language.
  • One participant shares a resource from a previous assembly class, indicating that examples may exist for the LC2 architecture.

Areas of Agreement / Disagreement

Participants express differing opinions on the appropriateness of using assembly versus higher-level languages, with some advocating for assembly due to educational requirements while others suggest that higher-level languages may offer better performance through compiler optimizations. The discussion remains unresolved regarding the best approach to take.

Contextual Notes

Participants note the specific architecture of LC2 and the potential limitations in knowledge about its registers and instruction set, which may affect the implementation of the program.

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
4K
  • · Replies 122 ·
5
Replies
122
Views
17K
  • · Replies 30 ·
2
Replies
30
Views
7K
Replies
2
Views
2K
Replies
235
Views
15K
Replies
20
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K