Advertisement
Vanilla_Fury

fasm_3_1

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