Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. ;
  2. ; CS1022 Introduction to Computing II 2018/2019
  3. ; Lab 2 - Subarray
  4. ;
  5.  
  6. N EQU 7
  7. M EQU 3
  8.  
  9. AREA RESET, CODE, READONLY
  10. ENTRY
  11.  
  12. ; initialize system stack pointer (SP)
  13. LDR SP, =0x40010000
  14. LDR R1, =LARGE_A
  15. LDR R2, =SMALL_A
  16. ;
  17. ; Write your program here to determine whether SMALL_A
  18. ; is a subarray of LARGE_A
  19. ;
  20. ; Store 1 in R0 if SMALL_A is a subarray and zero otherwise
  21. ;
  22.  
  23. MOV R0,#0; boolean subArray = false
  24. MOV R3,#0;
  25. forI ;
  26. CMP R3,#N; for(i = 0; i <array.length;i++)
  27. BGE endForI;{
  28. MOV R4,R3; for(j =0;j <array.length;j++)
  29. forJ ; {
  30. CMP R4,#N
  31. BGE endForJ
  32.  
  33. MOV R7,#N; r7 = arrayLarge.length
  34. MUL R7,R3,R7; r7 = i * arrayLarge.length
  35. ADD R7,R7,R4; index = r7 + coloumn[j]
  36. LDR R7,[R1,R7,LSL#2];r7 = arrayLarge[i][j]
  37.  
  38. MOV R8,#0 ;r8 = 0
  39. LDR R8,[R2,R8,LSL#2];r8 = arraySmall[0][0] (We do this as we only have to check if the first element of the small array
  40. ; is equal to the element of the large array. This means we only loop when we have an equal element.
  41. CMP R7,R8 ;if(arrayLarge[i][j] == arraySmall[0][0]
  42. BNE notSubArray ;{
  43. MOV R0,#1 ; subArray = true;
  44. ; (We set subArray to true. This stays true unless the next indexs are not equal until we reach the end.)
  45. MOV R9,R3 ; int x =i
  46. MOV R5,#0 ;
  47. forK ; for(k = 0; k < arraySmall.length;k++)
  48. CMP R5,#M ; {
  49. BGE endForK ;
  50. MOV R10,R4 ; int y = j
  51. ;
  52. MOV R6,#0 ; for(l=0;l<arraySmall.length;l++)
  53. forL ; {
  54. CMP R6,#M ; r6 = arraySmall.length
  55. BGE endForL ;
  56. MOV R7,#N ; r7 = arrayLarge.length
  57. MUL R7,R9,R7 ; r7 = x * arrayLarge.length(rowsize)
  58. ADD R7,R7,R10 ; index = x + coloumn(y)
  59. LDR R7,[R1,R7,LSL#2]; r7 = arrayLarge[x][y]
  60.  
  61. MOV R8,#M ; r8 = arraySmall.length
  62. MUL R8,R5,R8 ; r8 = k * arrayLarge.length(rowsize)
  63. ADD R8,R8,R6 ; index = k + coloumn(j)
  64. LDR R8,[R2,R8,LSL #2]; r8 = arraySmall[k][l]
  65.  
  66. CMP R7,R8 ; if(arrayLarge[x][y] != arraySmall[k][l])
  67. BEQ isEql ; {
  68. MOV R0,#0 ; subArray = false;
  69. isEql ; }
  70. ADD R10,R10,#1 ; y++
  71. ADD R6,R6,#1 ; }
  72. B forL ; }
  73. endForL
  74. ADD R9,R9,#1 ; x++
  75. ADD R5,R5,#1 ;}
  76. B forK ;
  77. endForK
  78. notSubArray
  79. ADD R4,R4,#1;
  80. B forJ ; }
  81. endForJ
  82. ADD R3,R3,#1;
  83. B forI ;}
  84. endForI
  85. STOP B STOP
  86.  
  87.  
  88. ;
  89. ; test data
  90. ;
  91.  
  92. LARGE_A DCD 48, 37, 15, 44, 3, 17, 26
  93. DCD 2, 9, 12, 18, 14, 33, 16
  94. DCD 13, 20, 1, 22, 7, 48, 21
  95. DCD 27, 19, 44, 49, 44, 18, 10
  96. DCD 29, 17, 22, 4, 46, 43, 41
  97. DCD 37, 35, 38, 34, 16, 25, 0
  98. DCD 17, 0, 48, 15, 27, 35, 11
  99.  
  100. SMALL_A DCD 49, 44, 18
  101. DCD 4, 46, 43
  102. DCD 34, 16, 25
  103.  
  104. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement