Vanilla_Fury

fasm_3_2

Apr 25th, 2021
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. org 100h
  2.  
  3. NL_ equ $d, $a ; new line
  4.  
  5. mov ah, $9
  6. mov dx, helloStr_
  7. int 21h
  8.  
  9. mov dx, inputA_
  10. int 21h
  11.  
  12. mov ah, $a ; input to buff
  13. mov dx, buffNum
  14. int 21h
  15.  
  16. add dx, 2 ; dx = adress of first char in str
  17. call str_to_int_ ; input dx - str with '$' in the end
  18. ; output al - 8 bit number
  19. mov [A], al ; A = al
  20.  
  21. mov ah, $2 ; new line
  22. mov dx, $0d0a
  23. int 21h
  24.  
  25. mov ah, $9
  26. mov dx, inputB_
  27. int 21h
  28.  
  29. mov ah, $a ; input to buff
  30. mov dx, buffNum
  31. int 21h
  32.  
  33. add dx, 2 ; get B
  34. call str_to_int_
  35.  
  36. mov [B], al ; B = al
  37.  
  38. call task_ ; output ax - 16 bit number
  39.  
  40. mov dx, buffAns
  41. call int_to_str_ ; input ax - 16 bit number, dx - 6 byte buffer; output - string
  42. ; representation of number in buff that ends with $
  43.  
  44. mov ah, $9 ; output - resultStr
  45. mov dx, resultStr_
  46. int 21h
  47.  
  48. mov dx, buffAns ; output - buffAns
  49. int 21h
  50.  
  51. mov dx, byeStr_ ; output - byeStr
  52. int 21h
  53.  
  54. mov ah, $8 ; wait for input
  55. int 21h
  56. ret
  57.  
  58. str_to_int_: ; input dx - string that ends on 0dh; output al - 8bit number
  59. push cx
  60. push bp
  61. mov bp, sp
  62. sub sp, 1 ; one byte for const base
  63. mov byte [bp-1], 10 ; const 10 (base)
  64. push bx ; save
  65. mov bx, dx ; memory accessible only from bx
  66. xor ax, ax ; clear ax
  67. next_digit_:
  68. mul byte [bp-1] ; *10, result ax
  69. mov cl, [bx]
  70. sub cl, '0' ; char to digit
  71. add al, cl ; add new digit
  72. inc bx ; move to next symbol
  73. cmp byte [bx], $d ; char = 13 ?
  74. jne next_digit_ ; if no then there're still numbers to convert left
  75. pop bx ; retrieve
  76. mov sp, bp ; stack...
  77. pop bp
  78. pop cx
  79. ret ; go back
  80.  
  81. int_to_str_: ; input ax - 16 bit number, dx - buffer (string with length of 6); output - string representation of number in buff that ends with $
  82. push ax ; save ...
  83. push dx
  84. push bx
  85. push bp ; stack...
  86. mov bp, sp
  87. sub sp, 2 ; reserve 2 bytes
  88. mov word [bp-2], 10 ; const 10 2 byte
  89. mov bx, dx ; address of buff to bx
  90. xor dx, dx ; clear dx
  91. push '*' ; "stopper" for string conversation
  92. push '$' ; last symbol
  93.  
  94. next_letter_:
  95. div word [bp-2] ; divide dx:ax by 16bit number; result dx-remainder, ax-quotient
  96. add dx, '0' ; convert to ascii number
  97. push dx ; symbol to stack
  98. xor dx, dx ; clear dx
  99. cmp ax, 0 ; ax = 0 ?
  100. jne next_letter_ ; if no there're still number to convert
  101.  
  102. next_save_letter_:
  103. pop dx ; get symbol
  104. cmp dx, '*' ; is stopper?
  105. je exit_int_to_str_ ; if yes - exit
  106. mov [bx], dl ; if no move to buffer
  107. inc bx ; go to next symbol address
  108. jmp next_save_letter_ ; next letter
  109. exit_int_to_str_:
  110. mov sp, bp ; return stack pointer to its original state
  111. pop bp ; retrieve
  112. pop bx
  113. pop dx
  114. pop ax
  115. ret ; go back
  116.  
  117. task_:
  118. ; output ax
  119. xor ax, ax ; clear ax
  120. mov al, [A] ; A to al
  121. mul al ; *A
  122. xor bh, bh
  123. mov bl, [B] ; B to bl
  124. xor dx, dx
  125. div bx ; (dx:ax)/B, mod in dx
  126. add ax, bx ; +B
  127. ret ;go back
  128.  
  129. helloStr_ db "This program calculates the result of a*a/b+b", NL_, "Using global variables", NL_, "$"
  130. inputA_ db "Input number in range from 0 to 255:", NL_, "$"
  131. inputB_ db "Input number in range from 1 to 255:", NL_, "$"
  132. resultStr_ db NL_, "Result:", NL_, "$"
  133. byeStr_ db NL_, "Press anything to terminate a program...$"
  134. buffNum db 6, 0, 6 dup(?)
  135. buffAns db 6 dup (?)
  136. A db ?
  137. B db ?
Advertisement
Add Comment
Please, Sign In to add comment