Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; multi-segment executable file template.
- data segment
- prompt db "Enter string (max 200 characters) :",0ah,'$'
- pkey db 0ah,"press any key...$"
- buffer db 200 dup('$')
- minlen dw 0C8h
- sh dw 0
- cur dw 0
- ends
- ;==================
- stack segment
- db 128 dup(0)
- ends
- ;===================
- code segment
- ;
- swap proc near
- push ax
- xor ax, ax
- loop_swap_1:
- xchg al, [bx+si]
- xchg al, [bx+di]
- mov [bx+si], al
- inc si
- inc di
- cmp [bx+di],' '
- je loop_swap_1_end
- mov al, [bx+di]
- cmp al, 024h
- jne loop_swap_1
- loop_swap_1_end:
- dec di
- loop_swap_2:
- mov ah, [bx+si]
- push si
- loop_swap_3:
- mov al, [bx+si+1]
- mov [bx+si], al
- inc si
- cmp si, di
- jne loop_swap_3
- pop si
- mov [bx+di], ah
- cmp [bx+si],' '
- je loop_swap_2_end
- cmp [bx+si],'$'
- jne loop_swap_2
- loop_swap_2_end:
- pop ax
- ret
- swap endp
- ;-----------------------
- find_sh proc near
- mov minlen, 0C8h
- dec di
- loop_sh:
- inc di
- ;xor cx, cx ; ?
- mov cx, di
- mov bx, si
- add bx, cx
- call find_beg ; after it in cx - index of beg , bx - point to biginning
- cmp cx, 0ffffh
- je loop_sh_end
- mov bx, si
- mov di, cx
- call find_end ; after in di - index of end, bx - point to 0 element
- cmp di, 0ffffh
- je loop_sh_end
- push di
- sub di, cx ; in di - tmplen
- cmp di, minlen
- jae above_e
- mov minlen, di ; min len - drom di to minlen variable
- mov sh, cx ; index of min len ( shortest ) - to sh
- above_e: ; above or equal - nothing to do
- pop di ; restore di as counter for loop_sh
- cmp [bx+di], '$'
- jne loop_sh
- loop_sh_end:
- ret
- find_sh endp
- ;----------------------
- find_beg proc near
- dec bx
- dec cx
- loop_beg:
- inc bx
- inc cx
- cmp [bx], ' '
- je sp_equal
- cmp cx, 0
- je loop_beg_end
- cmp [bx-1],' '
- je loop_beg_end
- jmp lbl1
- sp_equal:
- inc cx
- cmp [bx+1], '$'
- je lbl_d
- cmp [bx+1], ' '
- jne loop_beg_end
- dec cx
- lbl1:
- cmp [bx], '$'
- jne loop_beg
- lbl_d:
- mov cx, 0ffffh
- loop_beg_end:
- ret
- find_beg endp
- ;===============================
- find_end proc near
- loop_end:
- cmp [bx+di], ' '
- je lbl2
- cmp [bx+di+1], ' '
- je loop_end_end
- cmp [bx+di+1], '$'
- je loop_end_end
- cmp [bx+di], '$'
- lbl2:
- inc di
- jne loop_end
- mov di, 0ffffh
- loop_end_end:
- ret
- find_end endp
- ; =============================================
- ; ==============================================
- start:
- ; set segment registers:
- mov ax, data
- mov ds, ax
- mov es, ax
- ;==============================
- ; add your code here
- ; ----------------------------------------
- mov ah, 9
- mov dx, offset prompt
- int 21h
- mov ah, 0ah
- mov dx, offset buffer ; read string
- int 21h
- ; check length
- cmp buffer[1], 1 ; if 1 symbol - skip sorting
- jbe press_akey
- ;===============================
- ; remove $ signs inside inputted str buffer
- ;===============================
- ; mov bx,offset buffer+2
- ; xor cx, cx
- ; mov cl, buffer[1]
- ;loop_$:
- ; xor ax, ax
- ; mov al, [bx]
- ; cmp al, 24h
- ; jne not_$
- ; mov al, ' '
- ; mov [bx], al
- ;not_$:
- ; inc bx
- ; loop loop_$
- ;===============================
- xor bx, bx ; clear bx register
- mov bl, buffer[1] ; mov length of buffer to bl register
- mov buffer[bx+2], '$' ; add $ sign to the end of buffer string
- ;=============================
- mov si, offset buffer + 2
- ;xor cx,cx ; ?
- xor ax,ax ; counter for loop1
- loop1:
- ; find beginning of next word
- mov cx, ax ; cx - counter for loop_beg
- mov bx, si
- add bx, cx ; set bx pointer to start point before searching beginning of word
- ; find beginning of word here
- call find_beg
- cmp cx, 0ffffh
- je loop1_end
- mov cur, cx
- ; find end
- mov di, cx ; start searching end from the beginning of preivios found word
- call find_sh ; find shortest, in sh variable - index of shortest
- ; --------
- mov cx, cur
- cmp cx, 0ffffh
- je eq_
- cmp cx, sh
- je eq_
- push si
- mov bx, si ; o_O
- mov si, cur ; wrong !------------------
- mov di, sh
- call swap ; swap si and di
- mov ax, si
- pop si
- eq_:
- inc ax
- mov bx, si ; ?
- add bx, ax
- cmp [bx], '$'
- jne loop1
- loop1_end:
- ;===============================================
- ;===============================================
- mov dx, si ;
- mov ah, 9 ; show our string
- int 21h ;
- ;==============================
- ; show Press any key... message
- press_akey:
- lea dx, pkey
- mov ah, 9
- int 21h ; output string at ds:dx
- ;wait for any key....
- mov ah, 1
- int 21h
- mov ax, 4c00h ; exit to operating system.
- int 21h
- ends
- end start ; set entry point and stop the assembler.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement