计算字符串中的空格,打印字符串,CX保持为0.程序集8086

时间:2016-06-06 21:08:21

标签: assembly x86-16 emu8086

.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

1 个答案:

答案 0 :(得分:0)

你覆盖&#34的标志;空白"通过随后的" inc si"

进行比较
.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