User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Assembly section within the Software Development category of DaniWeb, a massive community of 374,572 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,803 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Assembly advertiser:
Aug 28th, 2006
Views: 4,841
This is a program I wrote for my x86 assembly class which generates matrices, multiplies them, and computes how long the arithmetic took. It uses Irvine32.inc which came with the textbook.
Last edited : Dec 24th, 2006.
asm Syntax
  1. title Matrix (matrix.asm)
  2.  
  3. ; Dani Horowitz
  4. ; CSC111 x86 Assembly Programming
  5. ; This program randomly generates two 3x3 integer matrices, A and B
  6. ; It then prints out the product of the matrices
  7.  
  8. INCLUDE Irvine32.inc
  9.  
  10. ;--------------------------------------------------
  11. .stack ; begin stack segment
  12.  
  13. ;--------------------------------------------------
  14.  
  15. TimerStart PROTO,
  16. pSavedTime: PTR DWORD
  17.  
  18. TimerStop PROTO,
  19. pSavedTime: PTR DWORD
  20.  
  21. .data ; begin data segment
  22. A dword 10000 dup(0) ; matrix A
  23. B dword 10000 dup(0) ; matrix B
  24. num_rows dword 0;
  25. num_cols dword 0;
  26. col_counter dword 0 ; inner loop counter
  27. row_counter dword 0 ; outer loop counter
  28. counter byte 0 ; this will always come in handy
  29. sum dword 0
  30.  
  31. ; used for CPU timer
  32. newmatrix dword 1
  33. time dword 0
  34.  
  35. ; used to generate product matrix
  36. working_row dword 0; (0=1st row, 12=2nd row, 24=3rd row)
  37. working_col dword 0; (0=1st col, 4=2nd col, 8=3rd col)
  38.  
  39. ; ######################
  40. ; ### MAY BE EDITED: ###
  41. ; ######################
  42. matrix_size = 5 ; 2= [3x3] 9= [10x10]
  43. upbound dword 50 ; upper bound value for matrix values
  44. num_matrices = 5 ; how many matrices to generate - 1
  45. ; ######################
  46.  
  47. nextrow = (matrix_size + 1) * 4
  48.  
  49. ; ###########################
  50. ; ##### STOPWATCH TIMER #####
  51. ; ###########################
  52.  
  53. msg BYTE " milliseconds have elapsed", 0dh, 0ah, 0
  54. timer1 dword ?
  55.  
  56. ; ###########################
  57.  
  58. ;--------------------------------------------------
  59. .code ; begin code segment
  60.  
  61. ;--------------------------------------------------
  62.  
  63. ;--------------------------------------------------
  64. TimerStart PROC uses eax esi,
  65. pSavedTime: PTR DWORD
  66.  
  67. INVOKE GetTickCount
  68. mov esi, pSavedTime
  69. mov [esi], eax
  70. ret
  71. TimerStart ENDP
  72.  
  73. TimerStop PROC uses esi,
  74. pSavedTime: PTR DWORD
  75.  
  76. INVOKE GetTickCount
  77. mov esi, pSavedTime
  78. sub eax, [esi]
  79. ret
  80. TimerStop ENDP
  81. ;--------------------------------------------------
  82.  
  83. ;--------------------------------------------------
  84. determinesize PROC
  85. ;
  86. ; Calculate num_rows and num_cols based on matrix_size
  87. ;--------------------------------------------------
  88. mov eax, matrix_size
  89. mov ebx, nextrow
  90. mul ebx
  91. mov num_rows, eax
  92.  
  93. mov eax, matrix_size
  94. mov ebx, 4
  95. mul ebx
  96. mov num_cols, eax
  97. ret
  98. ;--------------------------------------------------
  99. determinesize ENDP
  100. ;--------------------------------------------------
  101.  
  102. ;--------------------------------------------------
  103. setvalue PROC
  104. ;
  105. ; Set a value in the matrix to a random #
  106. ;--------------------------------------------------
  107. mov eax, upbound ; set range between 1 and upbound
  108. call RandomRange ; put a random int in eax
  109. inc eax
  110. mov [esi], eax ; put the value in the array
  111. ret
  112. ;--------------------------------------------------
  113. setvalue ENDP
  114. ;--------------------------------------------------
  115.  
  116. ;--------------------------------------------------
  117. createA PROC
  118. ;
  119. ; Initialize generation of matrix A
  120. ;--------------------------------------------------
  121. mov esi, OFFSET A ; finds the start of the matrix
  122. call traversematrix ; fills in the matrix
  123. ret
  124. ;--------------------------------------------------
  125. createA ENDP
  126. ;--------------------------------------------------
  127.  
  128. ;--------------------------------------------------
  129. createB PROC
  130. ;
  131. ; Initialize generation of matrix B
  132. ;--------------------------------------------------
  133. mov esi, OFFSET B ; finds the start of the matrix
  134. call traversematrix ; fills in the matrix
  135. ret
  136. ;--------------------------------------------------
  137. createB ENDP
  138. ;--------------------------------------------------
  139.  
  140. ;--------------------------------------------------
  141. printvalue PROC
  142. ;
  143. ; Print a value neatly
  144. ;--------------------------------------------------
  145. push eax
  146. call WriteDec ; print value
  147. mov al, ' '
  148. call WriteChar ; print a space
  149. pop eax
  150. ret
  151. ;--------------------------------------------------
  152. printvalue ENDP
  153. ;--------------------------------------------------
  154.  
  155. ;--------------------------------------------------
  156. traversematrix PROC
  157. ;
  158. ; Traverse each row of the matrix
  159. ;--------------------------------------------------
  160.  
  161. call Crlf ; line feed
  162. mov row_counter, 0 ; set to first row
  163.  
  164. NewRow:
  165.  
  166. mov col_counter, 0 ; set to first column
  167.  
  168. NewCol:
  169.  
  170. ; -- begin inner loop --
  171.  
  172. call setvalue ; put value in location
  173. call printvalue ; print value while we're here
  174. add esi, 4 ; next data member of array
  175.  
  176. inc col_counter ; did we reach the last column
  177. cmp col_counter, matrix_size ; of the row?
  178. jle NewCol ; if not, add another element
  179.  
  180. ; -- end inner loop --
  181.  
  182. call Crlf
  183. inc row_counter ; new row
  184.  
  185. cmp row_counter, matrix_size ; stop at 3rd row
  186. jle NewRow
  187.  
  188. ret
  189. ;--------------------------------------------------
  190. traversematrix ENDP
  191. ;--------------------------------------------------
  192.  
  193. ;--------------------------------------------------
  194. addelements PROC
  195. ;
  196. ; Add A1B1 + A2B2 + A3B3
  197. ;--------------------------------------------------
  198.  
  199. mov eax, working_row ; row_counter = working_row
  200. mov ebx, working_col ; col_counter = working_col
  201.  
  202. mov row_counter, eax
  203. mov col_counter, ebx
  204.  
  205. mov counter, 0
  206. mov sum, 0
  207.  
  208. Next:
  209.  
  210. ; -- begin loop --
  211.  
  212. call multiplyelements ; multiply AxBx
  213. add sum, eax ; accumulate the sum
  214.  
  215. add row_counter, 4 ; next elements
  216. add col_counter, nextrow
  217.  
  218. inc counter ; increment loop counter
  219.  
  220. cmp counter, matrix_size ; stop at 3rd row
  221. jle Next
  222.  
  223. ; -- end loop --
  224.  
  225. mov eax, sum
  226.  
  227. ret
  228. ;--------------------------------------------------
  229. addelements ENDP
  230. ;--------------------------------------------------
  231.  
  232. ;--------------------------------------------------
  233. multiplyelements PROC
  234. ;
  235. ; Multiply Ax and Bx
  236. ;--------------------------------------------------
  237.  
  238. mov esi, OFFSET A ; find matrix A
  239. add esi, row_counter ; find location we want
  240. mov eax, [esi] ; put into eax
  241.  
  242. mov esi, OFFSET B ; find matrix B
  243. add esi, col_counter ; find location we want
  244. mov ebx, [esi] ; put into ebx
  245.  
  246. mul ebx ; do multiplication
  247.  
  248. ret
  249. ;--------------------------------------------------
  250. multiplyelements ENDP
  251. ;--------------------------------------------------
  252.  
  253. ;--------------------------------------------------
  254. multiplymatrix PROC
  255. ;
  256. ; Do matrix multiplication
  257. ;--------------------------------------------------
  258.  
  259. call Crlf
  260. mov working_row, 0 ; start on the first row
  261.  
  262. NewRow:
  263.  
  264. mov working_col, 0 ; go to first column
  265.  
  266. NewCol:
  267.  
  268. ; -- begin inner loop --
  269.  
  270. call addelements ; do all the addition and multiplication
  271. call printvalue ; print value
  272.  
  273. add working_col, 4 ; get ready to do the next column
  274.  
  275. mov eax, working_col ; did we reach the end of a row?
  276. cmp eax, num_cols
  277. jle NewCol
  278.  
  279. ; -- end inner loop --
  280.  
  281. call Crlf ; new row
  282. add working_row, nextrow
  283.  
  284. mov eax, working_row ; did we reach the last row?
  285. cmp eax, num_rows
  286. jle NewRow
  287.  
  288. call Crlf
  289. ret
  290. ;--------------------------------------------------
  291. multiplymatrix ENDP
  292. ;--------------------------------------------------
  293.  
  294. ;--------------------------------------------------
  295. main proc
  296. ;--------------------------------------------------
  297. call Clrscr ; clear screen
  298. call Randomize ; randomize seed
  299. mov ebx, num_matrices ; how many matrices to generate
  300.  
  301. ; ########## TIMER ##########
  302. INVOKE TimerStart, ; start the timer
  303. ADDR timer1
  304. ; ########## TIMER ##########
  305.  
  306. NextMatrix:
  307.  
  308. ; -- begin loop --
  309.  
  310. call createA ; generate matrix A
  311. call createB ; generate matrix B
  312. call determinesize ; determine size of matrices
  313. call multiplymatrix ; multiply matrices
  314.  
  315. inc newmatrix
  316. mov eax, newmatrix
  317. cmp eax, num_matrices
  318. jle NextMatrix
  319.  
  320. ; ########## TIMER ##########
  321. INVOKE TimerStop,
  322. ADDR timer1
  323.  
  324. call WriteDec
  325. mov edx, OFFSET msg
  326. call WriteString
  327. ; ########## TIMER ##########
  328.  
  329. exit
  330.  
  331. main endp
  332. end main
  333. ;--------------------------------------------------
Comments (Newest First)
samo | Newbie Poster | Mar 15th, 2007
hi
i need a code to multipe two arrays 3*3 ,, can you help me
its in 8086

c u
hamed22 | Newbie Poster | Jan 4th, 2007
can you write aprogram to see sin(t) wave
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:55 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC