somewhere » 04 июн 2007, 14:26
Код: Выделить всё
.model small
.386
.stack
.code
jmp @start
buf db 255, 0, 255 dup (0)
s_inputstring db 'Input string : $'
s_keypress db 0Dh, 0Ah, 'Press any key to exit$'
log_position dw 0
max_position dw 0
out_position dw 12
; ===================== Waiting timer
WaitTimer proc near
push es
push ax
xor ax, ax
mov es, ax
mov ax, es:[46Ch]
@waiting:
cmp ax, es:[46Ch]
jz @waiting
pop ax
pop es
ret
WaitTimer endp
; ===================== Running string output
OutRunString proc near
pusha
push es
cld
mov ax, 0B800h
mov es, ax
mov ax, Out_Position
imul ax, ax, 160 ; Calculating offset
mov di, ax
mov si, ax
mov cx, 80
mov ax, 03420h
rep stosw ; Filling whole line by spaces
mov cx, 80 ; Characters to end of line
mov di, si
mov si, offset Buf + 2 ; First char of string
movzx bp, [si-1]
add bp, si ; Last char of string + 1
mov ax, Log_Position
cmp ax, cx
jnc @no_offset ; If position >= 80 then no need to offset string
sub cx, ax
add di, cx
add di, cx
mov cx, ax ; Calc offset and length
jmp short @outstr
@no_offset:
sub ax, cx
add si, ax ; Calc first output character
@outstr:
jcxz @outend
mov ah, 034h
mov al, [si]
stosw ; Out character
inc si
cmp si, bp ; If end of line or end of string then exit
jz @outend
dec cx
jmp short @outstr
@outend:
pop es
popa
retn
OutRunString endp
; ========================= MAIN CODE ================================
@start:
push cs
pop ds
mov ax, 3 ; Videomode 80x25
int 10h
mov dx, offset s_inputstring
mov ah, 9
int 21h
mov ah, 0Ah
mov dx, offset buf
int 21h ; Buffered input
mov dx, offset s_keypress
mov ah, 9
int 21h
movzx ax, byte ptr buf + 1
add ax, 80
mov Max_Position, ax
@work:
call WaitTimer ; Waiting 1/18 sec.
inc Log_Position
mov ax, Log_Position
cmp ax, Max_Position ; If LogPos > MaxPos then LogPos = 0
jnz @skip
mov Log_Position, 0
@skip:
call OutRunString ; Out running string
mov ah, 0Bh
int 21h
or al, al ; If not key pressed, then continue
jz @work
mov ah, 0Ch
int 21h ; Flush key buffer
mov ax, 4C00h
int 21h
end
Это один из способов. Алгоритм использует больше вычислительных операций, зато не использует дополнительной памяти.
[code]
.model small
.386
.stack
.code
jmp @start
buf db 255, 0, 255 dup (0)
s_inputstring db 'Input string : $'
s_keypress db 0Dh, 0Ah, 'Press any key to exit$'
log_position dw 0
max_position dw 0
out_position dw 12
; ===================== Waiting timer
WaitTimer proc near
push es
push ax
xor ax, ax
mov es, ax
mov ax, es:[46Ch]
@waiting:
cmp ax, es:[46Ch]
jz @waiting
pop ax
pop es
ret
WaitTimer endp
; ===================== Running string output
OutRunString proc near
pusha
push es
cld
mov ax, 0B800h
mov es, ax
mov ax, Out_Position
imul ax, ax, 160 ; Calculating offset
mov di, ax
mov si, ax
mov cx, 80
mov ax, 03420h
rep stosw ; Filling whole line by spaces
mov cx, 80 ; Characters to end of line
mov di, si
mov si, offset Buf + 2 ; First char of string
movzx bp, [si-1]
add bp, si ; Last char of string + 1
mov ax, Log_Position
cmp ax, cx
jnc @no_offset ; If position >= 80 then no need to offset string
sub cx, ax
add di, cx
add di, cx
mov cx, ax ; Calc offset and length
jmp short @outstr
@no_offset:
sub ax, cx
add si, ax ; Calc first output character
@outstr:
jcxz @outend
mov ah, 034h
mov al, [si]
stosw ; Out character
inc si
cmp si, bp ; If end of line or end of string then exit
jz @outend
dec cx
jmp short @outstr
@outend:
pop es
popa
retn
OutRunString endp
; ========================= MAIN CODE ================================
@start:
push cs
pop ds
mov ax, 3 ; Videomode 80x25
int 10h
mov dx, offset s_inputstring
mov ah, 9
int 21h
mov ah, 0Ah
mov dx, offset buf
int 21h ; Buffered input
mov dx, offset s_keypress
mov ah, 9
int 21h
movzx ax, byte ptr buf + 1
add ax, 80
mov Max_Position, ax
@work:
call WaitTimer ; Waiting 1/18 sec.
inc Log_Position
mov ax, Log_Position
cmp ax, Max_Position ; If LogPos > MaxPos then LogPos = 0
jnz @skip
mov Log_Position, 0
@skip:
call OutRunString ; Out running string
mov ah, 0Bh
int 21h
or al, al ; If not key pressed, then continue
jz @work
mov ah, 0Ch
int 21h ; Flush key buffer
mov ax, 4C00h
int 21h
end
[/code]
Это один из способов. Алгоритм использует больше вычислительных операций, зато не использует дополнительной памяти.