Guest User

Untitled

a guest
Nov 19th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. ;-------------------------------------------------------------------------------------------------------------------------------------------------------
  2. .186 ;You may need to uncomment this to get some of the instructions working (e.g. shl, dest, count)
  3. ;-------------------------------------------------------------------------------------------------------------------------------------------------------
  4. data segment ; data segment. Keyword db means define byte. You can also define word (dw)
  5. n dw 5 ;nth fib. number
  6. firstS db 'The Fibonacci number of order $' ;First part of output string
  7. secS db ' is $' ;Second part of output string
  8. newL db 0ah,'$' ;New line character
  9. temp db 1 dup(?),'$' ;Used for output
  10. data ends
  11.  
  12. ; stack segment
  13. stack1 segment stack
  14. db 100 dup(?) ; This is the stack of 100 bytes
  15. stack1 ends
  16.  
  17.  
  18. code segment
  19. assume cs:code, ds:data, ss:stack1
  20.  
  21. start:
  22. ;Perform initialization
  23. mov ax, data ;Put the starting address of the data segment into the ax register (must do this first)
  24. mov ds, ax ;Put the starting address of the data segment into the ds register (where it belongs)
  25.  
  26. mov ax, stack1 ;Put the starting address of the stack into the ax register (must do this first)
  27. mov ss, ax ;Put the starting address of the stack segment into the ss register (where it belongs)
  28. ;-------------------------------------------------------------------------------------------------------------------------------------------------------
  29. ;****************** Write Code Here ******************
  30.  
  31. mov cx, n ;Move n into cx (counter for the loop)
  32.  
  33. mov bl, 0 ;Initialize bl to 0 (used to store result)
  34. mov dl, 0 ;Store current fib. # (i.e. 0 is the 0th fib. #)
  35. call pEvery ;Output Fibonacci number
  36.  
  37. cmp cx, 0 ;Check to see if cx is 0
  38. je fEnd ;Jump to end if cx == 0
  39.  
  40. mov al, 0 ;Initialize al to 0
  41. mov bl, 1 ;Initialize bl to 1
  42.  
  43. inc dl ;Increment dl to indicate the current fib. #
  44.  
  45.  
  46. dec cx ;Decrement cx for algorithm
  47. call pEvery ;Output Fibonacci numbers
  48. jz fEnd
  49.  
  50. loopS: ;Loop label
  51.  
  52. add bl, al ;Add al and bl and store result in bl (nth fib. #)
  53.  
  54. dec cx ;Decrement counter
  55. inc dl ;Increment dl to indicate current fib. #
  56.  
  57. call pEvery ;Output Fibonacci number
  58.  
  59. cmp cx, 0 ;Check if finished processing
  60. jg loopS ;Continue while cx>0
  61.  
  62. fEnd:
  63. ;-------------------------------------------------------------------------------------------------------------------------------------------------------
  64. mov ah, 4ch ;Set up code to specify return to dos
  65. int 21h ;Interpt number 21 (Return control to dos prompt)
  66.  
  67. pEvery proc
  68. push ax ;Back up ax
  69. push dx ;Back up dx
  70.  
  71. mov temp, dl ;Put dl into memory for output
  72. add temp, 30h ;Convert dl into character
  73.  
  74. mov ah, 9 ;Specify print string service routine
  75.  
  76. lea dx, firstS ;Put the first part of output string into dx
  77. int 21h ;Request service from the OS
  78.  
  79. lea dx, temp ;Put address of current number of fib. into dx
  80. int 21h ;Request service from the OS
  81.  
  82. lea dx, secS ;Put the second part of output string into dx
  83. int 21h ;Request service from the OS
  84.  
  85. mov temp, bl ;Put current fib. # into memory
  86. add temp, 30h ;Convert it to a character
  87.  
  88. lea dx, temp ;Put address of current fib. number into dx
  89. int 21h ;Request service from the OS
  90.  
  91. lea dx, newL ;Put address for new line string into dx
  92. int 21h ;Request service from the OS
  93.  
  94. pop dx ;Restore dx
  95. pop ax ;Restore the value of ax
  96.  
  97. ret
  98. pEvery endp
  99. ;*****************************************************
  100.  
  101. code ends
  102.  
  103. end start
  104. ;-------------------------------------------------------------------------------------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment