Guest User

Untitled

a guest
May 21st, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. .8086
  2. .MODEL TINY
  3.  
  4. _CODE SEGMENT PARA PUBLIC 'CODE'
  5. ASSUME CS:_CODE, DS:_CODE
  6. ORG 100h
  7.  
  8. _start:
  9. ;Ask for the first number
  10. mov si, offset strPrompt1
  11. call readNumber
  12.  
  13. ;Keep it
  14. mov bx, ax
  15.  
  16. ;Ask for the second number
  17. mov si, offset strPrompt2
  18. call readNumber
  19.  
  20. ;Add the number (check for overflow)
  21. add bx, ax
  22. jnc _print
  23.  
  24. ;Overflow
  25. mov ah, 09h
  26. mov dx, OFFSET strResultTooBig
  27. int 21h
  28.  
  29. jmp _end
  30.  
  31. _print:
  32. ;Print the number
  33. mov ah, 09h
  34. mov dx, OFFSET strResult
  35. int 21h
  36.  
  37. mov si, bx
  38. call printNumber
  39.  
  40. mov ah, 09h
  41. mov dx, OFFSET strCRLF
  42. int 21h
  43.  
  44. _end:
  45. mov ax, 4c00h
  46. int 21h
  47.  
  48.  
  49. ;;
  50. ;; FUNCTIONS
  51. ;;
  52.  
  53.  
  54. ;si = number
  55. printNumber:
  56. push ax
  57. push bx
  58. push dx
  59.  
  60. ;Divisor
  61. mov bx, 10000
  62. mov ax, si
  63. ;Print zeros flag
  64. xor si, si
  65.  
  66. printNum_loop:
  67. ;Find the current digit
  68. xor dx, dx
  69. div bx
  70.  
  71. ;If non-zero or the flag is set, print the digit
  72. or si, ax
  73. jnz printNum_print
  74.  
  75. ;If it's the last digit print it anyway
  76. cmp bx, 1
  77. jnz printNum_next
  78.  
  79. printNum_print:
  80. push dx
  81. mov dl, al
  82. add dl, 30h
  83. mov ah, 02h
  84. int 21h
  85. pop dx
  86.  
  87. printNum_next:
  88. ;Divide the divisor by 10
  89. push dx
  90. mov ax, bx
  91. xor dx, dx
  92. mov bx, 10
  93. div bx
  94. mov bx, ax
  95. pop dx
  96.  
  97. ;The rest is the new number
  98. mov ax, dx
  99.  
  100. ;Printed all the digits?
  101. cmp bx, 0
  102. jne printNum_loop
  103.  
  104. pop dx
  105. pop bx
  106. pop ax
  107. ret
  108.  
  109. ;si = ptr number
  110. convertNumber:
  111. push bx
  112. push cx
  113.  
  114. ;Accumulator = 0
  115. xor bx, bx
  116.  
  117. cvtNum_loop:
  118. ;Read a char
  119. xor ax, ax
  120. lodsb
  121.  
  122. ;End of the string?
  123. cmp al, 0dh
  124. je cvtNum_end
  125.  
  126. ;Invalid digit?
  127. cmp al, '0'
  128. jb cvtNum_prologue
  129.  
  130. cmp al, '9'
  131. ja cvtNum_invalid
  132.  
  133. ;Digital to digit
  134. sub al, '0'
  135.  
  136. ;Multiply the accumulator by 10
  137. mov cx, bx
  138. shl cx, 1
  139. jc cvtNum_prologue
  140. shl bx, 1
  141. jc cvtNum_prologue
  142. shl bx, 1
  143. jc cvtNum_prologue
  144. shl bx, 1
  145. jc cvtNum_prologue
  146. add bx, cx
  147. jc cvtNum_prologue
  148. add bx, ax
  149.  
  150. jmp cvtNum_loop
  151.  
  152. cvtNum_invalid:
  153. ;Set the CF
  154. mov ax, 8000h
  155. shl ax, 1
  156. jmp cvtNum_prologue
  157.  
  158. cvtNum_end:
  159. mov ax, bx
  160. xor bx, bx
  161.  
  162. cvtNum_prologue:
  163. pop cx
  164. pop bx
  165. ret
  166.  
  167. ;si = prompt
  168. readNumber:
  169. push dx
  170.  
  171. mov ah, 09h
  172. mov dx, si
  173. int 21h
  174.  
  175. mov BYTE PTR [inputBuffer+1], 0
  176. mov ah, 0ah
  177. mov dx, OFFSET inputBuffer
  178. int 21h
  179.  
  180. push si
  181. mov si, OFFSET inputBuffer + 2
  182. call convertNumber
  183. pop si
  184.  
  185. jnc readNum_end
  186.  
  187. mov ah, 09h
  188. mov dx, OFFSET strOverflow
  189. int 21h
  190.  
  191. pop dx
  192. jmp readNumber
  193.  
  194. readNum_end:
  195. pop dx
  196. ret
  197.  
  198. ;;
  199. ;; DATA
  200. ;;
  201.  
  202. inputBuffer db 7, 0, 7 DUP(0)
  203.  
  204. strPrompt1 db "Insert the first number: ", 24h
  205. strPrompt2 db 0dh, 0ah, "Insert the second number: ", 24h
  206. strOverflow db 0d, 0ah, "The number is too big or invalid.", 0dh, 0ah, 24h
  207. strResultTooBig db 0dh, 0ah, "The result is too big.", 0dh, 0ah, 24h
  208. strResult db 0dh, 0ah, "The result is: ", 24h
  209. strCRLF db 0dh, 0ah, 24h
  210.  
  211. _CODE ENDS
  212.  
  213. END _start
Advertisement
Add Comment
Please, Sign In to add comment