Advertisement
madopew

Untitled

Mar 14th, 2020
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     include 'MACRO\PROC32.INC' ;for PROC-ENDP
  2.  
  3.     org 100h
  4.  
  5.     NL_ equ 0dh, 0ah ;const
  6.  
  7.     mov ah, 09h ;output - helloStr
  8.     mov dx, helloStr_
  9.     int 21h
  10.  
  11.     mov dx, inputStr_ ;output - inputStr
  12.     int 21h
  13.  
  14.     mov ah, 0ah ;input to buff
  15.     mov dx, buffNum
  16.     int 21h
  17.  
  18.     add dx, 2 ;dx to the first char of number
  19.     call str_to_int_ ;input dx - string that ends on 0dh, output al - 8 bit number
  20.     mov bh, al ;save first number to bh
  21.  
  22.     mov ah, 02h ;output - new line
  23.     mov dx, 0d0ah
  24.     int 21h
  25.  
  26.     mov ah, 09h ;output - inputStr
  27.     mov dx, inputStr_
  28.     int 21h
  29.  
  30.     mov ah, 0ah ;input to buff
  31.     mov dx, buffNum
  32.     int 21h
  33.  
  34.     add dx, 2 ;get second number
  35.     call str_to_int_
  36.  
  37.     mov dl, al ;save second to dl
  38.     mov dh, bh ;move first to dh
  39.  
  40.     call task_ ;input dh:dl - two numbers, 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 representation of number in buff that ends with $
  44.  
  45.     mov ah, 09h ;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, 08h ;wait for input
  56.     int 21h
  57.     ret
  58.  
  59. proc str_to_int_ ;input dx - string that ends on 0dh; output al - 8bit number
  60.     push dx ;save
  61.     push ax ;save
  62.     push bx ;save
  63.     mov bx, dx ;memory accessible only from bx
  64.     xor dx, dx ;clear dx
  65.     next_digit_:
  66.         mov ax, 10 ;10 to ax
  67.         mul dh ;multiplied by 1 byte; result ah:al
  68.         mov dh, al ;move result of multiolication to dh
  69.         mov dl, byte [bx] ;move one SYMBOL to dl
  70.         sub dl, '0' ;convert SYMBOL to NUMBER 0-9
  71.         add dh, dl ;add dl to dh
  72.         inc bx ;move to next symbol
  73.         cmp byte [bx], 0dh ;are we at the last symbol?
  74.         jne next_digit_ ;if no then there're still numbers to convert left
  75.     pop bx ;retrieve
  76.     pop ax ;retrieve
  77.     mov al, dh ;save 8bit answer to al
  78.     pop dx ;retrieve
  79.     ret ;go back
  80.     endp
  81.  
  82. proc int_to_str_ ;input ax - 16 bit number, dx - 6 byte buffer; output - string representation of number in buff that ends with $
  83.     push ax ;save ...
  84.     push bx
  85.     push si ;memory is also accessible at bx+si
  86.     push bp
  87.     mov bp, sp ;move sp to bp
  88.     sub sp, 2 ;reserve 2byte of stack
  89.     mov byte [bp-1], 10 ;in first byte save constant 10
  90.     mov byte [bp-2], 1 ;in second byte save AMOUNT of letters in the number, as it at least has '$' counter starts from 1
  91.     mov bx, dx ;memory accessible from bx only...
  92.     mov byte [bx], '$' ;this is the first letter
  93.     next_letter_:
  94.         inc bx ;move to next letter
  95.         add byte [bp-2], 1 ;increment AMOUNT of letters
  96.         div byte [bp-1] ;divide ax by 8bit number; result ah-remainder, al-quotient
  97.         add ah, '0' ;convert to ascii number
  98.         mov [bx], ah ;save that letter
  99.         xor ah, ah ;clear ah
  100.         cmp al, 0 ;are we converted whole number?
  101.         jne next_letter_ ;if no there're still number to convert
  102.     xor si, si ;clear si
  103.     mov si, [bp-2] ;move two bytes of reserved stack to si: lowest - [bp-1], highest - [bp-2] (little-endian)
  104.     and si, 00ffh ;clear highest byte, only AMOUNT left in si
  105.     dec si ;get offset to the LAST letter from FIRST
  106.     mov al, [bp-2] ;amount to al, ah is zero
  107.     shr ax, 1 ;divide by 2 as integer (for i = 0 to i / 2), "i/2" - integer division, so we get amount of swaps needed to reverse a string
  108.     mov [bp-2], al ;save that number to [bp-2]
  109.     mov bx, dx ;restore position of the FIRST letter
  110.     next_switch_:
  111.         mov ah, [bx] ;swap two letters using ax...
  112.         mov al, [bx + si]
  113.         mov [bx], al
  114.         mov [bx+si], ah
  115.         inc bx ;move to next letter
  116.         sub si, 2 ;as si is OFFSET to last letter, not index, subtract 2 to get OFFSET to (last-1) letter
  117.         sub byte [bp-2], 1 ;amount of swaps - 1
  118.         cmp byte [bp-2], 0 ;have we finished?
  119.         jne next_switch_ ;no? go back!
  120.     mov sp, bp ;return stack pointer to its original state
  121.     pop bp ;retrieve data...
  122.     pop si
  123.     pop bx
  124.     pop ax
  125.     ret ;go back
  126.     endp
  127.  
  128. proc task_ ;input dh:dl - two numbers, output ax - 16 bit number
  129.     xor ax, ax ;clear ax
  130.     mov al, dl ;first number...
  131.     mul dh ;...multiplied by second, 8bit*8bit=16bit => result in ax
  132.     shl ax, 1 ;multiply by 2
  133.     sub ax, 3 ;subtract 3
  134.     ret ;go back
  135.     endp
  136.  
  137. helloStr_ db "This program calculates the result of 2*x*y-3", NL_, "Using register type procedure", NL_, "$"
  138. inputStr_ db "Input number in range from 0 to 254:", NL_, "$"
  139. resultStr_ db NL_, "Result:", NL_, "$"
  140. byeStr_ db NL_, "The program has now terminated. Press anything to continue...$"
  141. buffNum db 4, 0, 4 dup(?)
  142. buffAns db 6 dup (?)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement