2016-04-07 64 views
0
.data 

.align 2 
array: .space 150 
row:  .asciiz "Enter the amount of rows in your array(2-6): " 
column: .asciiz "Enter the amout of colums in your array(2-6): " 
data: .asciiz "Enter an array element (in row order): " 
rowsum: .asciiz "The row sums are: " 
product: .asciiz "The column prdoucts are: " 

# ask for rows and columns 
# figure out size of the array rows X columns 
# save that as the array size 

.text 
main: 

     la $t7,array 

     # Print row string 
     li $v0, 4 # system call code for print_str 
     la $a0, row # address of string to print 
     syscall 

     # ask for row integer value 
     li $v0, 5 # syscall read_int 
     syscall 
     move $s0, $v0 

     # print column string 
     li $v0, 4  # system call code for print_str 
     la $a0, column # address of string to print 
     syscall 

     # ask for column integer value 
     li $v0, 5 # syscall read_int 
     syscall 
     move $s1, $v0 

     # matrix 
     mult $s0, $s1 # multiply row by column 
     mflo $t0  # move the value from our lower register 
     move $t1, $zero # set terminal to zero 

     read_loop: 

      # print data string 
      li $v0, 4 
      la $a0, data 
      syscall 

      # ask for element array value 
      li $v0, 6 
      syscall 

      # move the value to the array 
      swc1 $f0, 0($t7) 

      # increment position 
      addi $t1, $t1, 1 
      addi $t7, $t7, 4 

      # count of matrix 
      bne  $t1, $t0, read_loop # read input based on how big the matrix is. stop when we hit the end. 

我不知道如何正確循環數組以獲取每行的值作爲行總和打印出來。Mips 2d陣列rowsum和列產品

我有矩陣尺寸從上方# matrix

但是考慮到矩陣大小,我怎樣才能讀1-6輸入。我知道每個輸入是4個字節。所以行可以從4,8,12,16,24,28字節變化。

這將需要一個分支之前或之後,我開始循環,或多個分支?

row_loop: 

    # add every row value. 
    # Need to read 1-6 values 
    # Can I use the row value stored in $s0? 

    # print sum result 
    li $v0, 4  # system call code for print_str 
    la $a0, rowsum # address of string to print 
    syscall 

col_loop: 

    # print column product result 
    li $v0, 4  # system call code for print_str 
    la $a0, product # address of string to print 
    syscall 

li $v0, 10 
syscall 

回答

0

一個解決這個問題的問題是調試

例如,您創建一個程序版本。現在運行它。它爲提示爲所有數據。從控制檯輸入此值需要時間。如果有錯誤,你必須改變一些東西,重建和重新運行。並... 重新輸入重新數據。

在測試過程中,將測試矩陣放入.float語句中更容易,因此沒有數據輸入。

我會在節目的一開始就做什麼,是提示測試號碼的用戶:

Enter test number (-1=stop, -2=all, 0=prompt, >0 is predefined test): 

的另一個問題是,你如何驗證結果?換句話說,您如何生成正確的結果,以便手動或自動驗證程序輸出?因此,我建議一個生成測試矩陣並計算行數和列產品的C程序。

計算總和和產品的C代碼可以用作您希望生成的asm的僞代碼的雙重用途。

通過這樣做,我們分離[設計]的算法實施ASM是algoritm的

之後,機械翻譯C代碼變得更簡單了。


這裏的一個測試發生器程序:

#include <stdio.h> 
#include <stdlib.h> 

int opt_gen;       // generate asm statements 
int opt_T;        // number of tests 

#define ROWMIN    2 
#define ROWMAX    6 

int rowmax; 
int colmax; 
int stride; 
float mtx[ROWMAX * ROWMAX]; 
float rowsum[ROWMAX]; 
float colprod[ROWMAX]; 

int tstno; 

// getdim -- get matrix dimension 
int 
getdim(void) 
{ 
    int dim; 

    while (1) { 
     dim = lrand48() % (ROWMIN + ROWMAX); 
     dim += ROWMIN; 
     if (dim <= ROWMAX) 
      break; 
    } 

    return dim; 
} 

// arrshow -- output an array 
void 
arrshow(float *arr,int arrmax,const char *sym,const char *title) 
{ 
    char *sep; 
    int arridx; 

    printf("\t# %s\n",title); 
    printf("tst%2.2d_%s:\n",tstno,sym); 

    printf("\t.float\t"); 
    sep = ""; 

    for (arridx = 0; arridx < arrmax; ++arridx) { 
     printf("%s%g",sep,arr[arridx]); 
     sep = ","; 
    } 

    printf("\n"); 
} 

// dotst -- generate test 
void 
dotst(void) 
{ 
    int rowidx; 
    int colidx; 
    float val; 
    char *sep; 

    rowmax = getdim(); 
    colmax = getdim(); 

#if 0 
    stride = ROWMAX; 
#else 
    stride = colmax; 
#endif 

    if (tstno > 1) 
     printf("\n"); 
    printf("tst%2.2d:\n",tstno); 
    printf("\t.word\t%d\t# test number\n",tstno); 
    if (tstno < opt_T) 
     printf("\t.word\ttst%2.2d\t# pointer to next test\n",tstno + 1); 
    else 
     printf("\t.word\t0\t# pointer to next test\n"); 
    printf("\t.word\t%d\t# rowmax\n",rowmax); 
    printf("\t.word\t%d\t# colmax\n",colmax); 

    // generate the matrix 
    printf("tst%2.2d_mtx:\n",tstno); 
    for (rowidx = 0; rowidx < rowmax; ++rowidx) { 
     printf("\t.float\t"); 
     sep = ""; 
     for (colidx = 0; colidx < colmax; ++colidx) { 
      val = drand48(); 
      mtx[(rowidx * stride) + colidx] = val; 
      printf("%s%g",sep,val); 
      sep = ","; 
     } 
     printf("\t# row %d\n",rowidx); 
    } 

    // generate row sums 
    for (rowidx = 0; rowidx < rowmax; ++rowidx) { 
     val = 0.0; 
     for (colidx = 0; colidx < colmax; ++colidx) 
      val += mtx[(rowidx * stride) + colidx]; 
     rowsum[rowidx] = val; 
    } 

    // generate column products 
    for (colidx = 0; colidx < colmax; ++colidx) { 
     val = 1.0; 
     for (rowidx = 0; rowidx < rowmax; ++rowidx) 
      val *= mtx[(rowidx * stride) + colidx]; 
     colprod[colidx] = val; 
    } 

    // print row sums 
    arrshow(rowsum,rowmax,"rowsum","row sums [indexed by row]"); 

    // print column products 
    arrshow(colprod,colmax,"colprod","column products [indexed by column]"); 
} 

// dogen -- generate tests for asm 
void 
dogen(void) 
{ 

    for (tstno = 1; tstno <= opt_T; ++tstno) 
     dotst(); 
} 

// main -- main program 
int 
main(int argc,char **argv) 
{ 
    char *cp; 

    --argc; 
    ++argv; 

    for (; argc > 0; --argc, ++argv) { 
     cp = *argv; 
     if (*cp != '-') 
      break; 

     switch (cp[1]) { 
     case 'g': 
      opt_gen = 1; 
      break; 
     case 'T': 
      opt_T = atoi(cp + 2); 
      break; 
     } 
    } 

    if (opt_T <= 0) 
     opt_T = 20; 

    srand48(0xDEADBEEF); 

#if 0 
    if (opt_gen) 
     dogen(); 
    else 
     doloop(); 
#else 
    dogen(); 
#endif 

    return 0; 
} 

這裏的測試發生器程序的輸出:

tst01: 
    .word 1 # test number 
    .word tst02 # pointer to next test 
    .word 6 # rowmax 
    .word 2 # colmax 
tst01_mtx: 
    .float 0.0480832,0.925215 # row 0 
    .float 0.511196,0.342399 # row 1 
    .float 0.0231708,0.77913 # row 2 
    .float 0.458211,0.657552 # row 3 
    .float 0.192286,0.13829 # row 4 
    .float 0.718801,0.00220358 # row 5 
    # row sums [indexed by row] 
tst01_rowsum: 
    .float 0.973298,0.853595,0.8023,1.11576,0.330576,0.721005 
    # column products [indexed by column] 
tst01_colprod: 
    .float 3.60698e-05,4.94575e-05 

tst02: 
    .word 2 # test number 
    .word tst03 # pointer to next test 
    .word 6 # rowmax 
    .word 6 # colmax 
tst02_mtx: 
    .float 0.849782,0.546568,0.937853,0.864108,0.305004,0.00756697 # row 0 
    .float 0.739542,0.708572,0.207939,0.5616,0.11377,0.567681 # row 1 
    .float 0.0564002,0.221843,0.808113,0.0481284,0.733861,0.291474 # row 2 
    .float 0.49113,0.243682,0.547646,0.954115,0.0828184,0.377302 # row 3 
    .float 0.750289,0.984919,0.190717,0.0230619,0.832171,0.466742 # row 4 
    .float 0.0156874,0.505762,0.220668,0.411513,0.0195486,0.726932 # row 5 
    # row sums [indexed by row] 
tst02_rowsum: 
    .float 3.51088,2.8991,2.15982,2.69669,3.2479,1.90011 
    # column products [indexed by column] 
tst02_colprod: 
    .float 0.000204894,0.010429,0.00363223,0.000211483,3.43085e-05,0.000160282 

tst03: 
    .word 3 # test number 
    .word tst04 # pointer to next test 
    .word 2 # rowmax 
    .word 5 # colmax 
tst03_mtx: 
    .float 0.970571,0.231291,0.436997,0.822847,0.771793 # row 0 
    .float 0.820889,0.118123,0.628661,0.148443,0.447973 # row 1 
    # row sums [indexed by row] 
tst03_rowsum: 
    .float 3.2335,2.16409 
    # column products [indexed by column] 
tst03_colprod: 
    .float 0.796731,0.0273208,0.274723,0.122146,0.345742 

tst04: 
    .word 4 # test number 
    .word tst05 # pointer to next test 
    .word 3 # rowmax 
    .word 6 # colmax 
tst04_mtx: 
    .float 0.618794,0.303102,0.28536,0.883616,0.0425207,0.370875 # row 0 
    .float 0.233947,0.784831,0.109683,0.21696,0.117412,0.224096 # row 1 
    .float 0.629818,0.325219,0.204525,0.294107,0.760322,0.728664 # row 2 
    # row sums [indexed by row] 
tst04_rowsum: 
    .float 2.50427,1.68693,2.94265 
    # column products [indexed by column] 
tst04_colprod: 
    .float 0.0911756,0.0773644,0.00640146,0.056383,0.00379587,0.0605604 

tst05: 
    .word 5 # test number 
    .word tst06 # pointer to next test 
    .word 3 # rowmax 
    .word 4 # colmax 
tst05_mtx: 
    .float 0.358229,0.576659,0.339637,0.137463 # row 0 
    .float 0.521153,0.798593,0.834109,0.137639 # row 1 
    .float 0.0203656,0.614799,0.10029,0.903942 # row 2 
    # row sums [indexed by row] 
tst05_rowsum: 
    .float 1.41199,2.29149,1.6394 
    # column products [indexed by column] 
tst05_colprod: 
    .float 0.0038021,0.283125,0.0284116,0.0171028 

tst06: 
    .word 6 # test number 
    .word tst07 # pointer to next test 
    .word 2 # rowmax 
    .word 2 # colmax 
tst06_mtx: 
    .float 0.0710807,0.79236 # row 0 
    .float 0.166013,0.243027 # row 1 
    # row sums [indexed by row] 
tst06_rowsum: 
    .float 0.86344,0.40904 
    # column products [indexed by column] 
tst06_colprod: 
    .float 0.0118003,0.192564 

tst07: 
    .word 7 # test number 
    .word tst08 # pointer to next test 
    .word 2 # rowmax 
    .word 6 # colmax 
tst07_mtx: 
    .float 0.525881,0.767693,0.615366,0.684871,0.658207,0.0927842 # row 0 
    .float 0.711593,0.570353,0.650287,0.810089,0.557573,0.23852 # row 1 
    # row sums [indexed by row] 
tst07_rowsum: 
    .float 3.3448,3.53841 
    # column products [indexed by column] 
tst07_colprod: 
    .float 0.374213,0.437856,0.400164,0.554807,0.366998,0.0221309 

tst08: 
    .word 8 # test number 
    .word tst09 # pointer to next test 
    .word 2 # rowmax 
    .word 2 # colmax 
tst08_mtx: 
    .float 0.21128,0.484587 # row 0 
    .float 0.292336,0.980593 # row 1 
    # row sums [indexed by row] 
tst08_rowsum: 
    .float 0.695866,1.27293 
    # column products [indexed by column] 
tst08_colprod: 
    .float 0.0617646,0.475182 

tst09: 
    .word 9 # test number 
    .word tst10 # pointer to next test 
    .word 6 # rowmax 
    .word 4 # colmax 
tst09_mtx: 
    .float 0.424862,0.0964705,0.915842,0.77812 # row 0 
    .float 0.291517,0.279445,0.129929,0.757929 # row 1 
    .float 0.475404,0.655913,0.0729621,0.00775924 # row 2 
    .float 0.263494,0.222636,0.183699,0.381668 # row 3 
    .float 0.290414,0.101607,0.0826193,0.1799 # row 4 
    .float 0.337035,0.911792,0.,0.769976 # row 5 
    # row sums [indexed by row] 
tst09_rowsum: 
    .float 2.21529,1.45882,1.21204,1.0515,0.654541,2.0312 
    # column products [indexed by column] 
tst09_colprod: 
    .float 0.00151858,0.000364714,1.63368e-06,0.00024193 

tst10: 
    .word 10 # test number 
    .word tst11 # pointer to next test 
    .word 3 # rowmax 
    .word 2 # colmax 
tst10_mtx: 
    .float 0.927257,0.0815354 # row 0 
    .float 0.921494,0.600199 # row 1 
    .float 0.594305,0.504229 # row 2 
    # row sums [indexed by row] 
tst10_rowsum: 
    .float 1.00879,1.52169,1.09853 
    # column products [indexed by column] 
tst10_colprod: 
    .float 0.507811,0.0246757 

tst11: 
    .word 11 # test number 
    .word tst12 # pointer to next test 
    .word 2 # rowmax 
    .word 2 # colmax 
tst11_mtx: 
    .float 0.216704,0.835793 # row 0 
    .float 0.589206,0.622495 # row 1 
    # row sums [indexed by row] 
tst11_rowsum: 
    .float 1.0525,1.2117 
    # column products [indexed by column] 
tst11_colprod: 
    .float 0.127683,0.520277 

tst12: 
    .word 12 # test number 
    .word tst13 # pointer to next test 
    .word 2 # rowmax 
    .word 6 # colmax 
tst12_mtx: 
    .float 0.799863,0.268639,0.370484,0.509241,0.345844,0.0568862 # row 0 
    .float 0.0554226,0.346426,0.63628,0.933348,0.322327,0.726197 # row 1 
    # row sums [indexed by row] 
tst12_rowsum: 
    .float 2.35096,3.02 
    # column products [indexed by column] 
tst12_colprod: 
    .float 0.0443305,0.0930634,0.235732,0.475299,0.111475,0.0413106 

tst13: 
    .word 13 # test number 
    .word tst14 # pointer to next test 
    .word 6 # rowmax 
    .word 4 # colmax 
tst13_mtx: 
    .float 0.817304,0.609118,0.754376,0.712011 # row 0 
    .float 0.557027,0.676689,0.358653,0.237841 # row 1 
    .float 0.167921,0.0921725,0.80346,0.215156 # row 2 
    .float 0.741307,0.405931,0.0239142,0.236406 # row 3 
    .float 0.913345,0.867056,0.177716,0.71794 # row 4 
    .float 0.172439,0.627781,0.0726845,0.879335 # row 5 
    # row sums [indexed by row] 
tst13_rowsum: 
    .float 2.89281,1.83021,1.27871,1.40756,2.67606,1.75224 
    # column products [indexed by column] 
tst13_colprod: 
    .float 0.00892551,0.00839458,6.71508e-05,0.00543784 

tst14: 
    .word 14 # test number 
    .word tst15 # pointer to next test 
    .word 4 # rowmax 
    .word 5 # colmax 
tst14_mtx: 
    .float 0.0444047,0.985519,0.291545,0.747228,0.443979 # row 0 
    .float 0.711087,0.534498,0.508636,0.748564,0.629221 # row 1 
    .float 0.0881322,0.779609,0.928828,0.415243,0.578218 # row 2 
    .float 0.259088,0.992353,0.260463,0.376166,0.624528 # row 3 
    # row sums [indexed by row] 
tst14_rowsum: 
    .float 2.51268,3.13201,2.79003,2.5126 
    # column products [indexed by column] 
tst14_colprod: 
    .float 0.000720995,0.407525,0.0358751,0.0873703,0.100881 

tst15: 
    .word 15 # test number 
    .word tst16 # pointer to next test 
    .word 2 # rowmax 
    .word 5 # colmax 
tst15_mtx: 
    .float 0.530169,0.086245,0.070489,0.164902,0.109771 # row 0 
    .float 0.509496,0.366888,0.128401,0.252501,0.0109814 # row 1 
    # row sums [indexed by row] 
tst15_rowsum: 
    .float 0.961577,1.26827 
    # column products [indexed by column] 
tst15_colprod: 
    .float 0.270119,0.0316423,0.00905088,0.0416379,0.00120544 

tst16: 
    .word 16 # test number 
    .word tst17 # pointer to next test 
    .word 6 # rowmax 
    .word 4 # colmax 
tst16_mtx: 
    .float 0.49342,0.289792,0.366951,0.921996 # row 0 
    .float 0.721338,0.213262,0.911397,0.248489 # row 1 
    .float 0.855794,0.155251,0.765392,0.926219 # row 2 
    .float 0.608776,0.790685,0.603981,0.902971 # row 3 
    .float 0.282509,0.951709,0.509161,0.48244 # row 4 
    .float 0.772914,0.416316,0.309304,0.291831 # row 5 
    # row sums [indexed by row] 
tst16_rowsum: 
    .float 2.07216,2.09449,2.70266,2.90641,2.22582,1.79036 
    # column products [indexed by column] 
tst16_colprod: 
    .float 0.0404899,0.00300582,0.024348,0.0269772 

tst17: 
    .word 17 # test number 
    .word tst18 # pointer to next test 
    .word 2 # rowmax 
    .word 6 # colmax 
tst17_mtx: 
    .float 0.46297,0.216732,0.732362,0.40035,0.3825,0.976923 # row 0 
    .float 0.126104,0.340014,0.6256,0.958355,0.327664,0.891615 # row 1 
    # row sums [indexed by row] 
tst17_rowsum: 
    .float 3.17184,3.26935 
    # column products [indexed by column] 
tst17_colprod: 
    .float 0.0583822,0.0736917,0.458166,0.383678,0.125332,0.871039 

tst18: 
    .word 18 # test number 
    .word tst19 # pointer to next test 
    .word 3 # rowmax 
    .word 2 # colmax 
tst18_mtx: 
    .float 0.749495,0.954779 # row 0 
    .float 0.0476438,0.743898 # row 1 
    .float 0.679273,0.342579 # row 2 
    # row sums [indexed by row] 
tst18_rowsum: 
    .float 1.70427,0.791542,1.02185 
    # column products [indexed by column] 
tst18_colprod: 
    .float 0.024256,0.243319 

tst19: 
    .word 19 # test number 
    .word tst20 # pointer to next test 
    .word 6 # rowmax 
    .word 3 # colmax 
tst19_mtx: 
    .float 0.562899,0.984625,0.495615 # row 0 
    .float 0.603749,0.240201,0.905512 # row 1 
    .float 0.918128,0.649461,0.366722 # row 2 
    .float 0.295691,0.0880594,0.0199822 # row 3 
    .float 0.211216,0.127633,0.265338 # row 4 
    .float 0.132539,0.585006,0.534659 # row 5 
    # row sums [indexed by row] 
tst19_rowsum: 
    .float 2.04314,1.74946,1.93431,0.403733,0.604187,1.2522 
    # column products [indexed by column] 
tst19_colprod: 
    .float 0.00258285,0.00100995,0.000466548 

tst20: 
    .word 20 # test number 
    .word 0 # pointer to next test 
    .word 6 # rowmax 
    .word 3 # colmax 
tst20_mtx: 
    .float 0.351139,0.880843,0.407246 # row 0 
    .float 0.0540183,0.30472,0.0619591 # row 1 
    .float 0.137091,0.783589,0.200142 # row 2 
    .float 0.586626,0.480319,0.0743153 # row 3 
    .float 0.243075,0.125374,0.427872 # row 4 
    .float 0.459117,0.607819,0.747043 # row 5 
    # row sums [indexed by row] 
tst20_rowsum: 
    .float 1.63923,0.420698,1.12082,1.14126,0.796321,1.81398 
    # column products [indexed by column] 
tst20_colprod: 
    .float 0.000170237,0.00769839,0.000119961