Advertisement
Il_Voza

1.5 Generate reverse vector without support vector

May 3rd, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Given a vector of DIM word in memory, replace it with reverse vector (without using another vector support)
  2.  
  3. ;BEFORE                AFTER
  4. ;423                   9
  5. ;3191                  3
  6. ;23                    -412
  7. ;11                    11
  8. ;-412                  23
  9. ;3                     3191
  10. ;9                     423
  11.  
  12. ;1° SOLUTION:
  13.  
  14. DIM EQU 7
  15. .MODEL small
  16. .STACK
  17. .DATA
  18. vett DW 423, 3191, 23, 11, -412, 3, 9
  19. .CODE
  20. .STARTUP
  21. MOV CX, DIM/2 ;I write so because I will use XCHG instruction that exchanges the values of two operands, then it will cycle DIM/2 times
  22. XOR SI,SI
  23. MOV DI,(DIM-1)*2 ;DI takes the value of index of last element, in fact vett is DW, so, first to all, I do
  24.                  ;DIM-1 because vector's index starts from 0, and then I multiply for 2 because, being DW,
  25.                  ;I slide vector's cells two by two, because DW's dimension is 16 bit
  26. ciclo: MOV AX,vett[SI]
  27.        XCHG vett[DI],AX ;By XCHG I exchange values of two operands
  28.        MOV vett[SI],AX
  29.        ADD SI,2
  30.        SUB DI,2
  31.        DEC CX
  32.        CMP CX,0
  33.        JNZ ciclo
  34. .EXIT
  35. END
  36.  
  37.  
  38. ;2° SOLUTION:
  39.  
  40. DIM EQU 7
  41. .MODEL small
  42. .STACK
  43. .DATA
  44. vett DW 423, 3191, 23, 11, -412, 3, 9
  45. .CODE
  46. .STARTUP  
  47. MOV CX,DIM
  48. XOR SI,SI ;I initialize SI to 0
  49.  
  50. ciclo1: PUSH vett[SI] ;PUSH takes value of vett[SI]
  51.                       ;and places it into stack memory, on top
  52.         ADD SI,2
  53.         DEC CX
  54.         CMP CX,0
  55.         JNZ ciclo1
  56.        
  57. MOV CX,DIM
  58. XOR SI,SI
  59.  
  60. ciclo2: POP vett[SI] ;POP takes values that I placed into stack memory
  61.                      ;and places them into vett[SI]
  62.         ADD SI,2
  63.         DEC CX
  64.         CMP CX,0
  65.         JNZ ciclo2
  66.  
  67. .EXIT
  68. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement