接受字符串并在程序集中显示其长度

Accepting a string and displaying its length in assembly

print macro msg
lea dx, msg
mov ah, 09h
int 21h
endm

data segment
msg1        db  13, 10, 10, 'Enter the string: $'
msg2        db  13, 10, 10, 'The entered string is: $'
msg3        db  13, 10, 10, 'The length of the string is: $'
array       db  20 dup(0)
lenstr      db  5   dup(0)
data ends

code segment
assume cs: code, ds: data
start:      mov ax, data
    mov ds, ax
    
    print msg1
    lea si, array

这是一个ALP,用于接受来自键盘的字符串并显示其长度。有很多部分我不明白程序是如何工作的。

read:       mov ah, 01h
int 21h
cmp al, 0dh
je next
mov [si], al
inc si
inc bl
jmp read

在这里,当 al 不包含任何开头的值时,我们为什么要比较 al, 0dh
所以,如果有人能解释整个程序,那将非常有帮助。

next:       mov al, '$'
mov [si], al
print msg2
print array
print msg3
lea si, lenstr

xor ax, ax
mov al, bl
mov bl, 10
div bl
add al, 30h
mov [si], al
inc di
add ah, 30h
mov [si], ah
inc si
mov al, '$'
mov [si], al
print lenstr
mov ax, 4c00h
int 21h
code ends
end start

另外,我们不能只使用 count db $-array 来计算字符串的长度吗?

read: mov ah, 01h  ; Identification of DOS service.
int 21h            ; Invokation of the service.  
cmp al, 0dh
je next
mov [si], al
inc si
inc bl
jmp read

DOS 服务 Int 21h/AH=01h 等待直到用户按下一个键,例如数字 5,它 returns AL=35h 在这个案件。当她|他按下 Enter(表示字符串结束)时,服务 returns AL=0Dh.

why are we comparing al,0dh, when al does not contain any value to begin with?

你最好问为什么我们要递增 bl,因为我们不知道它之前是什么?我们不能依赖寄存器在程序开始时包含 0(有些寄存器确实如此)。显然 bl 是为了计算输入的字符数。

Also, can't we just use count db $-array to find the length of the string?

仅当字符串是在程序中静态定义的。我们不知道用户要输入多少个字符,所以我们必须在 运行.
上动态计算它们 在最后输入的字符后插入 $ 是 DOS 服务 Int 21h/AH=09h 所要求的,它在宏 print.

中使用