Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; multi-segment executable file template.
  2.  
  3. data segment
  4.     ; add your data here!
  5.     pkey    db  "press any key...$"
  6.     info1   db  "Enter the number",0Dh, 0Ah, '$'
  7.     info2   db  "The number with the lowest rank  assigned in 1";the number with the lowest rank  assigned in 1
  8.     CrLf    db  0Dh, 0Ah, '$'
  9.     buffer db 255,?,255 dup(?)
  10.     result   db 255
  11. ends
  12.  
  13. code segment
  14. assume cs:code,ds:data
  15. start:
  16. ; set segment registers:
  17.     mov ax, data
  18.     mov ds, ax
  19.    
  20.     mov ax, 0
  21.    
  22.     lea dx, info1
  23.     mov ah, 9
  24.     int 21h
  25.  
  26. input_str:
  27.    
  28.    lea dx,buffer
  29.    mov ah,0Ah
  30.    int 21h
  31.    mov bh,0
  32.    mov bl,buffer+1
  33.    mov buffer[bx+2],"$"
  34.    lea si, buffer+1
  35.    lea di , result
  36.    call Str2Num
  37.    
  38.    lea dx,CrLf
  39.    mov ah,9
  40.    int 21h
  41.    
  42.    lea dx,info2
  43.    mov ah,9
  44.    int 21h    
  45.    
  46.    mov dl , result
  47.    mov ax, 0  
  48. start_loop:
  49.    
  50.    shr dl,1
  51.    inc ax
  52.    jc end_loop
  53.    jmp start_loop
  54. end_loop:
  55.    call Show_AX
  56.    
  57.  
  58.    
  59.     mov bx,1
  60. start_2loop:
  61.     shr dl,1
  62.     jc if_1
  63.     jmp end2_loop
  64. if_1:
  65.     inc bx
  66.     jmp start_2loop
  67. end2_loop:
  68.     lea dx,CrLf
  69.     mov ah,9
  70.     int 21h
  71.     mov ax,bx
  72.     call Show_AX
  73.  
  74.     mov ah, 1
  75.     int 21h
  76.    
  77.     mov ax, 4c00h ; exit to operating system.
  78.     int 21h  
  79.  
  80. Show_AX proc
  81.         push    ax
  82.         push    bx
  83.         push    cx
  84.         push    dx
  85.         push    di
  86.  
  87.         mov     cx, 10          ; cx - основание системы счисления
  88.         xor     di, di          ; di - кол. цифр в числе
  89.  
  90. @@Conv:
  91.         xor     dx, dx
  92.         div     cx              ; dl = num mod 10
  93.         add     dl, '0'         ; перевод в символьный формат
  94.         inc     di
  95.         push    dx              ; складываем в стэк
  96.         or      ax, ax
  97.         jnz     @@Conv
  98.         ; выводим из стэка на экран
  99. @@Show:
  100.         pop     dx              ; dl = очередной символ
  101.         mov     ah, 2           ; ah - функция вывода символа на экран
  102.         int     21h
  103.         dec     di              ; повторяем пока di<>0
  104.         jnz     @@Show
  105.  
  106.         pop     di
  107.         pop     dx
  108.         pop     cx
  109.         pop     bx
  110.         pop     ax
  111.         ret
  112.  
  113. Show_AX endp
  114.  
  115. ;-----------------------------------------
  116.  
  117.  
  118. Str2Num PROC
  119.  
  120.    push ax
  121.    push bx
  122.    push cx
  123.    push dx
  124.    push ds
  125.    push es
  126.    push si
  127.    push ds
  128.    pop es
  129.  
  130.    mov cl, ds:[si]
  131.    xor ch,ch
  132.  
  133.    inc si
  134.  
  135.    mov bx,10
  136.    xor ax,ax
  137.  
  138.    cmp byte ptr [si], '-'
  139.    jne Start_Loop1
  140.    inc si
  141.    dec cx
  142. Start_Loop1:
  143.    mul bx
  144.    mov [di], ax
  145.    cmp dx, 0
  146.    jnz Error
  147.  
  148.    mov al,[si]
  149.    cmp al, '0'
  150.    jb Error
  151.    cmp al, '9'
  152.    ja Error
  153.    sub al, '0'
  154.    xor ah, ah
  155.    add ax, [di]
  156.    jc Error
  157.    cmp ax, 8000h
  158.    ja Error
  159.    inc si
  160.  
  161.    loop Start_Loop1
  162.  
  163.    pop si
  164.    push si
  165.    inc si
  166.    cmp byte ptr [si], '-'
  167.    jne Check
  168.    neg ax
  169.    jmp StoreRes
  170.  
  171. Check:
  172.    or ax,ax
  173.    js Error
  174.  
  175. StoreRes:
  176.    mov [di], ax
  177.    clc
  178.    pop si
  179.    pop es
  180.    pop ds
  181.    pop dx
  182.    pop cx
  183.    pop bx
  184.    pop ax
  185.    ret
  186.          
  187. Error:
  188.    xor ax, ax
  189.    mov [di], ax
  190.    stc
  191.    pop si
  192.    pop es
  193.    pop ds
  194.    pop dx
  195.    pop cx
  196.    pop bx
  197.    pop ax
  198.    ret
  199. Str2Num ENDP  
  200.    
  201. ends
  202.  
  203. end start ; set entry point and stop the assembler.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement