Advertisement
madopew

Untitled

Mar 21st, 2020
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 3_1:
  2.     org 100h
  3.  
  4.     NL_ equ 0dh, 0ah ;const
  5.  
  6.     mov ah, 09h ;output - helloStr
  7.     mov dx, helloStr_
  8.     int 21h
  9.  
  10.     mov dx, inputStr_ ;output - inputStr
  11.     int 21h
  12.  
  13.     mov ah, 0ah ;input to buff
  14.     mov dx, buffNum
  15.     int 21h
  16.  
  17.     add dx, 2 ;dx to the first char of number
  18.     call str_to_int_ ;input dx - string that ends on 0dh, output al - 8 bit number
  19.     mov bh, al ;save first number to bh
  20.  
  21.     mov ah, 02h ;output - new line
  22.     mov dx, 0d0ah
  23.     int 21h
  24.  
  25.     mov ah, 09h ;output - inputStr
  26.     mov dx, inputStr_
  27.     int 21h
  28.  
  29.     mov ah, 0ah ;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:dl - two numbers, 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 representation of number in buff that ends with $
  43.  
  44.     mov ah, 09h ;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, 08h ;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], 0dh ;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 - 6 byte buffer; 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.     next_letter_:
  91.         div word [bp-2] ;divide dx:ax by 16bit number; result dx-remainder, ax-quotient
  92.         add dx, '0' ;convert to ascii number
  93.         push dx ;save symbol to stack
  94.         xor dx, dx ;clear dx
  95.         cmp ax, 0 ;have we converted whole number?
  96.         jne next_letter_ ;if no there're still number to convert
  97.     next_save_letter_:
  98.         pop dx ;get symbol
  99.         cmp dx, '*' ;is stopper?
  100.         je exit_int_to_str_ ;if yes - exit
  101.         mov [bx], dl ;if no move to buffer
  102.         inc bx ;go to next symbol address
  103.         jmp next_save_letter_ ;next letter
  104.     exit_int_to_str_:
  105.         mov sp, bp ;return stack pointer to its original state
  106.         pop bp ;retrieve
  107.         pop bx
  108.         pop dx
  109.         pop ax
  110.         ret ;go back
  111.  
  112. task_: ;input dh:dl - two numbers, output ax - 16 bit number
  113.     xor ax, ax ;clear ax
  114.     mov al, dl ;first number...
  115.     mul dh ;...multiplied by second, 8bit*8bit=16bit => result in ax
  116.     shl ax, 1 ;multiply by 2
  117.     sub ax, 3 ;subtract 3
  118.     ret ;go back
  119.  
  120. helloStr_ db "This program calculates the result of 2*x*y-3", NL_, "Using register type procedure", NL_, "$"
  121. inputStr_ db "Input number in range from 0 to 254:", NL_, "$"
  122. resultStr_ db NL_, "Result:", NL_, "$"
  123. byeStr_ db NL_, "The program has now terminated. Press anything to continue...$"
  124. buffNum db 4, 0, 4 dup(?)
  125. buffAns db 6 dup (?)
  126.  
  127. 3_2:
  128.     org 100h
  129.  
  130.     NL_ equ 0dh, 0ah ;const
  131.  
  132.     mov ah, 09h ;output - helloStr
  133.     mov dx, helloStr_
  134.     int 21h
  135.  
  136.     mov dx, inputStr_ ;output - inputStr
  137.     int 21h
  138.  
  139.     mov ah, 0ah ;input to buff
  140.     mov dx, buffNum
  141.     int 21h
  142.  
  143.     add dx, 2 ;dx to the first char of number
  144.     call str_to_int_ ;input dx - string that ends on 0dh, output al - 8 bit number
  145.     mov [num1_], al ;mov first number to num1_
  146.  
  147.     mov ah, 02h ;output - new line
  148.     mov dx, 0d0ah
  149.     int 21h
  150.  
  151.     mov ah, 09h ;output - inputStr
  152.     mov dx, inputStr_
  153.     int 21h
  154.  
  155.     mov ah, 0ah ;input to buff
  156.     mov dx, buffNum
  157.     int 21h
  158.  
  159.     add dx, 2 ;get second number
  160.     call str_to_int_
  161.  
  162.     mov [num2_], al ;move second number to num2_
  163.  
  164.     call task_ ;input num1_ & num2_ - two numbers, output numAns_ - 16 bit number
  165.    
  166.     mov ax, [numAns_]
  167.     mov dx, buffAns
  168.     call int_to_str_ ;input ax - 16 bit number, dx - 6 byte buffer; output - string representation of number in buff that ends with $
  169.  
  170.     mov ah, 09h ;output - resultStr
  171.     mov dx, resultStr_
  172.     int 21h
  173.  
  174.     mov dx, buffAns ;output - buffAns
  175.     int 21h
  176.  
  177.     mov dx, byeStr_ ;output - byeStr
  178.     int 21h
  179.  
  180.     mov ah, 08h ;wait for input
  181.     int 21h
  182.     ret
  183.  
  184. str_to_int_: ;input dx - string that ends on 0dh; output al - 8bit number
  185.     push bp ;stack...
  186.     mov bp, sp
  187.     sub sp, 1 ;one byte for const
  188.     mov byte [bp-1], 10 ;const 10
  189.     push bx ;save
  190.     mov bx, dx ;memory accessible only from bx
  191.     xor ax, ax ;clear ax
  192.     next_digit_:
  193.         mul byte [bp-1] ;multiplied by 1 byte; result ah:al
  194.         add al, [bx] ;add symbol to al
  195.         sub al, '0' ;convert SYMBOL to NUMBER 0-9
  196.         inc bx ;move to next symbol
  197.         cmp byte [bx], 0dh ;are we at the last symbol?
  198.         jne next_digit_ ;if no then there're still numbers to convert left
  199.     pop bx ;retrieve
  200.     mov sp, bp ;stack...
  201.     pop bp
  202.     ret ;go back
  203.  
  204. int_to_str_: ;input ax - 16 bit number, dx - 6 byte buffer; output - string representation of number in buff that ends with $
  205.     push ax ;save ...
  206.     push dx
  207.     push bx
  208.     push bp ;stack...
  209.     mov bp, sp
  210.     sub sp, 2 ;reserve 2 bytes
  211.     mov word [bp-2], 10 ;const 10 2 byte
  212.     mov bx, dx ;address of buff to bx
  213.     xor dx, dx ;clear dx
  214.     push '*' ;"stopper" for string conversation
  215.     push '$' ;last symbol
  216.     next_letter_:
  217.         div word [bp-2] ;divide dx:ax by 16bit number; result dx-remainder, ax-quotient
  218.         add dx, '0' ;convert to ascii number
  219.         push dx ;save symbol to stack
  220.         xor dx, dx ;clear dx
  221.         cmp ax, 0 ;have we converted whole number?
  222.         jne next_letter_ ;if no there're still number to convert
  223.     next_save_letter_:
  224.         pop dx ;get symbol
  225.         cmp dx, '*' ;is stopper?
  226.         je exit_int_to_str_ ;if yes - exit
  227.         mov [bx], dl ;if no move to buffer
  228.         inc bx ;go to next symbol address
  229.         jmp next_save_letter_ ;next letter
  230.     exit_int_to_str_:
  231.         mov sp, bp ;return stack pointer to its original state
  232.         pop bp ;retrieve
  233.         pop bx
  234.         pop dx
  235.         pop ax
  236.         ret ;go back
  237.  
  238. task_: ;input dh:dl - two numbers, output ax - 16 bit number
  239.     push ax ;save
  240.     mov al, [num1_] ;num1_ to al...
  241.     mul [num2_] ;...multiplied by second, 8bit*8bit=16bit => result in ax
  242.     shl ax, 1 ;multiply by 2
  243.     sub ax, 3 ;subtract 3
  244.     mov [numAns_], ax ;move answer to numAns_
  245.     pop ax ;retrieve
  246.     ret ;go back
  247.  
  248. helloStr_ db "This program calculates the result of 2*x*y-3", NL_, "Using global var type procedure", NL_, "$"
  249. inputStr_ db "Input number in range from 0 to 254:", NL_, "$"
  250. resultStr_ db NL_, "Result:", NL_, "$"
  251. byeStr_ db NL_, "The program has now terminated. Press anything to continue...$"
  252. buffNum db 4, 0, 4 dup(?)
  253. num1_ db 0
  254. num2_ db 0
  255. numAns_ dw 0
  256. buffAns db 6 dup (?)
  257.  
  258. 3_3:
  259.     org 100h
  260.  
  261.     NL_ equ 0dh, 0ah ;const
  262.  
  263.     mov ah, 09h ;output - helloStr
  264.     mov dx, helloStr_
  265.     int 21h
  266.  
  267.     mov dx, inputStr_ ;output - inputStr
  268.     int 21h
  269.  
  270.     mov ah, 0ah ;input to buff
  271.     mov dx, buffNum
  272.     int 21h
  273.  
  274.     add dx, 2 ;dx to the first char of number
  275.     call str_to_int_ ;input dx - string that ends on 0dh, output al - 8 bit number
  276.     xor ah, ah ;clear ah
  277.     push ax ;save first number to stack
  278.  
  279.     mov ah, 02h ;output - new line
  280.     mov dx, 0d0ah
  281.     int 21h
  282.  
  283.     mov ah, 09h ;output - inputStr
  284.     mov dx, inputStr_
  285.     int 21h
  286.  
  287.     mov ah, 0ah ;input to buff
  288.     mov dx, buffNum
  289.     int 21h
  290.  
  291.     add dx, 2 ;get second number
  292.     call str_to_int_
  293.     xor ah, ah ;save second number to stack
  294.     push ax
  295.  
  296.     call task_ ;input stack - two numbers, output ax - 16 bit number
  297.    
  298.     mov dx, buffAns
  299.     call int_to_str_ ;input ax - 16 bit number, dx - 6 byte buffer; output - string representation of number in buff that ends with $
  300.  
  301.     mov ah, 09h ;output - resultStr
  302.     mov dx, resultStr_
  303.     int 21h
  304.  
  305.     mov dx, buffAns ;output - buffAns
  306.     int 21h
  307.  
  308.     mov dx, byeStr_ ;output - byeStr
  309.     int 21h
  310.  
  311.     mov ah, 08h ;wait for input
  312.     int 21h
  313.     ret
  314.  
  315. str_to_int_: ;input dx - string that ends on 0dh; output al - 8bit number
  316.     push bp ;stack...
  317.     mov bp, sp
  318.     sub sp, 1 ;one byte for const
  319.     mov byte [bp-1], 10 ;const 10
  320.     push bx ;save
  321.     mov bx, dx ;memory accessible only from bx
  322.     xor ax, ax ;clear ax
  323.     next_digit_:
  324.         mul byte [bp-1] ;multiplied by 1 byte; result ah:al
  325.         add al, [bx] ;add symbol to al
  326.         sub al, '0' ;convert SYMBOL to NUMBER 0-9
  327.         inc bx ;move to next symbol
  328.         cmp byte [bx], 0dh ;are we at the last symbol?
  329.         jne next_digit_ ;if no then there're still numbers to convert left
  330.     pop bx ;retrieve
  331.     mov sp, bp ;stack...
  332.     pop bp
  333.     ret ;go back
  334.  
  335. int_to_str_: ;input ax - 16 bit number, dx - 6 byte buffer; output - string representation of number in buff that ends with $
  336.     push ax ;save ...
  337.     push dx
  338.     push bx
  339.     push bp ;stack...
  340.     mov bp, sp
  341.     sub sp, 2 ;reserve 2 bytes
  342.     mov word [bp-2], 10 ;const 10 2 byte
  343.     mov bx, dx ;address of buff to bx
  344.     xor dx, dx ;clear dx
  345.     push '*' ;"stopper" for string conversation
  346.     push '$' ;last symbol
  347.     next_letter_:
  348.         div word [bp-2] ;divide dx:ax by 16bit number; result dx-remainder, ax-quotient
  349.         add dx, '0' ;convert to ascii number
  350.         push dx ;save symbol to stack
  351.         xor dx, dx ;clear dx
  352.         cmp ax, 0 ;have we converted whole number?
  353.         jne next_letter_ ;if no there're still number to convert
  354.     next_save_letter_:
  355.         pop dx ;get symbol
  356.         cmp dx, '*' ;is stopper?
  357.         je exit_int_to_str_ ;if yes - exit
  358.         mov [bx], dl ;if no move to buffer
  359.         inc bx ;go to next symbol address
  360.         jmp next_save_letter_ ;next letter
  361.     exit_int_to_str_:
  362.         mov sp, bp ;return stack pointer to its original state
  363.         pop bp ;retrieve
  364.         pop bx
  365.         pop dx
  366.         pop ax
  367.         ret ;go back
  368.  
  369. task_: ;input stack - two numbers, output ax - 16 bit number
  370.     push bp ;save...
  371.     mov bp, sp ;move bp to sp
  372.     push dx
  373.     mov ax, [bp+4] ;first number
  374.     mov dx, [bp+6] ;second number
  375.     mul dl ;first multiplied by second, 8bit*8bit=16bit => result in ax
  376.     shl ax, 1 ;multiply by 2
  377.     sub ax, 3 ;subtract 3
  378.     pop dx ;retrieve
  379.     pop bp
  380.     ret 4 ;go back clearing stack from 4byte of parameters
  381.  
  382. helloStr_ db "This program calculates the result of 2*x*y-3", NL_, "Using stack type procedure", NL_, "$"
  383. inputStr_ db "Input number in range from 0 to 254:", NL_, "$"
  384. resultStr_ db NL_, "Result:", NL_, "$"
  385. byeStr_ db NL_, "The program has now terminated. Press anything to continue...$"
  386. buffNum db 4, 0, 4 dup(?)
  387. buffAns db 6 dup (?)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement