为什么我的矩阵在寄存器中没有正确显示?

时间:2014-10-17 19:06:14

标签: assembly matrix add avr subtraction

我一直在研究一些代码来添加和减去matricies并将它们放在一个新的矩阵中。有了这个,我将它们放在寄存器中,mymatrix3中的值是添加值,而mymatrix4是减去的值。这个程序现在只能部分工作,我无法弄清楚原因。

这是我的代码

.device ATmega2560

;this program adds two matrices of size 5x5 
;first we initialize matrices (matrix1 and matrix2) and then store the result in matrix

;first get a pointer to matrix1 and put it in Register X: R27:R26
ldi R26, low(mymatrix1); get the low order byte of the address
ldi R27, high(mymatrix1); get the higher order byte of the address
ldi R16, 50; starting value of matrix1
ldi R17, 25; number of values to initialize is matrix size 2x2
 ;like for loop in C (do the following as long as R17>0)
loop: 
    st X+, r16;
    inc R16; next value to initialize
    dec R17; decrement count R17
    breq nextinit; initialized first matrix. go to next one
    rjmp loop

;now initialize second matrix
nextinit:
    ldi R26, low(mymatrix2); get the low order byte of the address
    ldi r27, high(mymatrix2); get the higher order byte of the address
    ldi R16, 1; starting value of matrix2
    ldi R17, 25; number of values to initialize
 ;like for loop in C (do the following as long as R17>0)
loop1: 
    st X+, r16;
    inc R16; next value to initialize
    dec R17;
    breq add_matrix; initialized second matrix. go to add them
    rjmp loop1

add_matrix:
    ;get the address of matrix1 into X register
    ldi R26, low(mymatrix1); get the low order byte of the address
    ldi R27, high(mymatrix1); get the higher order byte of the address
    ;get the address of matrix2 into Y register
    ldi R28, low(mymatrix2); get the low order byte of the address
    ldi R29, high(mymatrix2); get the higher order byte of the address
    ;get the address of matrix into Z register
    ldi R30, low(mymatrix3); get the low order byte of the address
    ldi R31, high(mymatrix3); get the higher order byte of the address

    ;add matrices in a loop
    ldi R17, 4; number of values to initialize
loop2:
    ld R0,X+;get the element from matrix1 into R0
    ld R1,Y+;get the element from matrix2 into R1
    add R0, R1; R0=R0+R1 add the elements
    st Z+,R0; store the result
    dec R17;
    breq sub_matrix; we are done adding matrices
    rjmp loop2; if not continue

sub_matrix:
    ;get the address of matrix1 into X register
    ldi R26, low(mymatrix1); get the low order byte of the address
    ldi R27, high(mymatrix1); get the higher order byte of the address
    ;get the address of matrix2 into Y register
    ldi R28, low(mymatrix2); get the low order byte of the address
    ldi R29, high(mymatrix2); get the higher order byte of the address
    ;get the address of matrix into Z register
    ldi R30, low(mymatrix4); get the low order byte of the address
    ldi R31, high(mymatrix4); get the higher order byte of the address

    ;add matrices in a loop
    ldi R17, 25; number of values to initialize

loop3:
    ld R0,X+;get the element from matrix1 into R0
    ld R1,Y+;get the element from matrix2 into R1
    sub R0, R1; R0=R0+R1 add the elements
    st Z+,R0; store the result
    dec R17;
    breq done; we are done adding matrices
    rjmp loop3; if not continue

done:
   rjmp done; infinite loop

;initialize the matrix values

; store for final result
.dseg
.org 0x000200 ; ATmega2560 data memory starts at 0x000200
mymatrix1: .byte 4; defines 4 bytes of storage for matrix1 
mymatrix2: .byte 4; defines 4 bytes of storage for matrix2
mymatrix3: .byte 4; defines 4 bytes of storage for matrix
mymatrix4: .byte 4

0 个答案:

没有答案