Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- org 100h
- NL_ equ $d, $a ; new line
- mov ah, $9
- mov dx, helloStr_
- int 21h
- mov dx, inputA_
- int 21h
- mov ah, $a ; input to buff
- mov dx, buffNum
- int 21h
- add dx, 2 ; dx = adress of first char in str
- call str_to_int_ ; input dx - str with '$' in the end
- ; output al - 8 bit number
- mov [A], al ; A = al
- mov ah, $2 ; new line
- mov dx, $0d0a
- int 21h
- mov ah, $9
- mov dx, inputB_
- int 21h
- mov ah, $a ; input to buff
- mov dx, buffNum
- int 21h
- add dx, 2 ; get B
- call str_to_int_
- mov [B], al ; B = al
- call task_ ; 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, $9 ; output - resultStr
- mov dx, resultStr_
- int 21h
- mov dx, buffAns ; output - buffAns
- int 21h
- mov dx, byeStr_ ; output - byeStr
- int 21h
- mov ah, $8 ; wait for input
- int 21h
- ret
- str_to_int_: ; input dx - string that ends on 0dh; output al - 8bit number
- push cx
- push bp
- mov bp, sp
- sub sp, 1 ; one byte for const base
- mov byte [bp-1], 10 ; const 10 (base)
- push bx ; save
- mov bx, dx ; memory accessible only from bx
- xor ax, ax ; clear ax
- next_digit_:
- mul byte [bp-1] ; *10, result ax
- mov cl, [bx]
- sub cl, '0' ; char to digit
- add al, cl ; add new digit
- inc bx ; move to next symbol
- cmp byte [bx], $d ; char = 13 ?
- jne next_digit_ ; if no then there're still numbers to convert left
- pop bx ; retrieve
- mov sp, bp ; stack...
- pop bp
- pop cx
- ret ; go back
- int_to_str_: ; input ax - 16 bit number, dx - buffer (string with length of 6); output - string representation of number in buff that ends with $
- push ax ; save ...
- push dx
- push bx
- push bp ; stack...
- mov bp, sp
- sub sp, 2 ; reserve 2 bytes
- mov word [bp-2], 10 ; const 10 2 byte
- mov bx, dx ; address of buff to bx
- xor dx, dx ; clear dx
- push '*' ; "stopper" for string conversation
- push '$' ; last symbol
- next_letter_:
- div word [bp-2] ; divide dx:ax by 16bit number; result dx-remainder, ax-quotient
- add dx, '0' ; convert to ascii number
- push dx ; symbol to stack
- xor dx, dx ; clear dx
- cmp ax, 0 ; ax = 0 ?
- jne next_letter_ ; if no there're still number to convert
- next_save_letter_:
- pop dx ; get symbol
- cmp dx, '*' ; is stopper?
- je exit_int_to_str_ ; if yes - exit
- mov [bx], dl ; if no move to buffer
- inc bx ; go to next symbol address
- jmp next_save_letter_ ; next letter
- exit_int_to_str_:
- mov sp, bp ; return stack pointer to its original state
- pop bp ; retrieve
- pop bx
- pop dx
- pop ax
- ret ; go back
- task_:
- ; output ax
- xor ax, ax ; clear ax
- mov al, [A] ; A to al
- mul al ; *A
- xor bh, bh
- mov bl, [B] ; B to bl
- xor dx, dx
- div bx ; (dx:ax)/B, mod in dx
- add ax, bx ; +B
- ret ;go back
- helloStr_ db "This program calculates the result of a*a/b+b", NL_, "Using global variables", NL_, "$"
- inputA_ db "Input number in range from 0 to 255:", NL_, "$"
- inputB_ db "Input number in range from 1 to 255:", NL_, "$"
- resultStr_ db NL_, "Result:", NL_, "$"
- byeStr_ db NL_, "Press anything to terminate a program...$"
- buffNum db 6, 0, 6 dup(?)
- buffAns db 6 dup (?)
- A db ?
- B db ?
Advertisement
Add Comment
Please, Sign In to add comment