Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .model small
- .stack 256
- .data
- KeyBuf db 7, 0, 7 dup(0)
- mes db "Enter string: ", 0ah, 0dh, '$'
- numb dw 8 dup(?)
- err01 db 'Error! Try again!', '$'
- CR_LF db 0Dh, 0Ah,'$'
- m db "M$"
- cm db "CM$"
- d db "D$"
- cd db "CD$"
- c db "C$"
- xc db "XC$"
- l db "L$"
- xl db "XL$"
- x db "X$"
- ix db "IX$"
- v db "V$"
- iv db "IV$"
- i db "I$"
- .code
- ;--------------------------------------------------
- ; преобразования строки в число
- ; на входе:
- ; ds:[si] - строка с числом
- ; ds:[di] - адрес числа
- ; на выходе
- ; ds:[di] - число
- ; CY - флаг переноса (при ошибке - установлен, иначе - сброшен)
- str_to_udec_word proc
- push cx ;Сохранение всех используемых регистров
- push dx
- push bx
- push si
- push di
- mov si,dx ;SI = адрес строки
- mov di,10 ;DI = множитель 10 (основание системы счисления)
- movzx cx,al ;CX = счётчик цикла = длина строки
- jcxz studw_error ;Если длина = 0, возвращаем ошибку
- xor ax,ax ;AX = 0
- xor bx,bx ;BX = 0
- studw_lp:
- mov bl,[si] ;Загрузка в BL очередного символа строки
- inc si ;Инкремент адреса
- cmp bl,'0' ;Если код символа меньше кода '0'
- jl studw_error ; возвращаем ошибку
- cmp bl,'9' ;Если код символа больше кода '9'
- jg studw_error ; возвращаем ошибку
- sub bl,'0' ;Преобразование символа-цифры в число
- mul di ;AX = AX * 10
- jc studw_error ;Если результат больше 16 бит - ошибка
- add ax,bx ;Прибавляем цифру
- jc studw_error ;Если переполнение - ошибка
- loop studw_lp ;Команда цикла
- jmp studw_exit ;Успешное завершение (здесь всегда CF = 0)
- studw_error:
- xor ax,ax ;AX = 0
- stc ;CF = 1 (Возвращаем ошибку)
- studw_exit:
- pop di ;Восстановление регистров
- pop si
- pop bx
- pop dx
- pop cx
- ret
- str_to_udec_word ENDP
- ;--------------------------------------------------------------------
- ;------------------------------------------------------------------------
- ;Процедура ввода слова с консоли в десятичном виде
- ;Выход: ax - слово(в случае ошибки ax = 0)
- ;cf = 1 - ошибка
- input_udec_word proc
- push dx
- push ds
- push es
- push si
- Input:
- push di
- lea dx, [mes]
- mov ah, 09h
- int 21h
- mov ah,0Ah
- lea dx,[KeyBuf]
- int 21h
- ; перевод строки (на новую строку)
- lea dx, [CR_LF]
- mov ah, 09h
- int 21h
- lea si, [KeyBuf+1]
- pop di
- call str_to_udec_word
- jnc NoError
- lea dx, err01
- mov ah, 09h
- int 21h
- jmp Input
- NoError:
- mov ax, [di]
- pop si
- pop es
- pop ds
- pop dx
- ret
- input_udec_word ENDP
- ;-------------------------------------------------------------------------
- start:
- mov bx, @data
- mov ds, bx
- mov es, bx
- beg:
- call input_udec_word
- cmp ax, 0
- jl erro
- cmp ax, 1999
- jg erro
- jmp step1
- step1:
- cmp ax, 1000
- jl step2
- sub ax, 1000
- mov ah, 09h
- lea dx, m
- int 21h
- loop step1
- step2:
- cmp ax, 900
- jl step3
- sub ax, 900
- mov ah, 09h
- lea dx, cm
- int 21h
- loop step2
- step3:
- cmp ax, 500
- jl step4
- sub ax, 500
- mov ah, 09h
- lea dx, d
- int 21h
- loop step3
- step4:
- cmp ax, 400
- jl step5
- sub ax, 400
- mov ah, 09h
- lea dx, cd
- int 21h
- loop step4
- step5:
- cmp ax, 100
- jl step6
- sub ax, 100
- mov ah, 09h
- lea dx, c
- int 21h
- loop step5
- step6:
- cmp ax, 90
- jl step7
- sub ax, 90
- mov ah, 09h
- lea dx, xc
- int 21h
- loop step6
- step7:
- cmp ax, 50
- jl step8
- sub ax, 50
- mov ah, 09h
- lea dx, l
- int 21h
- loop step7
- step8:
- cmp ax, 40
- jl step9
- sub ax, 40
- mov ah, 09h
- lea dx, xl
- int 21h
- loop step8
- step9:
- cmp ax, 10
- jl step10
- sub ax, 10
- mov ah, 09h
- lea dx, x
- int 21h
- loop step9
- step10:
- cmp ax, 9
- jl step11
- sub ax, 9
- mov ah, 09h
- lea dx, ix
- int 21h
- loop step10
- step11:
- cmp ax, 5
- jl step12
- sub ax, 5
- mov ah, 09h
- lea dx, v
- int 21h
- loop step11
- step12:
- cmp ax, 4
- jl step13
- sub ax, 4
- mov ah, 09h
- lea dx, iv
- int 21h
- loop step12
- step13:
- cmp ax, 1
- jl endFin
- sub ax, 1
- mov ah, 09h
- lea dx, i
- int 21h
- loop step13
- endFin:
- mov ah, 4c00h
- int 21h
- erro:
- mov ah, 09h
- lea dx, err01
- int 21h
- jmp beg
- END START
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement