我目前正在编写一个用于任务管理的Assemblyx 86 tasm语言程序,它允许添加和查看任务。然而,我一直在努力实现一个删除特定任务的功能。
.model small
.stack 100h
.data
sir db 80 dup('$')
m2 db 'Introduceti sirul:$'
m4 db '1. Add task', 13, 10, '$'
m5 db '2. View tasks', 13, 10, '$'
filename db 'tasks.txt',0
handle dw ?
strLength dw ?
t_handle dw ?
newline db 13, 10, '$' ; New line characters
menuChoice db ?
buffer db 2000 dup(0)
.code
mov ax,@data
mov ds,ax
mov ah, 9h
mov dx, offset m4
int 21h
mov ah, 9h
mov dx, offset m5
int 21h
mov ah, 1h
int 21h
mov menuChoice, al
cmp menuChoice, '1'
je add_task
cmp menuChoice, '2'
je view_tasks
cmp menuChoice, '3'
je delete_aux
jmp end_program
view_tasks:
; Opening file for reading
mov ah, 3Dh
mov al, 0
mov dx, offset filename
int 21h
mov handle, ax
; Reading file contents
mov ah, 3Fh
mov bx, handle
mov dx, offset sir
mov cx, 80
int 21h
; Displaying tasks line by line with incrementing numbers
mov si, offset sir
display_loop:
; Check if end of file or end of buffer
cmp byte ptr [si], '$'
je end_display
; Print character to screen
mov ah, 02h
mov dl, [si]
int 21h
; Move to the next character
inc si
jmp display_loop
end_display:
; Close the file
mov ah, 3Eh ; File close service
mov bx, handle ; File handle
int 21h ; File close interrupt
jmp end_program
delete_aux:
cmp menuChoice, '3' ; Check if the choice is for delete
jne view_tasks ; If not, jump to view_tasks
jmp delete_task ; If yes, jump to delete_task
add_task:
; Clearing the buffer
mov ah, 0Ah
mov dx, offset sir
int 21h
; Adding a task to the file
; Prompting for task input
mov ah, 9h
mov dx, offset m2
int 21h
mov bx,0
mov cx,80
mov ah,3fh
mov dx,offset sir
int 21h
mov ah, 9h
mov dx, offset sir
int 21h
; Calculating string length
mov si, offset sir
add si, 2
mov cx, 0 ; Initialize count
count_length:
cmp byte ptr [si], '$'
je end_count
inc cx
inc si
jmp count_length
end_count:
mov strLength, cx
; Opening file for appending at the end
mov ah, 3Dh
mov al, 2 ;
mov dx, offset filename
int 21h
mov handle, ax
; Positioning at the end of the file
mov ah, 42h
mov al, 2
mov bx, handle
mov cx, 0
mov dx, 0
int 21h
; Writing string to file
mov ah, 40h
mov bx, handle
mov dx, offset sir
mov cx, strLength
int 21h
; Writing newline to file
mov ah, 40h
mov bx, handle
mov dx, offset newline
mov cx, 2
int 21h
; Closing the file
mov ah, 3Eh
mov bx, handle
int 21h
jmp view_tasks
delete_task:
jmp view_tasks ; Redirect to view tasks after deletion
end_program:
mov ah,4Ch
int 21h
end
字符串
我试过实现一个“删除任务”选项,并使用终止字符13,10,“$”管理文件操作,但我不能得到它的窍门。
delete_task:
; Prompt for task number to delete
mov ah, 9h
mov dx, offset m_delete_prompt
int 21h
; Read user input for the task number to delete
mov ah, 1h
int 21h
sub al, '0'
mov bl, al
; Opening the file for reading and writing
mov ah, 3Dh
mov al, 2
mov dx, offset filename
int 21h
mov handle, ax
; Reading and deleting the desired task
mov ah, 3Fh
mov bx, handle
mov dx, offset buffer
mov cx, 2000
int 21h
mov si, offset buffer
mov di, si
delete_loop:
cmp byte ptr [si], '$'
je end_delete
mov al, [si]
cmp al, 13
je check_next_char
mov [di], al
inc di
inc si
jmp delete_loop
check_next_char:
inc si ; Move to next character (LF)
mov al, [si]
cmp al, 10
je delete_task_found
mov byte ptr [di], 13
inc di
mov [di], al
inc di
inc si
jmp delete_loop
delete_task_found:
; Check if it's the desired task to delete
sub al, '0'
cmp al, bl
je skip_task
jmp copy_task
skip_task:
; Skip the task to delete
mov al, [si]
cmp al, 13
jne skip_task
jmp delete_loop
copy_task:
; Copy the task to keep it
mov [di], al
inc di
inc si
jmp delete_loop
end_delete:
; Truncate the file at the updated position
mov ah, 40h
mov bx, handle
mov dx, offset buffer
sub di, offset buffer
int 21h
; Close the file
mov ah, 3Eh
mov bx, handle
int 21h
jmp view_tasks
; Data Section
m_delete_prompt db 'Enter the task number to delete: $'
型
对于以下任务:
task1
task2
task3
task4
task5
型
这就是我得到的:
task1
task2
task3
task4
task5
task1Útask2Útask3Útask4Útask5Ú ÿÿùtâ
型
1条答案
按热度按时间iaqfqrcu1#
1.正如我在上面的评论中所说,应该保存删除任务的数量。所以
push bx
完成了这项工作。在delete_loop:
之前使用pop bx
恢复此值。1.在
delete_loop
的开始处的指令cmp byte ptr [si], '$'
搜索$
,但是buffer
被0
填充。所以这个循环可以永远运行,除非内存中的某个地方有$
。所以要么用$
填充缓冲区,要么在buffer
之后添加变量eotb db '$'
来标记缓冲区的结束。delete_loop
里面发生的事情是这样的。0d
,则转到check_next_char:
0a
,则转到delete_task_found:
al = 0a
,因此此指令sub al '0'
不会从'1' - '9'
中继承'0'
以获得正确的任务编号t
,a
,s
,k
,1 - 9
被跳过task 1-9 0d 0a
buffer + 4
buffer + 4 + 7
buffer + 4 + 7 + 7
个buffer + 4 + 7 + 7 + 7
buffer + 4 + 7 + 7 + 7 ...
$
意味着文件中没有任务。字符串