关于x86 16:计算字符串中的空格,它打印字符串并且CX保持为0。程序集8086

Counting spaces in a string, it prints the string and CX stays 0. assembly 8086

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
.model  small
.stack  100H
.data

A   db   '   this is    a test $'

.code  

    mov ax, @data
    mov ds, ax

    mov si, 0
    mov cx, 0  

myloop:

    cmp A[si], '$'
    je final

    cmp  A[si], ' '  
    inc si
    je count
    jmp myloop

count:

    inc cx
    jmp myloop

final:

    mov dx, cx
    mov ah, 9
    int 21h

end


您通过随后的 "inc si"

覆盖了 "is blank" 比较的标志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
.model  small
.stack  100H
.data

A   db   '   this is    a test $'

.code  

    mov ax, @data
    mov ds, ax

    mov si, 0
    mov cx, 0  

myloop:

    cmp A[si], '$'
    je final

    cmp  A[si], ' '  
    jne do_not_count   ; skip count if it's not a blank

count:
    inc cx             ; it is a blank, count it

do_not_count:
    inc si
    jmp myloop

final:

    ;mov dx, cx                     ; this does NOT print CX
    ;mov ah, 9
    ;int 21h

    mov dl, cl                      ; workaround: this works for cx < 10
    add dl, '0'
    mov ah, 2
    int 21h

end