Advertisement
madopew

laba3_3

Mar 15th, 2020
444
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.     xor ah, ah ;clear ah
  21.     push ax ;save first number to stack
  22.  
  23.     mov ah, 02h ;output - new line
  24.     mov dx, 0d0ah
  25.     int 21h
  26.  
  27.     mov ah, 09h ;output - inputStr
  28.     mov dx, inputStr_
  29.     int 21h
  30.  
  31.     mov ah, 0ah ;input to buff
  32.     mov dx, buffNum
  33.     int 21h
  34.  
  35.     add dx, 2 ;get second number
  36.     call str_to_int_
  37.     xor ah, ah ;save second number to stack
  38.     push ax
  39.  
  40.     call task_ ;input stack - 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 dx
  85.     push bx
  86.     push si ;memory is also accessible at bx+si
  87.     push bp
  88.     mov bp, sp ;move sp to bp
  89.     sub sp, 5 ;reserve 5byte of stack
  90.     mov word [bp-2], dx ;save address of buff
  91.     mov word [bp-4], 10 ;in first two byte save constant 10
  92.     mov byte [bp-5], 1 ;in third byte save AMOUNT of letters in the number, as it at least has '$' counter starts from 1
  93.     xor dx, dx
  94.     mov bx, word [bp-2] ;memory accessible from bx only...
  95.     mov byte [bx], '$' ;this is the first letter
  96.     next_letter_:
  97.         inc bx ;move to next letter
  98.         add byte [bp-5], 1 ;increment AMOUNT of letters
  99.         div word [bp-4] ;divide dx:ax by 16bit number; result dx-remainder, ax-quotient
  100.         add dl, '0' ;convert to ascii number
  101.         mov [bx], dl ;save that letter
  102.         xor dx, dx ;clear dx
  103.         cmp ax, 0 ;have we converted whole number?
  104.         jne next_letter_ ;if no there're still number to convert
  105.     xor si, si ;clear si
  106.     mov si, [bp-5] ;move two bytes of reserved stack to si: lowest - [bp-3], highest - [bp-2] (little-endian)
  107.     and si, 00ffh ;clear highest byte, only AMOUNT left in si
  108.     dec si ;get offset to the LAST letter from FIRST
  109.     mov al, [bp-5] ;amount to al, ah is zero
  110.     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
  111.     mov [bp-5], al ;save that number to [bp-2]
  112.     mov bx, [bp-2] ;restore position of the FIRST letter
  113.     next_switch_:
  114.         mov ah, [bx] ;swap two letters using ax...
  115.         mov al, [bx + si]
  116.         mov [bx], al
  117.         mov [bx+si], ah
  118.         inc bx ;move to next letter
  119.         sub si, 2 ;as si is OFFSET to last letter, not index, subtract 2 to get OFFSET to (last-1) letter
  120.         sub byte [bp-5], 1 ;amount of swaps - 1
  121.         cmp byte [bp-5], 0 ;have we finished?
  122.         jne next_switch_ ;no? go back!
  123.     mov sp, bp ;return stack pointer to its original state
  124.     pop bp ;retrieve data...
  125.     pop si
  126.     pop bx
  127.     pop dx
  128.     pop ax
  129.     ret ;go back
  130.     endp
  131.  
  132. proc task_ ;input stack - two numbers, output ax - 16 bit number
  133.     push bp ;save...
  134.     push dx
  135.     mov bp, sp ;move bp to sp
  136.     mov ax, [bp+6] ;first number
  137.     mov dx, [bp+8] ;second number
  138.     mul dl ;first multiplied by second, 8bit*8bit=16bit => result in ax
  139.     shl ax, 1 ;multiply by 2
  140.     sub ax, 3 ;subtract 3
  141.     pop dx ;retrieve
  142.     pop bp
  143.     ret 4 ;go back clearing stack from 4byte of parameters
  144.     endp
  145.  
  146. helloStr_ db "This program calculates the result of 2*x*y-3", NL_, "Using stack type procedure", NL_, "$"
  147. inputStr_ db "Input number in range from 0 to 254:", NL_, "$"
  148. resultStr_ db NL_, "Result:", NL_, "$"
  149. byeStr_ db NL_, "The program has now terminated. Press anything to continue...$"
  150. buffNum db 4, 0, 4 dup(?)
  151. buffAns db 6 dup (?)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement