汇编和C语言版本的 DCT程序

来源:互联网 发布:php工资系统源码下载 编辑:程序博客网 时间:2024/05/07 02:29

 

 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;                                Jeff Xu                            ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;                               2009,11,17                     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

 AREA DCT,CODE,READONLY
ROW EQU 8
COL EQU 8
    ENTRY
start
    LDR R2,=MID

;;;;;;;;;;;;;;;;;;;transpose achieve MATRIX to MATRIXT;;;;;;;;;;;;;;;;;;;;;;;;;;;;  

transpose
    LDR R0,=MATRIX
    LDR R4,=MATRIXT
    MOV R5,#ROW
    MOV R6,#0        ; set i = 0
do1
    CMP R6,R5        ;judge i == ROW
    BEQ multiply    
    BL do2
    ADD R6,R6,#1        ;i++
    B do1
do2
    MOV R7,#0        ;set j=0;
do222
    CMP R7,R5        ;judge j == COL
    BEQ return
    MLA R8,R6,R5,R7        ;R8 = i*ROW+j
    MLA R9,R7,R5,R6        ;R9 = j*ROW+i
    LDR R10,[R0,R8,LSL #2]        ;R10<-MATRIX[i][j]
    STR R10,[R4,R9,LSL #2]        ;R10->MATRIXT[j][i]
    ADD R7,R7,#1        ;j++
    B do222

return
    MOV PC,LR

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;multiply achieve MATRIX * DATA,  and store the result into MID;;;;;;;;;;;;;;;;;;;;;;;;;;;
multiply
     LDR R0,=MATRIX
    LDR R1,=DATA
    MOV R4,#ROW
    MOV R5,#0        ;set i=0
    
do3
    CMP R5,R4        ;compare i == ROW
    BEQ multiply2
    BL storelr
    ADD R5,R5,#1        ;i++
    B do3

storelr
    MOV R9,LR        ; store the LR   下一次 MOV PC,R9就是跳到do3 add r5...了
    
do4
    MOV R6,#0        ;set j=0

do5
    CMP R6,R4        ;judge j == ROW
    BEQ return2
    BL do6            ;;;;;;;;;;;;;;;;;;LR address is on here
    ADD R6,R6,#1        ;j++
    B do5
do6
    MOV R8,#0        ;set k=0
    MOV R10,#0        ;initial R10=0
do7
    MLA R13,R5,R4,R6        ;R13 = i* ROW +j
do8
    CMP R8,R4        ;judge k == ROW
    BEQ do9
    MLA R7,R5,R4,R8            ;R7=i*ROW+k            
    MLA R11,R8,R4,R6    ;R11 = k*ROW +j;
    LDR R12,[R0,R7,LSL #2]        ;R12 = MATRIX[i][k] although R7 multiply 4, but the R7 does not changed
    LDR R3,[R1,R11,LSL #2]        ;R3 = DATA[k][j]
    MLA R10,R12,R3,R10    ;R10 = R12 * R3 + R10
    ADD R8,R8,#1        ;k++
    B do8    
do9
    STR R10,[R2,R13,LSL #2]
    MOV PC,LR        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;jump to do5 in "BL do6"
    
return2
    MOV PC,R9        ;JUMP TO  storelr in order to add i
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;multiply2 achieve MID * MATRIXT,  and store the result into RESULT;;;;;;;;;;;;;;;;;;;;;;;;;;;
multiply2
    LDR R0,=RESULT
    LDR R1,=MATRIXT
    MOV R4,#ROW
    MOV R5,#0        ;set i=0
    
do33
    CMP R5,R4        ;compare i == ROW
    BEQ stop
    BL storelr2
    ADD R5,R5,#1        ;i++
    B do33

storelr2
    MOV R9,LR        ; store the LR
    
do44
    MOV R6,#0        ;set j=0

do55
    CMP R6,R4        ;judge j == ROW
    BEQ return22
    BL do66
    ADD R6,R6,#1        ;j++
    B do55
do66
    MOV R8,#0        ;set k=0
    MOV R10,#0        ;initial R10=0
do77
    MLA R13,R5,R4,R6        ;R13 = i* ROW +j
do88
    CMP R8,R4        ;judge k == ROW
    BEQ do99
    MLA R7, R5,R4,R8        ;R7=i*ROW+k     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;pay attention to this R13
    MLA R11,R8,R4,R6    ;R11 = j*ROW +k;
    LDR R12,[R2,R7,LSL #2]        ;R12 = MID[i][j]
    LDR R3,[R1,R11,LSL #2]        ;R3 = MATRIXT[j][k]
    MLA R10,R12,R3,R10    ;R10 = R12 * R3 + R10
    ADD R8,R8,#1        ;k++
    B do88    

do99
    MOV R10,R10,ASR #14
    STR R10,[R0,R13,LSL #2]
    MOV PC,LR    
    
return22
    MOV PC,R9        ;JUMP TO  storelr in order to add i
    
    
stop
    MOV R0,#0x18
    LDR R1,=0x20026
    SWI    0x123456
    
    AREA DCTDATA,DATA,READWRITE
DATA DCD 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
MATRIX DCD 45,45,45,45,45,45,45,45,62,53,35,12,-12,-35,-53,-62,59,24,-24,-59,-59,-24,24,59,53,-12,-62,-35,35,62,12,-53,45,-45,-45,45,45,-45,-45,45,35,-62,12,53,-53,-12,62,35,24,-59,59,-24,-24,59,-59,24,12,-35,53,-62,62,-53,35,-12
MATRIXT DCD 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
MID DCD 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
RESULT DCD 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

    END

 

 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////                     Jeff Xu                        ////////////////////////////////////////////////
///////////////////////////////                   2009.11.08                    ////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <stdio.h>
#include <math.h>


#define PI 3.14159
#define ROW 8
#define COL 8

int Data[ROW][COL]={0};
int A[ROW][COL]={0};

int getdata( );                                            //Get the initial data, Data and A
void transpose( int **a1, int **at1);                    //transpose the matrix A into At
void multiplymatrix( int **a2, int **b2, int **c2 );    //multiply the two matrix ,and store the result in matrix **c//
void printmatrix(int **a3);                                //print the matrx;


int main(void)
{
    int i,j;
    int At[ROW][COL] = {0};
    int Mid[ROW][COL] = {0};
    int Res[ROW][COL] = {0};
    getdata( );
    printf("The Data is :/n");
    printmatrix( (int **)Data );
    getchar();
    printf("The A is :/n");
    printmatrix( (int **)A );
    getchar();

    transpose( (int **)A, (int **)At);
    multiplymatrix( (int **)A, (int **)Data, (int **)Mid );
    printf("The Mid is :/n");
    printmatrix( (int **)Mid );
        getchar();
    multiplymatrix( (int **)Mid, (int **)At, (int **)Res );
   
    printf("The result is :/n");
    printmatrix( (int **)Res );
        getchar();
    // turn right for 20bit, as A multiply 1024 before;
    for(i=0;i < ROW; i++)
    {
        for( j = 0; j< COL; j++)
        {           
                Res[i][j] = (int) ( Res[i][j] >> 14);
        }
    }
    printf("the unsigned char Result is :/n");
    printmatrix(  (int **)Res );
    getchar();
   
    printf("Press Enter to quit/n");
    getchar();
    return 0;
}


//extern void doInitial( (int **)A, (int **)Data);
int getdata()                        //Creat the matrix A and the initial Data;
{
    int i, j, k = 0 ;
    double u1, u2;
    u1 = sqrt(1.0/COL);
    u2 = sqrt(2.0/COL);

    for(i=0;i < ROW; i++)
    {
        for( j = 0; j< COL; j++)
        {
            Data[i][j] = ++k ;
            if( i ==0)
            {
                A[i][j] = int (128* u1 * cos( ( (2 * j) + 1 ) * i * PI / ( 2 * COL ) ) );
            }
            else
            {
                A[i][j] = int (128* u2 * cos( ( (2 * j) + 1 ) * i * PI / ( 2 * COL ) ) );
            }

        }
    }
    return 0;
}
   
void transpose( int **a1, int **at1)                    //transpose the matrix A into At
{
    int i,j;
    for(i=0;i<ROW;i++)
    {
        for(j=0;j<COL;j++)
        {
             *( ( (int*)at1+j * COL) + i ) = *( ( (int*)a1+i * ROW) + j );
        }
    }
}

void multiplymatrix( int **a2, int **b2, int **c2 )    //multiply the two matrix ,and store the result in matrix **c//
{
    int i,j,k,temp;
    for(i=0;i < ROW; i++)
    {
        for( j = 0; j< COL; j++)
        {
            temp = 0;
            for( k=0; k<ROW;k++)
            {
                temp += ( *( ( (int*)a2 + i * COL) + k ) ) * ( *( ( (int*)b2 + k * ROW) + j ) ) ;
            }
            *(((int*)c2+i*ROW)+j) = temp;
        }
    }
}


void printmatrix(int **a3)                            //print the matrx;
{
    int i,j;
    for(i=0;i<ROW;i++)
    {
        for(j=0;j<COL;j++)
        {
            printf("%d,",*(((int*)a3+i*ROW)+j));
        }
        printf("/n");
    }
}


原创粉丝点击