fork download
  1. title Fibonacci [FIB.ASM]
  2. .model small
  3. .stack 100h
  4.  
  5. .data
  6.  
  7. msg1 db "Please enter the number of terms in the fibonacci sequence to display:",0DH,0AH,"$" ;message to user
  8. msg2 db "Displaying Fibonacci sequence",0DH,0AH,"$" ; message to user
  9. msg3 db ", ","$" ; formating - coma
  10. msg4 db "Please enter a suitable number in range [1-25]","$"
  11. num1 dw 0 ;this is a numeric variable
  12. num2 dw 0 ; this is a numeric variable
  13. input dw 0 ; this is a numeric variable
  14. fibn_1 DW 1 ; word numeric value n-1 value
  15. fibn_2 DW 0 ; word numeric value n-2 value
  16. fib DW 0 ; word numeric value
  17. temp DW 0 ; word numeric value
  18. saveCount DW 0 ;counter storage
  19.  
  20.  
  21. .code
  22.  
  23.  
  24. main proc
  25.  
  26. mov ax,@data ; set up data segment
  27. mov ds,ax
  28.  
  29. ; clear the screen
  30.  
  31. mov ah,9 ; send message with instructions for user
  32. mov dx,offset msg1
  33. int 21h
  34.  
  35.  
  36. call keyin ;gets user input
  37.  
  38. SUB AL, 48 ;changes ASCII value into numeric value for further processing
  39. MOV AH,0
  40. MOV num1 , AX ;saves user input to variable num1
  41. SUB AL,'0'
  42. MOV BL,10
  43. MUL BL
  44.  
  45. call keyin ;gets user input
  46.  
  47. SUB AL, 48 ;changes ASCII value into numeric value for further processing
  48. MOV AH,0
  49. MOV num2 , AX ;saves user input to variable num2, so now we have both digits
  50.  
  51.  
  52. CHECKINPUT:
  53.  
  54. CMP AX,25
  55. JAE WARNING
  56. JMP STEP1
  57.  
  58. WARNING:
  59.  
  60. mov ah,09
  61. mov dx,offset msg4
  62. int 21h
  63. JMP CHECKINPUT
  64.  
  65.  
  66.  
  67. STEP1:
  68. ;multiplying num1 10 times
  69.  
  70. MOV CX,10
  71. repeat1: ; loop 10 times
  72.  
  73. MOV AX, NUM1 ;copies value of num1 to AX
  74. ADD input, AX ;adds value from AX
  75. DEC CX ;decrements the counter
  76. JNZ repeat1 ;loops until counter = 0
  77.  
  78. MOV AX, num2 ;adding the value from num2 so if user entered 83, so it was num1=8 num2=3, then we multiplied 8x10=80, so we add 80+3 and we get 83
  79. ADD input, AX
  80.  
  81. call newLine
  82. mov ah,9 ; send informative message to user regarding displaying the sequence
  83. mov dx,offset msg2
  84. int 21h
  85. call newLine
  86.  
  87. call displayFib
  88. call newLine
  89.  
  90. mov ax,4C00h ; return to DOS
  91. int 21h
  92.  
  93. main endp
  94.  
  95.  
  96. newLine proc ;procedure displays new line
  97.  
  98. mov dx,0Dh ;line feed
  99. mov ah,2
  100. int 21h
  101. mov dx,0Ah ;carriage return
  102. mov ah,2
  103. int 21h
  104. ret
  105.  
  106. newLine endp
  107.  
  108.  
  109. keyin proc
  110.  
  111. mov ah, 1 ; getting a key from the keyboard
  112. int 21h
  113. ret
  114.  
  115. keyin endp
  116.  
  117. displayFib proc
  118.  
  119. ;display zero as a 0'th term
  120. MOV DX, 30h ; move value 30 hexadecimal to DX, which represents 0
  121. call display
  122. MOV AX, input
  123. CMP AX, 0 ;if the input is 0 in hexadecimal ASCII value then jump to finish
  124. JE finish_it
  125.  
  126. mov ah,9 ; formating - coma
  127. mov dx,offset msg3
  128. int 21h
  129.  
  130. ;display the 1st term
  131. MOV DX, 31h ; move value 31 hexadecimal to DX, which represents 1
  132. call display
  133. CMP input, 1 ;if the input is 1 in hexadecimal ASCII value then jump to finish
  134. JE finish_it
  135.  
  136. MOV CX, input ;intializing counter, knowing that first 2 terms were displayed already
  137. SUB CX, 2
  138.  
  139. repeat:
  140.  
  141. mov ah,9 ; formating - coma
  142. mov dx,offset msg3
  143. int 21h
  144.  
  145. MOV AX, fibn_2 ; calculating the n'th term of a sequence n = (n-1) + (n-2)
  146. ADD AX, fibn_1
  147. MOV fib, AX
  148. MOV DX, fib
  149.  
  150. PUSH CX ;saving the state of the counter as it will be modified in the displayNum
  151. call displayNum
  152. ;display the n'th term (current term)
  153.  
  154. POP CX ;restoring state of the counter
  155. MOV AX, fibn_1 ; n-1 in the next round of a loop will be n-2
  156. MOV fibn_2, AX
  157. MOV AX, fib ;n'th term in the next round will be n-1
  158. MOV fibn_1, AX
  159. DEC CX ;decrementing counter
  160.  
  161. JNZ repeat ; loop until counter = 0
  162.  
  163. finish_it:
  164.  
  165.  
  166. ret
  167. displayFib endp
  168.  
  169.  
  170. displayNum proc ;display numbers including these with more than one digit
  171.  
  172. MOV AX, fib ;copying fib to temp
  173. MOV temp, AX
  174. MOV CX,0 ;initializing counter to 0
  175.  
  176. loop1:
  177. ;dividng fib by 10 and pushing reminder on the stock
  178. INC CX ;incrementing counter
  179. MOV ax, temp
  180. MOV bx, 10
  181. SUB dx, dx ;set dx to zero
  182. DIV bx ;BX will contain integer division result and DX remainder
  183. PUSH DX
  184. MOV temp, AX ;temp will hold value of itself integer devided by 10
  185. TEST AX,AX
  186. JNZ loop1
  187.  
  188. loop2:
  189.  
  190. POP DX
  191. ADD DX, 30h
  192. call display
  193. DEC CX
  194. JNZ loop2 ;loop until all digits on stack are popped and counter =0
  195.  
  196. ret
  197. displayNum endp
  198.  
  199.  
  200. display proc ; display of a single character
  201.  
  202. mov ah, 6
  203. int 21h
  204. ret
  205.  
  206. display endp
  207.  
  208.  
  209. end main
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.asm:1: error: parser: instruction expected
prog.asm:2: error: parser: instruction expected
prog.asm:3: error: parser: instruction expected
prog.asm:24: error: parser: instruction expected
prog.asm:32: error: comma, colon, decorator or end of line expected after operand
prog.asm:61: error: comma, colon, decorator or end of line expected after operand
prog.asm:83: error: comma, colon, decorator or end of line expected after operand
prog.asm:93: error: symbol `main' redefined
prog.asm:93: error: parser: instruction expected
prog.asm:96: error: parser: instruction expected
prog.asm:106: error: symbol `newLine' redefined
prog.asm:106: error: parser: instruction expected
prog.asm:109: error: parser: instruction expected
prog.asm:115: error: symbol `keyin' redefined
prog.asm:115: error: parser: instruction expected
prog.asm:117: error: parser: instruction expected
prog.asm:127: error: comma, colon, decorator or end of line expected after operand
prog.asm:142: error: comma, colon, decorator or end of line expected after operand
prog.asm:167: error: symbol `displayFib' redefined
prog.asm:167: error: parser: instruction expected
prog.asm:170: error: parser: instruction expected
prog.asm:197: error: symbol `displayNum' redefined
prog.asm:197: error: parser: instruction expected
prog.asm:200: error: parser: instruction expected
prog.asm:206: error: symbol `display' redefined
prog.asm:206: error: parser: instruction expected
prog.asm:209: error: parser: instruction expected
ld: cannot find prog.o: No such file or directory
stdout
Standard output is empty