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 "Press any key to continue","$"
  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. main proc
  24. mov ax,@data ; set up data segment
  25. mov ds,ax
  26.  
  27. ; clear the screen
  28.  
  29. mov ah,9 ; send message with instructions for user
  30. mov dx,offset msg1
  31. int 21h
  32.  
  33. call keyin ;gets user input
  34. SUB AL, 48 ;changes ASCII value into numeric value for further processing
  35. mov num1 , AX ;saves user input to variable num1
  36. call keyin ;gets user input
  37. SUB AL, 48 ;changes ASCII value into numeric value for further processing
  38. mov num2 , AX ;saves user input to variable num2, so now we have both digits
  39.  
  40. ;multiplying num1 10 times
  41.  
  42. MOV CX, 10
  43. repeat1: ; loop 10 times
  44. MOV AX, NUM1 ;copies value of num1 to AX
  45. ADD input, AX ;adds value from AX
  46. DEC CX ;decrements the counter
  47. JNZ repeat1 ;loops until counter = 0
  48.  
  49. 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
  50. ADD input, AX
  51.  
  52. call newLine
  53. mov ah,9 ; send informative message to user regarding displaying the sequence
  54. mov dx,offset msg2
  55. int 21h
  56. call newLine
  57.  
  58. call displayFib
  59.  
  60. call newLine
  61. mov ax,4C00h ; return to DOS
  62. int 21h
  63.  
  64. main endp
  65.  
  66.  
  67. newLine proc ;procedure displays new line
  68. mov dx,0Dh ;line feed
  69. mov ah,2
  70. int 21h
  71. mov dx,0Ah ;carriage return
  72. mov ah,2
  73. int 21h
  74. ret
  75. newLine endp
  76.  
  77.  
  78.  
  79.  
  80. keyin proc
  81. mov ah, 1 ; getting a key from the keyboard
  82. int 21h
  83. ret
  84. keyin endp
  85.  
  86. displayFib proc
  87. ;display zero as a 0'th term
  88. MOV DX, 30h ; move value 30 hexadecimal to DX, which represents 0
  89. call display
  90. MOV AX, input
  91. CMP AX, 0 ;if the input is 0 in hexadecimal ASCII value then jump to finish
  92. JE finish_it
  93.  
  94. mov ah,9 ; formating - coma
  95. mov dx,offset msg3
  96. int 21h
  97.  
  98. ;display the 1st term
  99. MOV DX, 31h ; move value 31 hexadecimal to DX, which represents 1
  100. call display
  101. CMP input, 1 ;if the input is 1 in hexadecimal ASCII value then jump to finish
  102. JE finish_it
  103.  
  104. MOV CX, input ;intializing counter, knowing that first 2 terms were displayed already
  105. SUB CX, 2
  106.  
  107. repeat:
  108. mov ah,9 ; formating - coma
  109. mov dx,offset msg3
  110. int 21h
  111.  
  112. MOV AX, fibn_2 ; calculating the n'th term of a sequence n = (n-1) + (n-2)
  113. ADD AX, fibn_1
  114. MOV fib, AX
  115. MOV DX, fib
  116. MOV saveCount, CX ;saving the state of the counter as it will be modified in the displayNum
  117. call displayNum
  118. ;display the n'th term (current term)
  119. MOV CX, saveCount ;restoring state of the counter
  120. MOV AX, fibn_1 ; n-1 in the next round of a loop will be n-2
  121. MOV fibn_2, AX
  122. MOV AX, fib ;n'th term in the next round will be n-1
  123. MOV fibn_1, AX
  124. DEC CX ;decrementing counter
  125. JNZ repeat ; loop until counter = 0
  126.  
  127. finish_it:
  128.  
  129. ret
  130. displayFib endp
  131.  
  132.  
  133. displayNum proc ;display numbers including these with more than one digit
  134.  
  135. MOV AX, fib ;copying fib to temp
  136. MOV temp, AX
  137. MOV CX,0 ;initializing counter to 0
  138. loop1:
  139. ;dividng fib by 10 and pushing reminder on the stock
  140. INC CX ;incrementing counter
  141. MOV ax, temp
  142. MOV bx, 10
  143. SUB dx, dx ;set dx to zero
  144. DIV bx ;BX will contain integer division result and DX remainder
  145. PUSH DX
  146. MOV temp, AX ;temp will hold value of itself integer devided by 10
  147. TEST AX,AX
  148. JNZ loop1
  149.  
  150. loop2:
  151. POP DX
  152. ADD DX, 30h
  153. call display
  154. DEC CX
  155. JNZ loop2 ;loop until all digits on stack are popped and counter =0
  156.  
  157. ret
  158.  
  159.  
  160. displayNum endp
  161.  
  162.  
  163. display proc ; display of a single character
  164. mov ah, 6
  165. int 21h
  166. ret
  167. display endp
  168.  
  169.  
  170. 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:23: error: parser: instruction expected
prog.asm:30: error: comma, colon, decorator or end of line expected after operand
prog.asm:54: error: comma, colon, decorator or end of line expected after operand
prog.asm:64: error: symbol `main' redefined
prog.asm:64: error: parser: instruction expected
prog.asm:67: error: parser: instruction expected
prog.asm:75: error: symbol `newLine' redefined
prog.asm:75: error: parser: instruction expected
prog.asm:80: error: parser: instruction expected
prog.asm:84: error: symbol `keyin' redefined
prog.asm:84: error: parser: instruction expected
prog.asm:86: error: parser: instruction expected
prog.asm:95: error: comma, colon, decorator or end of line expected after operand
prog.asm:109: error: comma, colon, decorator or end of line expected after operand
prog.asm:130: error: symbol `displayFib' redefined
prog.asm:130: error: parser: instruction expected
prog.asm:133: error: parser: instruction expected
prog.asm:160: error: symbol `displayNum' redefined
prog.asm:160: error: parser: instruction expected
prog.asm:163: error: parser: instruction expected
prog.asm:167: error: symbol `display' redefined
prog.asm:167: error: parser: instruction expected
prog.asm:170: error: parser: instruction expected
ld: cannot find prog.o: No such file or directory
stdout
Standard output is empty