Vanilla_Fury

fasm_3_1_v2

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