Vanilla_Fury

fasm_3_3

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