.model tiny .data sInpMsg db 0dh, 0ah, "Number: $" sResMsg db 0dh, 0ah, "Result: $" sErrMsg db 0dh, 0ah, "Error!$" .code org 100h start: lea dx, sInpMsg mov ah, 09h int 21h call Input10 jc errExit cmp ax, 10 jg xBigTen cmp ax, 1 jg xOneTen sub ax, 2 jmp short xEnd xBigTen: inc ax ; x+1 rcl ax, 1 ; 2*(x+1) jo errExit ; ---owerflow add ax, 5 ; 5+2*(x+1) jo errExit ; ---owerflow jmp short xEnd xOneTen: rcl ax, 1 ; x*2 inc ax ; x*2+1 rcr ax, 1 ; (x*2+1)/2 xEnd: push ax lea dx, sResMsg mov ah, 09h int 21h pop ax call print10 int 20h errExit: lea dx, sErrMsg mov ah, 09h int 21h int 20h input10 proc push bx push cx push dx push si xor si, si lea di, inp mov dx, di mov ah, 0ah int 21h add di, 2 xor cx, cx mov al, [di] cmp al, '+' je i3 cmp al, '-' jne i1 mov si, 1 jmp short i3 i1: cmp al, 0dh je i_exit cmp al, '0' jb err1 cmp al, '9' ja err1 sub al, '0' shl cx, 1 cmp cx, 32767 ja err2 mov bx, cx shl cx, 1 cmp cx, 32767 ja err2 shl cx, 1 cmp cx, 32767 ja err2 add cx, bx cbw add cx, ax cmp cx, 32767 ja err2 i3: inc di mov al, [di] jmp short i1 i_exit: cmp si, 1 jne i2 neg cx i2: mov ax, cx clc i4: pop si pop dx pop cx pop bx ret err1: stc mov ax, 1 jmp short i4 err2: stc mov ax, 2 jmp short i4 .data inp db 7, 0, 7 dup(?) input10 endp print10 proc push ax push bx push cx push dx xor cx, cx test ax, ax jge p1 mov dl, '-' neg ax jmp short p2 p1: mov dl, ' ' p2: push ax mov ah, 02h int 21h pop ax mov bx, 10 p3: cwd div bx add dl, '0' push dx inc cx test ax, ax jnz p3 mov ah, 02h p4: pop dx int 21h loop p4 pop dx pop cx pop bx pop ax ret print10 endp end start |