Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; multi-segment executable file template.
- data segment
- ; add your data here!
- pkey db "press any key...$"
- info1 db "Enter the number",0Dh, 0Ah, '$'
- info2 db "The number with the lowest rank assigned in 1";the number with the lowest rank assigned in 1
- CrLf db 0Dh, 0Ah, '$'
- buffer db 255,?,255 dup(?)
- result db 255
- ends
- code segment
- assume cs:code,ds:data
- start:
- ; set segment registers:
- mov ax, data
- mov ds, ax
- mov ax, 0
- lea dx, info1
- mov ah, 9
- int 21h
- input_str:
- lea dx,buffer
- mov ah,0Ah
- int 21h
- mov bh,0
- mov bl,buffer+1
- mov buffer[bx+2],"$"
- lea si, buffer+1
- lea di , result
- call Str2Num
- lea dx,CrLf
- mov ah,9
- int 21h
- lea dx,info2
- mov ah,9
- int 21h
- mov dl , result
- mov ax, 0
- start_loop:
- shr dl,1
- inc ax
- jc end_loop
- jmp start_loop
- end_loop:
- call Show_AX
- mov bx,1
- start_2loop:
- shr dl,1
- jc if_1
- jmp end2_loop
- if_1:
- inc bx
- jmp start_2loop
- end2_loop:
- lea dx,CrLf
- mov ah,9
- int 21h
- mov ax,bx
- call Show_AX
- mov ah, 1
- int 21h
- mov ax, 4c00h ; exit to operating system.
- int 21h
- Show_AX proc
- push ax
- push bx
- push cx
- push dx
- push di
- mov cx, 10 ; cx - основание системы счисления
- xor di, di ; di - кол. цифр в числе
- @@Conv:
- xor dx, dx
- div cx ; dl = num mod 10
- add dl, '0' ; перевод в символьный формат
- inc di
- push dx ; складываем в стэк
- or ax, ax
- jnz @@Conv
- ; выводим из стэка на экран
- @@Show:
- pop dx ; dl = очередной символ
- mov ah, 2 ; ah - функция вывода символа на экран
- int 21h
- dec di ; повторяем пока di<>0
- jnz @@Show
- pop di
- pop dx
- pop cx
- pop bx
- pop ax
- ret
- Show_AX endp
- ;-----------------------------------------
- Str2Num PROC
- push ax
- push bx
- push cx
- push dx
- push ds
- push es
- push si
- push ds
- pop es
- mov cl, ds:[si]
- xor ch,ch
- inc si
- mov bx,10
- xor ax,ax
- cmp byte ptr [si], '-'
- jne Start_Loop1
- inc si
- dec cx
- Start_Loop1:
- mul bx
- mov [di], ax
- cmp dx, 0
- jnz Error
- mov al,[si]
- cmp al, '0'
- jb Error
- cmp al, '9'
- ja Error
- sub al, '0'
- xor ah, ah
- add ax, [di]
- jc Error
- cmp ax, 8000h
- ja Error
- inc si
- loop Start_Loop1
- pop si
- push si
- inc si
- cmp byte ptr [si], '-'
- jne Check
- neg ax
- jmp StoreRes
- Check:
- or ax,ax
- js Error
- StoreRes:
- mov [di], ax
- clc
- pop si
- pop es
- pop ds
- pop dx
- pop cx
- pop bx
- pop ax
- ret
- Error:
- xor ax, ax
- mov [di], ax
- stc
- pop si
- pop es
- pop ds
- pop dx
- pop cx
- pop bx
- pop ax
- ret
- Str2Num ENDP
- ends
- end start ; set entry point and stop the assembler.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement