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 bh, al ;save first number to bh
- 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 dl, al ;save second to dl
- mov dh, bh ;move first to dh
- call task_ ;input dh:dl - two numbers, output ax - 16 bit number
- 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 bx
- push si ;memory is also accessible at bx+si
- push bp
- mov bp, sp ;move sp to bp
- sub sp, 2 ;reserve 2byte of stack
- mov byte [bp-1], 10 ;in first byte save constant 10
- mov byte [bp-2], 1 ;in second byte save AMOUNT of letters in the number, as it at least has '$' counter starts from 1
- mov bx, dx ;memory accessible from bx only...
- mov byte [bx], '$' ;this is the first letter
- next_letter_:
- inc bx ;move to next letter
- add byte [bp-2], 1 ;increment AMOUNT of letters
- div byte [bp-1] ;divide ax by 8bit number; result ah-remainder, al-quotient
- add ah, '0' ;convert to ascii number
- mov [bx], ah ;save that letter
- xor ah, ah ;clear ah
- cmp al, 0 ;are we converted whole number?
- jne next_letter_ ;if no there're still number to convert
- xor si, si ;clear si
- mov si, [bp-2] ;move two bytes of reserved stack to si: lowest - [bp-1], 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-2] ;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-2], al ;save that number to [bp-2]
- mov bx, dx ;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-2], 1 ;amount of swaps - 1
- cmp byte [bp-2], 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 ax
- ret ;go back
- endp
- proc task_ ;input dh:dl - two numbers, output ax - 16 bit number
- xor ax, ax ;clear ax
- mov al, dl ;first number...
- mul dh ;...multiplied by second, 8bit*8bit=16bit => result in ax
- shl ax, 1 ;multiply by 2
- sub ax, 3 ;subtract 3
- ret ;go back
- endp
- helloStr_ db "This program calculates the result of 2*x*y-3", NL_, "Using register 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(?)
- buffAns db 6 dup (?)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement