LC3 assembly language - create 2 arrays, multiply, and add sums

二次信任 提交于 2019-12-24 15:23:21

问题


I am trying to figure out how to create two arrays (hardcoded), multiply them together (a[0]x b[0], a[1]x b[1] ...etc.) and then add the sums together and print it.

I don't have much yet but that's because I am still getting used to this. Please please please help!

What I have so far is listed below -

.ORIG x3000
LEA R1, arr1
LEA R2, arr2 
AND R3, R3, #0  ;index 
AND R4, R4, #0  ;total


LOOP    ADD R4, R3, #4  
BRzp    DONE    
ADD R5, R1, R3  
LDR R6, R5, #0  
ADD R4, R4, R6  
ADD R3, R3, #-1     
BR  LOOP

HALT

arr1    .FILL   5   
.FILL   2   
.FILL   7   
.FILL   3    
arr1    .FILL   7   
.FILL   4   
.FILL   1   
.FILL   2   

.END

Please and thanks,

Kristyn


回答1:


I understand it's been awhile since this has been asked, but this could help someone else who has a similar issue. This would be a bit tricky if you didn't want to code this in a linear way.

  1. First I would set up the loop to cycle through each of the array items
  2. Store all of the registers you are currently using for this loop
  3. Create a second loop to handle the multiplication
  4. Store the value and exit second the loop
  5. Restore your first loop's registers
  6. Increment your loop counter and then jump back to the top of the loop

Here's some sample code that cycles through each element in the arrays and multiplies them together. After the result is stored in the array arr3.

Code:

.ORIG x3000

MAIN    
    AND R1, R1, #0          ; LOOP counter

    LOOP        
        LD R2, LIMIT
        ADD R2, R2, R1      ; Check to see if we've hit our limit
        BRz END_LOOP

        LEA R2, arr1        ; Load the memory location of arr1
        ADD R2, R2, R1      ; Add our array index to get our current arr1 memory location           
        LDR R3, R2, #0      ; Load the value at arr1[R1] into R3

        LEA R2, arr2        ; Load the memory location of arr2
        ADD R2, R2, R1      ; Add our array index to get our current arr2 memory location
        LDR R4, R2, #0      ; Load the value at arr2[R1] into R4

        ; This loop is used to multiply our numbers
        ; R3 becomes our LOOP2 counter for the multiplication
        ; Example: 7 x 5 = 7 + 7 + 7 + 7 +7
        ;
        AND R5, R5, #0
        ADD R5, R5, R4      ; Make R5 = R4
        ADD R3, R3, #-1     ; Reduce our count by 1
        LOOP2
            ADD R5, R5, R4      ; Add our second number to itself

            ADD R3, R3, #-1     ; Decrease our loop counter
            BRz END_L2      ; If our LOOP2 counter has reached 0 then break
            BR LOOP2
        END_L2

        LEA R2, arr3        ; Load the memory location of arr3
        ADD R2, R2, R1      ; Add our array index to get our current arr3 memory location
        STR R5, R2, #0      ; Store our answer currently in R5 into the memory location stored in R2

        ADD R1, R1, #1      ; Increment our loop counter
        BR LOOP         ; Branch to LOOP
    END_LOOP

HALT    

    ; Variables
LIMIT   .FILL xFFFC     ; Store the value of -4 into our loop limit variable

arr1    .FILL   5   
    .FILL   2   
    .FILL   7   
    .FILL   3    

arr2    .FILL   7   
    .FILL   4   
    .FILL   1   
    .FILL   2  

arr3    .BLKW 4         ; used to store our answer

.END


来源:https://stackoverflow.com/questions/28967946/lc3-assembly-language-create-2-arrays-multiply-and-add-sums

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!