LC3汇编语言 - 创建2个阵列,乘,加总和 [英] LC3 assembly language - create 2 arrays, multiply, and add sums

查看:1083
本文介绍了LC3汇编语言 - 创建2个阵列,乘,加总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何创建两个阵列(硬codeD),它们相乘(A [0] XB [0],A [1] XB [1] ...等)和然后加入款项一起打印出来。

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

请和感谢,

Kristyn

推荐答案

我明白它已经一段时间,因为这已被问过,但这样可以帮助别人谁也有类似的问题。这将是一个有点棘手,如果你不想以线性方式code这一点。

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. 首先,我会成立了循环周期中通过每个阵列项目

  2. 将所有你目前正在使用这个循环寄存器

  3. 创建第二个循环来处理乘法

  4. 存储值并退出第二循环

  5. 恢复你的第一个循环的寄存器

  6. 您递增循环计数器,然后跳回循环顶部

下面是一些示例code,通过在阵列中的每个元素周期和它们相乘在一起。结果之后被存储在数组ARR3。

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

这篇关于LC3汇编语言 - 创建2个阵列,乘,加总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆