Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. # Benjamin H. Somma
  2. # Homework 2.4 Edit Dist (Assembly)
  3. # 2/14/2020
  4.  
  5. /*
  6. NOTES:
  7. 1. The label for the first string should be string1 and the label for the second string should be string2.
  8. 2. The edit distance between string1 and string2 should be placed in EAX.
  9. 3. For each string please allocate space for 100 bytes.
  10. a. .space 100
  11. --- Syntax Notes ---
  12. 1. ret = Pop PC
  13. 2. cld = clear the direction flag
  14. */
  15.  
  16. .data
  17. value:
  18. .long 0
  19.  
  20. len_i:
  21. .long 0
  22.  
  23. len_j:
  24. .long 0
  25.  
  26. # d[100][100]# "you must allocate space for 100 bytes in your final submission"
  27. string1:
  28. .space 100
  29.  
  30. string2:
  31. .space 100
  32.  
  33. array: # repeat 1,000 times
  34. .rept 1000
  35. .long 0
  36. .endr
  37.  
  38. .text
  39. .global _start
  40.  
  41. # int m = strlen(string1)
  42. # int n = strlen(string2)
  43. strlen:
  44. push %ecx
  45. push %edi
  46. sub %ecx, %ecx
  47. mov $-1,%ecx
  48. sub %al, %al
  49. cld # clear the direction flag
  50. repne scasb # repeat while not equal, if direction EDI -= 1 else EDI += 1
  51. neg %ecx
  52. sub $1, %ecx
  53. mov %ecx, %eax
  54. pop %edi
  55. pop %ecx
  56. ret # Pop PC
  57.  
  58. min1_or_2:
  59. cmp %ebx, %eax
  60. jge minimum_1
  61. ret
  62.  
  63. minimum_1:
  64. movl %ebx,%eax
  65. ret
  66.  
  67. min_eax:
  68. cmp value,%eax
  69. jge minnimum_2
  70. ret
  71.  
  72. minnimum_2:
  73. movl value,%eax
  74. ret
  75.  
  76. # # #
  77. /*
  78. ### --- Left over code from own creation of .c program --- ###
  79. int LevenshteinDistance(char* string1, char* string2){
  80. int i,j,distance_of_two_inputs,substitution_cost#
  81. ... ... ...
  82. // "return d[m, n]#"
  83. distance_of_two_inputs = d[i-1][j-1]#
  84.  
  85. return distance_of_two_inputs#
  86. }
  87. */
  88. # # #
  89.  
  90. _start:
  91.  
  92. lea string1, %edi
  93. call strlen
  94. movl %eax, len_i
  95. lea string2, %edi
  96. call strlen
  97. movl %eax, len_j
  98. movl $0, %ecx
  99.  
  100. # // "source prefixes can be transformed into empty string by dropping all characters"
  101. # for(i = 0# i <= m# i++){
  102. # d[i][0] = i#
  103. # }
  104. loop_one:
  105. cmpl %ecx, len_i
  106. jle exit_loop_one
  107. movl len_j, %eax
  108. imull %ecx
  109. movl %ecx, array(,%eax,4)
  110. incl %ecx
  111. jmp loop_one
  112.  
  113. exit_loop_one:
  114. movl $0, %ecx
  115.  
  116. # // target prefixes can be reached from empty source prefix by inserting every character
  117. # for(j = 0# j <= n# j++) {
  118. # d[0][j] = j#
  119. # }
  120. loop_two:
  121. cmpl %ecx, len_j
  122. jle exit_loop_two
  123. movl %ecx, array(,%ecx,4)
  124. incl %ecx
  125. jmp loop_two
  126.  
  127. exit_loop_two:
  128. movl $1, %ecx
  129.  
  130. # for(i = 1# i <= m# i++) {
  131. # for(j = 1# j <= n# j++) {
  132. # if (string1[i-1] == string2[j-1]){
  133. # substitution_cost = 0#
  134. # // substitution_cost = d[i-1][j-1]#
  135. # }
  136. # else {
  137. # substitution_cost = 1#
  138. # // substitution_cost = d[i-1][j-1]+1#
  139. # }
  140.  
  141. # d[i][j] = minimum_of_two(minimum_of_two(d[i-1][j]+1,d[i][j-1]+1), (d[i-1][j-1] + substitution_cost))#
  142.  
  143. # }
  144. # }
  145. first_for_large_loop:
  146. cmpl %ecx, len_i
  147. jle exit_first_for_large_loop
  148. movl $1, %edx
  149.  
  150. second_for_large_loop:
  151. cmpl %edx, len_j
  152. jle exit_second_for_large_loop
  153. movl len_i, %esi
  154. movl len_j, %edi
  155. sub $1, %esi
  156. sub $1, %edi
  157. mov string1(,%esi,4),%esi
  158. mov string2(,%edi,4),%edi
  159. sub %esi, %edi
  160. cmpl $0, %edi
  161. jz possibility2
  162.  
  163. possibility1:
  164. movl %ecx, %eax
  165. sub $1, %eax
  166. movl %edx, %ebx
  167. sub $1, %ebx
  168. imull len_j
  169. add %ebx, %eax
  170. movl array(,%eax,4),%eax
  171. add $1, %eax
  172.  
  173. do_work:
  174. movl %eax, value
  175. movl %ecx, %eax
  176. sub $1, %eax
  177. imull len_j
  178. add %edx,%eax
  179. movl array(,%eax,4),%eax
  180. add $1, %eax
  181. movl %eax, %ebx
  182. movl %ecx, %eax
  183. imull len_j
  184. add %edx,%eax
  185. sub $1, %eax
  186. movl array(,%eax,4),%eax
  187. add $1, %eax
  188. call min1_or_2
  189. call min_eax
  190. movl %eax, %ebx
  191. movl %ecx, %eax
  192. imull len_j
  193. add %edx,%eax
  194. movl %ebx,array(,%eax,4)
  195. incl %edx
  196. jmp second_for_large_loop
  197.  
  198. possibility2:
  199. movl %ecx, %eax
  200. sub $1, %eax
  201. movl %edx, %ebx
  202. sub $1, %ebx
  203. imull len_j # signed multiplication
  204. add %ebx, %eax
  205. movl array(,%eax,4),%eax
  206. jmp do_work
  207.  
  208. exit_second_for_large_loop:
  209. incl %ecx
  210. jmp first_for_large_loop
  211.  
  212. exit_first_for_large_loop:
  213. movl %ecx, %eax
  214. sub $1, %eax
  215. movl %edx, %ebx
  216. sub $1, %ebx
  217. imull len_j
  218. add %ebx, %eax
  219. movl array(,%eax,4),%eax
  220.  
  221. done:
  222. nop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement