Advertisement
Guest User

Untitled

a guest
May 26th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .model small
  2. .stack 256
  3. .data
  4.     KeyBuf  db      7, 0, 7 dup(0)
  5.     mes db "Enter string: ", 0ah, 0dh, '$'
  6.     numb dw 8 dup(?)
  7.     err01 db 'Error! Try again!', '$'
  8.     CR_LF db 0Dh, 0Ah,'$'
  9.     m db "M$"
  10.     cm db "CM$"
  11.     d db "D$"
  12.     cd db "CD$"
  13.     c db "C$"
  14.     xc db "XC$"
  15.     l db "L$"
  16.     xl db "XL$"
  17.     x db "X$"
  18.     ix db "IX$"
  19.     v db "V$"
  20.     iv db "IV$"
  21.     i db "I$"
  22. .code
  23. ;--------------------------------------------------
  24. ; преобразования строки в число
  25. ; на входе:
  26. ; ds:[si] - строка с числом
  27. ; ds:[di] - адрес числа
  28. ; на выходе
  29. ; ds:[di] - число
  30. ; CY - флаг переноса (при ошибке - установлен, иначе - сброшен)
  31. str_to_udec_word proc
  32.   push cx                 ;Сохранение всех используемых регистров
  33.     push dx
  34.     push bx
  35.     push si
  36.     push di
  37.  
  38.     mov si,dx               ;SI = адрес строки
  39.     mov di,10               ;DI = множитель 10 (основание системы счисления)
  40.     movzx cx,al             ;CX = счётчик цикла = длина строки
  41.     jcxz studw_error        ;Если длина = 0, возвращаем ошибку
  42.     xor ax,ax               ;AX = 0
  43.     xor bx,bx               ;BX = 0
  44.  
  45. studw_lp:
  46.     mov bl,[si]             ;Загрузка в BL очередного символа строки
  47.     inc si                  ;Инкремент адреса
  48.     cmp bl,'0'              ;Если код символа меньше кода '0'
  49.     jl studw_error          ; возвращаем ошибку
  50.     cmp bl,'9'              ;Если код символа больше кода '9'
  51.     jg studw_error          ; возвращаем ошибку
  52.     sub bl,'0'              ;Преобразование символа-цифры в число
  53.     mul di                  ;AX = AX * 10
  54.     jc studw_error          ;Если результат больше 16 бит - ошибка
  55.     add ax,bx               ;Прибавляем цифру
  56.     jc studw_error          ;Если переполнение - ошибка
  57.     loop studw_lp           ;Команда цикла
  58.     jmp studw_exit          ;Успешное завершение (здесь всегда CF = 0)
  59.  
  60. studw_error:
  61.     xor ax,ax               ;AX = 0
  62.     stc                     ;CF = 1 (Возвращаем ошибку)
  63.  
  64. studw_exit:
  65.     pop di                  ;Восстановление регистров
  66.     pop si
  67.     pop bx
  68.     pop dx
  69.     pop cx
  70.     ret
  71.  str_to_udec_word ENDP
  72.  
  73. ;--------------------------------------------------------------------
  74.  
  75. ;------------------------------------------------------------------------
  76. ;Процедура ввода слова с консоли в десятичном виде
  77. ;Выход: ax - слово(в случае ошибки ax = 0)
  78. ;cf = 1 - ошибка
  79.  
  80. input_udec_word proc
  81.  
  82.         push    dx
  83.         push    ds
  84.         push    es
  85.         push    si
  86. Input:
  87.     push di
  88.     lea dx, [mes]
  89.     mov ah, 09h
  90.     int 21h
  91.  
  92.     mov ah,0Ah
  93.         lea dx,[KeyBuf]
  94.         int 21h
  95.  
  96.  
  97.         ; перевод строки (на новую строку)
  98.         lea     dx,     [CR_LF]
  99.         mov     ah,     09h
  100.         int     21h
  101.  
  102.         lea     si,     [KeyBuf+1]
  103.         pop     di
  104.         call    str_to_udec_word
  105.  
  106.     jnc     NoError
  107.  
  108.     lea     dx,     err01
  109.         mov     ah,     09h
  110.         int     21h
  111.         jmp     Input
  112. NoError:
  113.     mov ax, [di]
  114.     pop     si
  115.         pop     es
  116.         pop     ds
  117.         pop     dx
  118.  
  119.         ret
  120. input_udec_word ENDP
  121. ;-------------------------------------------------------------------------
  122.  
  123. start:
  124.     mov bx, @data
  125.     mov ds, bx
  126.     mov es, bx
  127. beg:
  128.    
  129.     call input_udec_word
  130.     cmp ax, 0
  131.     jl erro
  132.     cmp ax, 1999
  133.     jg erro
  134.     jmp step1
  135.  
  136. step1:
  137.     cmp ax, 1000
  138.     jl step2
  139.     sub ax, 1000
  140.     mov ah, 09h
  141.     lea dx, m
  142.     int 21h
  143.     loop step1
  144.  
  145. step2:
  146.     cmp ax, 900
  147.     jl step3
  148.     sub ax, 900
  149.     mov ah, 09h
  150.     lea dx, cm
  151.     int 21h
  152.     loop step2
  153. step3:
  154.     cmp ax, 500
  155.     jl step4
  156.     sub ax, 500
  157.     mov ah, 09h
  158.     lea dx, d
  159.     int 21h
  160.     loop step3
  161.  
  162. step4:
  163.     cmp ax, 400
  164.     jl step5
  165.     sub ax, 400
  166.     mov ah, 09h
  167.     lea dx, cd
  168.     int 21h
  169.     loop step4
  170.  
  171. step5:
  172.     cmp ax, 100
  173.     jl step6
  174.     sub ax, 100
  175.     mov ah, 09h
  176.     lea dx, c
  177.     int 21h
  178.     loop step5
  179.  
  180. step6:
  181.     cmp ax, 90
  182.     jl step7
  183.     sub ax, 90
  184.     mov ah, 09h
  185.     lea dx, xc
  186.     int 21h
  187.     loop step6
  188.  
  189. step7:
  190.     cmp ax, 50
  191.     jl step8
  192.     sub ax, 50
  193.     mov ah, 09h
  194.     lea dx, l
  195.     int 21h
  196.     loop step7
  197.  
  198. step8:
  199.     cmp ax, 40
  200.     jl step9
  201.     sub ax, 40
  202.     mov ah, 09h
  203.     lea dx, xl
  204.     int 21h
  205.     loop step8
  206.  
  207. step9:
  208.     cmp ax, 10
  209.     jl step10
  210.     sub ax, 10
  211.     mov ah, 09h
  212.     lea dx, x
  213.     int 21h
  214.     loop step9
  215.  
  216. step10:
  217.     cmp ax, 9
  218.     jl step11
  219.     sub ax, 9
  220.     mov ah, 09h
  221.     lea dx, ix
  222.     int 21h
  223.     loop step10
  224.  
  225. step11:
  226.     cmp ax, 5
  227.     jl step12
  228.     sub ax, 5
  229.     mov ah, 09h
  230.     lea dx, v
  231.     int 21h
  232.     loop step11
  233.  
  234. step12:
  235.     cmp ax, 4
  236.     jl step13
  237.     sub ax, 4
  238.     mov ah, 09h
  239.     lea dx, iv
  240.     int 21h
  241.     loop step12
  242.  
  243. step13:
  244.     cmp ax, 1
  245.     jl endFin
  246.     sub ax, 1
  247.     mov ah, 09h
  248.     lea dx, i
  249.     int 21h
  250.     loop step13
  251.  
  252. endFin:
  253.     mov ah, 4c00h
  254.     int 21h
  255. erro:
  256.     mov ah, 09h
  257.     lea dx, err01
  258.     int 21h
  259.     jmp beg
  260. END START
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement