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) :$"
- pkey db "press any key...$"
- buffer db 200 dup('$')
- minlen dw 0C8h
- sh 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
- ; di - loop counter here :)
- 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 below_e
- mov minlen, di ; min len - drom di to minlen variable
- mov sh, cx ; index of min len ( shortest ) - to sh
- below_e: ; below 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], ' ' ; if (buf[i]!= ' ')
- je sp_equal
- cmp cx, 0 ; && a == 0
- je loop_beg_end ; return 0
- dec cx ;
- cmp [bx-1],' ' ; && (buf[i-1]== ' ']
- je loop_beg_end ; return i
- inc cx ; buf[i]
- jmp lbl1
- sp_equal:
- inc cx ; i+1
- cmp [bx+1], '$'
- je loop_beg
- cmp [bx+1], ' ' ; if (buf[i+1] != ' ')
- jne loop_beg_end ; return i+1
- dec cx ; i
- lbl1:
- cmp [bx], '$' ; if (buf[i] == 0)
- jne loop_beg
- mov cx, 0ffffh
- loop_beg_end:
- ret
- find_beg endp
- ;===============================
- ; working ^)
- 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 dx, offset buffer ; read string
- mov ah, 0ah
- 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 ( working ) bug: single symbol without space after it on the end of str ^(
- call find_beg
- ; -----------
- ; 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
- ; --------
- ; debug output
- ;push ax ; save ax
- ;mov di, sh
- ;mov ah, 2
- ;mov dl, [bx+di]
- ;int 21h
- ;pop ax
- ;cmp cx, ax
- ;jbe bel_eq
- ;mov ax, cx
- cmp ax, sh
- je eq_
- cmp ax, 0ffffh
- je eq_
- push si
- mov bx, si
- mov si, ax
- mov di, sh
- call swap ; swap si and di
- mov ax, si
- pop si
- eq_:
- inc ax
- add bx, ax
- cmp [bx], '$'
- jne loop1
- ;=============================
- 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