Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 12th, 2012  |  syntax: None  |  size: 1.58 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Matrix in assembly mxn dimension
  2. ; multi-segment executable file template.
  3.  
  4. data segment
  5. matrix db 1, 2, 3, 4, 5, 6, 7, 8, 9 ; load matrix in memory
  6.  
  7. ends
  8.  
  9. stack segment
  10. dw 128 dup(0)
  11. ends
  12.  
  13. code segment
  14. start:
  15. ; set segment registers:
  16. mov ax, data
  17. mov ds, ax
  18. mov es, ax
  19.  
  20.  
  21.  
  22. mov bx, matrix ; move matrix to offset bx
  23. mov ch, 3
  24. mov cl, 0
  25.  
  26.  
  27. COLUMNAHEAD:
  28.  
  29. mov dl, [bx] ; get the first element of matrix in dl
  30. add dl, 30h ; add to show number
  31. mov ah, 02h
  32. int 21h ; print first number
  33. inc cl
  34.  
  35. cmp cl, ch ; compare if the counter is at the end of column
  36.  
  37.  
  38. jge ROWAHEAD ; if greater go to row
  39. add bx, 3 ; if not inc offset for 3
  40. jmp COLUMNAHEAD
  41.  
  42.  
  43.  
  44.  
  45.  
  46. ROWAHEAD:
  47. inc cl ; 1 element of roe
  48. inc bx ; inc offset for one place
  49. mov dl, [bx] ; get the number in dl
  50. add dl, 30h ; convert to number
  51. mov ah, 02h
  52. int 21h ; print the number
  53.  
  54. cmp cl, 5
  55. je COLUMNAHEAD
  56. jne ROWAHEAD
  57.  
  58.  
  59. COLUMNBACK:
  60. inc cl
  61. sub bx, 3
  62. mov dl, [bx]
  63. add dl, 30h
  64. mov ah, 02h
  65. int 21h
  66. cmp cl, 7
  67. jne COLUMNBACK
  68. je ROWBACK
  69.  
  70. ROWBACK:
  71. dec bx
  72. mov dl, [bx]
  73. add dl, 30h
  74. mov ah, 02h
  75. int 21h
  76. JMP MIDDLE
  77.  
  78.  
  79. MIDDLE:
  80. add bx, 3
  81. mov dl, [bx]
  82. add dl, 30h
  83. mov ah, 02h
  84. int 21h
  85.  
  86. JMP END
  87.  
  88. END:
  89.  
  90.  
  91. this is the code i wrote. it works for the matrix
  92. 1, 2, 3,
  93. 4, 5, 6,
  94. 7, 8, 9 and print 1, 4, 7, 8, 9, 6, 3, 2, 5
  95.        
  96. COLUMNAHEAD(M); ROWAHEAD(N-1); COLUMNBACK(M-1); ROWBACK(N-2);
  97.        
  98. - pointer to the current element (your ds:bx)
  99. - values M and N, kept until algorithm stopped
  100. - shift parameter, let be D
  101.        
  102. D := 0
  103. label:
  104. if (M-D==0) stop
  105. COLUMNAHEAD(M-D)
  106. inc D
  107. if (N-D==0) stop
  108. ROWAHEAD(N-D)
  109. if (M-D==0) stop
  110. COLUMNBACK(M-D)
  111. inc D
  112. if (N-D==0) stop
  113. ROWBACK(N-D)
  114. goto label