name "calcu

"

PUTC MACRO char push ax, mov al, char mov ah, 0EH int 10h pop ax

endm

org 100h jmp start

msg0 db "note: calculator works with integer values only.",0Dh,0Ah db "",0Dh,0Ah,'$' msg1 db 0Dh,0Ah, 0Dh,0Ah, 'enter first number: $' msg2 db "enter the operator: + - * / msg3 db "enter second number: $" : $"

msg4 db 0dh,0ah , 'the approximate result of my calculations is : $' err1 db "wrong operator!", 0Dh,0Ah , '$' smth db " and something.... $"

opr db '?'

num1 dw ? num2 dw ?

start: mov dx, offset msg0 mov ah, 9 int 21h

lea dx, msg1 mov ah, 09h int 21h

call scan_num

1 int 21h mov opr.mov num1. msg2 mov ah. al putc 0Dh putc 0Ah . cx putc 0Dh putc 0Ah lea dx. 09h int 21h mov ah.

'/' ja wrong_opr lea dx. '*' jb wrong_opr cmp opr. msg4 mov ah. cx lea dx. 'q' je exit cmp opr. msg3 mov ah.cmp opr. 09h int 21h . 09h int 21h call scan_num mov num2.

09h . '+' je do_plus cmp opr. '*' je do_mult cmp opr. '-' je do_minus cmp opr. err1 mov ah.cmp opr. '/' je do_div wrong_opr: lea dx.

09h int 21h mov ah. 0 int 16h ret .int 21h exit: lea dx. msg5 mov ah.

num1 add ax. num2 call print_num jmp exit do_minus: mov ax.do_plus: mov ax. num2 call print_num jmp exit do_mult: . num1 sub ax.

num1 imul num2 call print_num jmp exit do_div: mov dx. 09h int 21h jmp exit . num1 idiv num2 cmp dx. 0 jnz approx call print_num jmp exit approx: call print_num lea dx. smth mov ah. 0 mov ax.mov ax.

0 next_digit: MOV INT AH.scan_num PROC NEAR PUSH DX PUSH AX PUSH SI MOV CX. 00h 16h . 0 MOV CS:make_minus.

0 AX. '-' set_minus CMP JNE JMP not_cr: AL.MOV INT AH. 8 backspace_checked DX. 0Eh 10h CMP JE AL. CX CS:ten CX. 0Dh not_cr stop_input CMP JNE MOV MOV DIV MOV AL. AX PUTC ' ' PUTC 8 JMP next_digit backspace_checked: .

ax ax . '9' ok_digit remove_not_digit: putc 8 putc ' ' putc 8 JMP ok_digit: next_digit push ax mov mul mov pop ax.CMP JAE JMP ok_AE_0: CMP JBE AL. '0' ok_AE_0 remove_not_digit AL. cx cs:ten cx.

AX too_big2 jmp next_digit set_minus: mov jmp CS:make_minus. 0 dx. 0 . 30h mov mov add JC ah. 0 too_big sub al. AX CX. cx CX. DX DX.cmp jne dx. 1 next_digit too_big2: mov mov too_big: mov div mov AX. CX CS:ten CX.

0 not_minus CX POP POP POP RET SI AX DX make_minus SCAN_NUM DB ENDP ? .putc 8 putc ' ' putc 8 jmp next_digit stop_input: cmp JE NEG not_minus: CS:make_minus.

0 not_zero PUTC '0' JMP printed not_zero: CMP JNS NEG AX.PRINT_NUM PUSH DX PUSH AX PROC NEAR CMP JNZ AX. 0 positive AX PUTC '-' positive: CALL PRINT_NUM_UNS printed: POP POP RET PRINT_NUM ENDP AX DX .

0 end_print .PRINT_NUM_UNS PROC NEAR PUSH AX PUSH BX PUSH CX PUSH DX MOV CX. 10000 CMP JZ AX. 0 print_zero begin_print: CMP JZ BX. 1 MOV BX.

0 BX ADD AL. 0 AX. 0 calc CMP JB calc: MOV AX. 30h PUTC AL MOV AX. DX skip: PUSH AX MOV MOV DX. BX . BX skip CX. 0 MOV DIV DX.CMP JE CX.

DIV MOV POP CS:ten BX. AX AX JMP begin_print print_zero: PUTC '0' end_print: POP POP POP POP RET DX CX BX AX PRINT_NUM_UNS ENDP ten DW 10 .

0Dh . 1 empty_buffer DEC DX wait_for_key: MOV INT AH. 0 CMP JBE DX.GET_STRING PUSH AX PUSH CX PUSH DI PUSH DX PROC NEAR MOV CX. 0 16h CMP AL.

DX wait_for_key MOV INC INC [DI]. 8 add_to_buffer JCXZ wait_for_key DEC DEC CX DI PUTC 8 PUTC ' ' PUTC 8 JMP wait_for_key add_to_buffer: CMP JAE CX.JZ exit_GET_STRING CMP JNE AL. AL DI CX MOV INT AH. 0Eh 10h .

============================ exit_GET_STRING: MOV [DI].JMP wait_for_key . 0 empty_buffer: POP POP POP POP RET DX DI CX AX GET_STRING ENDP .

Sign up to vote on this title
UsefulNot useful