Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include 'MACRO\PROC32.INC' ;for PROC-ENDP
- org 100h
- NL_ equ 0dh, 0ah ;const
- mov ah, 09h ;output - helloStr
- mov dx, helloStr_
- int 21h
- mov dx, inputStr_ ;output - inputStr
- int 21h
- mov ah, 0ah ;input to buff
- mov dx, buffNum
- int 21h
- add dx, 2 ;dx to the first char of number
- call str_to_int_ ;input dx - string that ends on 0dh, output al - 8 bit number
- mov [num1_], al ;mov first number to num1_
- mov ah, 02h ;output - new line
- mov dx, 0d0ah
- int 21h
- mov ah, 09h ;output - inputStr
- mov dx, inputStr_
- int 21h
- mov ah, 0ah ;input to buff
- mov dx, buffNum
- int 21h
- add dx, 2 ;get second number
- call str_to_int_
- mov [num2_], al ;move second number to num2_
- call task_ ;input num1_ & num2_ - two numbers, output numAns_ - 16 bit number
- mov ax, [numAns_]
- mov dx, buffAns
- call int_to_str_ ;input ax - 16 bit number, dx - 6 byte buffer; output - string representation of number in buff that ends with $
- mov ah, 09h ;output - resultStr
- mov dx, resultStr_
- int 21h
- mov dx, buffAns ;output - buffAns
- int 21h
- mov dx, byeStr_ ;output - byeStr
- int 21h
- mov ah, 08h ;wait for input
- int 21h
- ret
- proc str_to_int_ ;input dx - string that ends on 0dh; output al - 8bit number
- push dx ;save
- push ax ;save
- push bx ;save
- mov bx, dx ;memory accessible only from bx
- xor dx, dx ;clear dx
- next_digit_:
- mov ax, 10 ;10 to ax
- mul dh ;multiplied by 1 byte; result ah:al
- mov dh, al ;move result of multiolication to dh
- mov dl, byte [bx] ;move one SYMBOL to dl
- sub dl, '0' ;convert SYMBOL to NUMBER 0-9
- add dh, dl ;add dl to dh
- inc bx ;move to next symbol
- cmp byte [bx], 0dh ;are we at the last symbol?
- jne next_digit_ ;if no then there're still numbers to convert left
- pop bx ;retrieve
- pop ax ;retrieve
- mov al, dh ;save 8bit answer to al
- pop dx ;retrieve
- ret ;go back
- endp
- proc int_to_str_ ;input ax - 16 bit number, dx - 6 byte buffer; output - string representation of number in buff that ends with $
- push ax ;save ...
- push dx
- push bx
- push si ;memory is also accessible at bx+si
- push bp
- mov bp, sp ;move sp to bp
- sub sp, 5 ;reserve 5byte of stack
- mov word [bp-2], dx ;save address of buff
- mov word [bp-4], 10 ;in first two byte save constant 10
- mov byte [bp-5], 1 ;in third byte save AMOUNT of letters in the number, as it at least has '$' counter starts from 1
- xor dx, dx
- mov bx, word [bp-2] ;memory accessible from bx only...
- mov byte [bx], '$' ;this is the first letter
- next_letter_:
- inc bx ;move to next letter
- add byte [bp-5], 1 ;increment AMOUNT of letters
- div word [bp-4] ;divide dx:ax by 16bit number; result dx-remainder, ax-quotient
- add dl, '0' ;convert to ascii number
- mov [bx], dl ;save that letter
- xor dx, dx ;clear dx
- cmp ax, 0 ;have we converted whole number?
- jne next_letter_ ;if no there're still number to convert
- xor si, si ;clear si
- mov si, [bp-5] ;move two bytes of reserved stack to si: lowest - [bp-3], highest - [bp-2] (little-endian)
- and si, 00ffh ;clear highest byte, only AMOUNT left in si
- dec si ;get offset to the LAST letter from FIRST
- mov al, [bp-5] ;amount to al, ah is zero
- 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
- mov [bp-5], al ;save that number to [bp-2]
- mov bx, [bp-2] ;restore position of the FIRST letter
- next_switch_:
- mov ah, [bx] ;swap two letters using ax...
- mov al, [bx + si]
- mov [bx], al
- mov [bx+si], ah
- inc bx ;move to next letter
- sub si, 2 ;as si is OFFSET to last letter, not index, subtract 2 to get OFFSET to (last-1) letter
- sub byte [bp-5], 1 ;amount of swaps - 1
- cmp byte [bp-5], 0 ;have we finished?
- jne next_switch_ ;no? go back!
- mov sp, bp ;return stack pointer to its original state
- pop bp ;retrieve data...
- pop si
- pop bx
- pop dx
- pop ax
- ret ;go back
- endp
- proc task_ ;input dh:dl - two numbers, output ax - 16 bit number
- push ax ;save
- mov al, [num1_] ;num1_ to al...
- mul [num2_] ;...multiplied by second, 8bit*8bit=16bit => result in ax
- shl ax, 1 ;multiply by 2
- sub ax, 3 ;subtract 3
- mov [numAns_], ax ;move answer to numAns_
- pop ax ;retrieve
- ret ;go back
- endp
- helloStr_ db "This program calculates the result of 2*x*y-3", NL_, "Using global var type procedure", NL_, "$"
- inputStr_ db "Input number in range from 0 to 254:", NL_, "$"
- resultStr_ db NL_, "Result:", NL_, "$"
- byeStr_ db NL_, "The program has now terminated. Press anything to continue...$"
- buffNum db 4, 0, 4 dup(?)
- num1_ db 0
- num2_ db 0
- numAns_ dw 0
- buffAns db 6 dup (?)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement